204 lines
4.5 KiB
JavaScript
204 lines
4.5 KiB
JavaScript
import store from '@/store';
|
||
import defaultSettings from '@/config/defaultSettings';
|
||
/**
|
||
* 通用js方法封装处理
|
||
* Copyright (c) 2019 ruoyi
|
||
*/
|
||
|
||
/**
|
||
* 动态修改标题
|
||
*/
|
||
export function useDynamicTitle() {
|
||
if (store.state.settings.dynamicTitle) {
|
||
document.title = `${store.state.settings.title} - ${defaultSettings.title}`;
|
||
} else {
|
||
document.title = defaultSettings.title;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @param {Array} actual
|
||
* @returns {Array}
|
||
*/
|
||
export function trimArray(actual) {
|
||
const newArray = [];
|
||
for (let i = 0; i < actual.length; i++) {
|
||
if (actual[i]) {
|
||
newArray.push(actual[i]);
|
||
}
|
||
}
|
||
return newArray;
|
||
}
|
||
|
||
/**
|
||
* @param {Array} arr
|
||
* @returns {Array}
|
||
*/
|
||
export function arrayToSet(arr) {
|
||
return Array.from(new Set(arr));
|
||
}
|
||
|
||
// 表单重置
|
||
export function resetForm(refName) {
|
||
if (this.$refs[refName]) {
|
||
this.$refs[refName].resetFields();
|
||
}
|
||
}
|
||
|
||
// 添加时间查询参数
|
||
export function addTimeRange(params, dateRange) {
|
||
if (dateRange != null) {
|
||
const [beginTime, endTime] = dateRange;
|
||
params.beginTime = beginTime;
|
||
params.endTime = endTime;
|
||
} else {
|
||
params.beginTime = null;
|
||
params.endTime = null;
|
||
}
|
||
return params;
|
||
}
|
||
|
||
// 转换字符串,undefined,null等转化为""
|
||
export function parseStrEmpty(str) {
|
||
if (!str || str === 'undefined' || str === 'null' || str === null) {
|
||
return '';
|
||
}
|
||
return str;
|
||
}
|
||
|
||
// 数据合并
|
||
export function mergeRecursive(source, target) {
|
||
for (const p in target) {
|
||
try {
|
||
if (target[p].constructor === Object) {
|
||
source[p] = mergeRecursive(source[p], target[p]);
|
||
} else {
|
||
source[p] = target[p];
|
||
}
|
||
} catch (e) {
|
||
source[p] = target[p];
|
||
}
|
||
}
|
||
return source;
|
||
}
|
||
|
||
/**
|
||
* Merges two objects, giving the last one precedence
|
||
* @param {Object} target
|
||
* @param {(Object|Array)} source
|
||
* @returns {Object}
|
||
*/
|
||
export function objectMerge(target, source) {
|
||
if (typeof target !== 'object') {
|
||
target = {};
|
||
}
|
||
if (Array.isArray(source)) {
|
||
return source.slice();
|
||
}
|
||
Object.keys(source).forEach((property) => {
|
||
const sourceProperty = source[property];
|
||
if (typeof sourceProperty === 'object') {
|
||
target[property] = objectMerge(target[property], sourceProperty);
|
||
} else {
|
||
target[property] = sourceProperty;
|
||
}
|
||
});
|
||
return target;
|
||
}
|
||
|
||
/**
|
||
* This is just a simple version of deep copy
|
||
* Has a lot of edge cases bug
|
||
* If you want to use a perfect deep copy, use lodash's _.cloneDeep
|
||
* @param {Object} source
|
||
* @returns {Object}
|
||
*/
|
||
export function deepClone(source) {
|
||
if (!source && typeof source !== 'object') {
|
||
throw new Error('error arguments', 'deepClone');
|
||
}
|
||
const targetObj = source.constructor === Array ? [] : {};
|
||
Object.keys(source).forEach((keys) => {
|
||
if (source[keys] && typeof source[keys] === 'object') {
|
||
targetObj[keys] = deepClone(source[keys]);
|
||
} else {
|
||
targetObj[keys] = source[keys];
|
||
}
|
||
});
|
||
return targetObj;
|
||
}
|
||
|
||
/**
|
||
* 构造树型结构数据
|
||
* @param {*} data 数据源
|
||
* @param {*} id id字段 默认 'id'
|
||
* @param {*} parentId 父节点字段 默认 'parentId'
|
||
* @param {*} children 孩子节点字段 默认 'children'
|
||
*/
|
||
export function handleTree(data, id, parentId, children) {
|
||
const config = {
|
||
id: id || 'id',
|
||
parentId: parentId || 'parentId',
|
||
childrenList: children || 'children',
|
||
};
|
||
|
||
const childrenListMap = {};
|
||
const nodeIds = {};
|
||
const tree = [];
|
||
|
||
for (const d of data) {
|
||
const parentId = d[config.parentId];
|
||
if (childrenListMap[parentId] == null) {
|
||
childrenListMap[parentId] = [];
|
||
}
|
||
nodeIds[d[config.id]] = d;
|
||
childrenListMap[parentId].push(d);
|
||
}
|
||
|
||
for (const d of data) {
|
||
const parentId = d[config.parentId];
|
||
if (nodeIds[parentId] == null) {
|
||
tree.push(d);
|
||
}
|
||
}
|
||
|
||
for (const t of tree) {
|
||
adaptToChildrenList(t);
|
||
}
|
||
|
||
function adaptToChildrenList(o) {
|
||
if (childrenListMap[o[config.id]] !== null) {
|
||
o[config.childrenList] = childrenListMap[o[config.id]];
|
||
}
|
||
if (o[config.childrenList]) {
|
||
for (const c of o[config.childrenList]) {
|
||
adaptToChildrenList(c);
|
||
}
|
||
}
|
||
}
|
||
return tree;
|
||
}
|
||
|
||
// 返回项目路径
|
||
export function getNormalPath(p) {
|
||
if (p.length === 0 || !p || p === undefined) {
|
||
return p;
|
||
}
|
||
const res = p.replace('//', '/');
|
||
if (res[res.length - 1] === '/') {
|
||
return res.slice(0, res.length - 1);
|
||
}
|
||
return res;
|
||
}
|
||
|
||
// 验证是否为blob格式
|
||
export async function isBlobData(data) {
|
||
try {
|
||
const text = await data.text();
|
||
JSON.parse(text);
|
||
return false;
|
||
} catch (error) {
|
||
return true;
|
||
}
|
||
}
|