import Cookies from 'js-cookie' import store from '@/store' /** * 获取字典数据列表 * @param dictType 字典类型 */ export function getDictDataList(dictType) { const type = window.SITE_CONFIG['dictList'].find( (element) => element.dictType === dictType ) if (type) { return type.dataList } else { return [] } } /** * 获取字典名称 * @param dictType 字典类型 * @param dictValue 字典值 */ export function getDictLabel(dictType, dictValue) { const type = window.SITE_CONFIG['dictList'].find( (element) => element.dictType === dictType ) if (type) { const val = type.dataList.find( (element) => element.dictValue === dictValue.toString() ) if (val) { return val.dictLabel } else { return dictValue } } else { return dictValue } } /** * 清除登录信息 */ export function clearLoginInfo() { store.commit('resetStore') Cookies.remove('token') window.SITE_CONFIG['dynamicMenuRoutesHasAdded'] = false } /** * 获取uuid */ export function getUUID() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { return (c === 'x' ? (Math.random() * 16) | 0 : 'r&0x3' | '0x8').toString(16) }) } /** * 获取svg图标(id)列表 */ export function getIconList() { var res = [] document.querySelectorAll('svg symbol').forEach((item) => { res.push(item.id) }) return res } /** * 树形数据转换 * @param {*} data * @param {*} id * @param {*} pid */ export function treeDataTranslate(data, id = 'id', pid = 'pid') { var res = [] var temp = {} for (var i = 0; i < data.length; i++) { temp[data[i][id]] = data[i] } for (var k = 0; k < data.length; k++) { if (!temp[data[k][pid]] || data[k][id] === data[k][pid]) { res.push(data[k]) continue } if (!temp[data[k][pid]]['children']) { temp[data[k][pid]]['children'] = [] } temp[data[k][pid]]['children'].push(data[k]) data[k]['_level'] = (temp[data[k][pid]]._level || 0) + 1 } return res } // 时间格式化 export function dateFormats(fmt, date) { let ret const _date = new Date(date) const opt = { 'Y+': _date.getFullYear().toString(), // 年 'm+': (_date.getMonth() + 1).toString(), // 月 'd+': _date.getDate().toString(), // 日 'H+': _date.getHours().toString(), // 时 'M+': _date.getMinutes().toString(), // 分 'S+': _date.getSeconds().toString() // 秒 // 有其他格式化字符需求可以继续添加,必须转化成字符串 } for (const k in opt) { ret = new RegExp('(' + k + ')').exec(fmt) if (ret) { fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0'))) } } return fmt }