通用
import get from 'lodash.get'
import cloneDeep from 'lodash.clonedeep'
export function deepClone(obj) {return obj ? cloneDeep(obj) : obj
}
export function lodashGet(obj, key, defaultValue = '') {return get(obj, key, defaultValue);
}
export const createUuid = (len, radix) => {var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')var uuid = []var iradix = radix || chars.lengthif (len) {for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix]} else {var ruuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'uuid[14] = '4'for (i = 0; i < 36; i++) {if (!uuid[i]) {r = 0 | Math.random() * 16uuid[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r]}}}return uuid.join('')
}
export function getDataType(data) {if (data === null) {return 'null';}if (data instanceof Array) {return 'Array';}if (data instanceof Object) {return 'Object';}return 'param is no object type';
}
export function treeTraversal(treeList) {let list = [];const loop = (tree) => {tree.map((item) => {const children = item.children ? item.children : '';delete item.children;list = [...list, item];return children ? loop(children) : '';});};loop(deepClone(treeList));return list;
}
export function findFiled(val, filed, data = []) {if (!val) {return '';}const list = data[0] && data[0].children ? treeTraversal(data) : data;const nameArr = list.filter(({ dbid }) => dbid === val);if (!nameArr.length) {return '';}return lodashGet(nameArr[0], filed);
}
export function setQueryString(val) {let querystring = JSON.parse(localStorage.getItem('queryString'))querystring = querystring || {}const { query, hash } = router.app.$routeconst { name } = router.currentRouteconst newQuery = { ...query, ...val }querystring[name] = newQuerylocalStorage.setItem('queryString', JSON.stringify(querystring))router.replace({ query: newQuery, hash })
}
export function getQueryString(data) {const { currentRoute, app } = router;const { name } = currentRoute;const val = deepClone(data);const { query } = app.$route;const queryKeys = Object.keys(query);if (queryKeys.length) {Object.keys(val).forEach((key) => {if (queryKeys.find(queryKey => queryKey === key)) {const item = query[key];if (getDataType(val[key]) === 'Array' && getDataType(item) !== 'Array') {val[key] = !item ? [] : [item];} else if (item === 'true') {val[key] = true;} else if (item === 'false') {val[key] = false;} else if (item) {val[key] = item;}}});return JSON.parse(JSON.stringify(val), (k, v) => {v = v && typeof v === 'string' && !Number.isNaN(Number(v)) ? Number(v) : v;return v;});}return val;
}
export function isLight(color) {if (!color) {return false;}const colorRgb = () => {const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;color = color.toLowerCase();if (reg.test(color)) {if (color.length === 4) {let colorNew = '#';for (let i = 1; i < 4; i += 1) {colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1))