| | |
| | | import i18n from '@/i18n' |
| | | export function getImageUrl(path) { |
| | | return new URL(path, import.meta.url).href |
| | | import Vue from 'vue' |
| | | import router from '@/router' |
| | | import store from '@/store' |
| | | |
| | | /** |
| | | * 获取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) |
| | | }) |
| | | } |
| | | |
| | | // 设置localStorage |
| | | export const setStorage = function (key, obj) { |
| | | let json = JSON.stringify(obj) |
| | | window.localStorage.setItem(key, json) |
| | | // console.log('设置语言', key, json) |
| | | /** |
| | | * 是否有权限 |
| | | * @param {*} key |
| | | */ |
| | | export function isAuth (key) { |
| | | let authorities = JSON.parse(sessionStorage.getItem('authorities') || '[]') |
| | | if (authorities.length) { |
| | | for (const i in authorities) { |
| | | const element = authorities[i] |
| | | if (element === key) { |
| | | return true |
| | | } |
| | | } |
| | | } |
| | | return false |
| | | } |
| | | |
| | | // 获取localStorage |
| | | export const getStorage = function (key) { |
| | | const str = window.localStorage.getItem(key) || '' |
| | | if (!str) { |
| | | return null |
| | | /** |
| | | * 树形数据转换 |
| | | * @param {*} data |
| | | * @param {*} id |
| | | * @param {*} pid |
| | | */ |
| | | export function treeDataTranslate (data, id = 'id', pid = 'parentId') { |
| | | var res = [] |
| | | var temp = {} |
| | | for (var i = 0; i < data.length; i++) { |
| | | temp[data[i][id]] = data[i] |
| | | } |
| | | return JSON.parse(str) |
| | | for (var k = 0; k < data.length; k++) { |
| | | if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) { |
| | | if (!temp[data[k][pid]]['children']) { |
| | | temp[data[k][pid]]['children'] = [] |
| | | } |
| | | // 获取浏览器默认语言 |
| | | export const getBrowserLang = function () { |
| | | let browserLang = navigator.language |
| | | ? navigator.language |
| | | : navigator.browserLanguage |
| | | let defaultBrowserLang = '' |
| | | if ( |
| | | browserLang.toLowerCase() === 'cn' || |
| | | browserLang.toLowerCase() === 'zh' || |
| | | browserLang.toLowerCase() === 'zh-cn' |
| | | ) { |
| | | defaultBrowserLang = 'CN' |
| | | if (!temp[data[k][pid]]['_level']) { |
| | | temp[data[k][pid]]['_level'] = 1 |
| | | } |
| | | data[k]['_level'] = temp[data[k][pid]]._level + 1 |
| | | temp[data[k][pid]]['children'].push(data[k]) |
| | | } else { |
| | | defaultBrowserLang = 'en' |
| | | res.push(data[k]) |
| | | } |
| | | return defaultBrowserLang |
| | | } |
| | | return res |
| | | } |
| | | |
| | | export const dataTime = (data, isTrue) => { |
| | | var date = new Date(data) |
| | | let Y = date.getFullYear() + '-' |
| | | let M = |
| | | (date.getMonth() + 1 < 10 |
| | | ? '0' + (date.getMonth() + 1) |
| | | : date.getMonth() + 1) + '-' |
| | | let D = date.getDate() + ' ' |
| | | let h = date.getHours() + ':' |
| | | let m = date.getMinutes() + ':' |
| | | let s = date.getSeconds() |
| | | let str = Y + M + D |
| | | if (isTrue) { |
| | | str = Y + M + D + h + m + s |
| | | } else { |
| | | str = Y + M + D |
| | | } |
| | | return str |
| | | /** |
| | | * 将数组中的parentId列表取出,倒序排列 |
| | | * @param {*} data |
| | | * @param {*} id |
| | | * @param {*} pid |
| | | */ |
| | | export function idList (data, val, id = 'id', children = 'children') { |
| | | let res = [] |
| | | idListFromTree(data, val, res, id) |
| | | return res |
| | | } |
| | | |
| | | export const debounce = (fn, delay = 500) => { |
| | | // timer 是在闭包中的 |
| | | let timer = null |
| | | |
| | | return function () { |
| | | if (timer) { |
| | | clearTimeout(timer) |
| | | } |
| | | timer = setTimeout(() => { |
| | | fn.apply(this, arguments) |
| | | timer = null |
| | | }, delay) |
| | | /** |
| | | * @param {*} data |
| | | * @param {*} id |
| | | * @param {*} pid |
| | | */ |
| | | function idListFromTree (data, val, res = [], id = 'id', children = 'children') { |
| | | for (let i = 0; i < data.length; i++) { |
| | | const element = data[i] |
| | | if (element[children]) { |
| | | if (idListFromTree(element[children], val, res, id, children)) { |
| | | res.push(element[id]) |
| | | return true |
| | | } |
| | | } |
| | | |
| | | export const throttle = (fn, wait = 50) => { |
| | | // 上一次执行 fn 的时间 |
| | | let previous = 0 |
| | | // 将 throttle 处理结果当作函数返回 |
| | | return function (...args) { |
| | | // 获取当前时间,转换成时间戳,单位毫秒 |
| | | let now = +new Date() |
| | | // 将当前时间和上一次执行函数的时间进行对比 |
| | | // 大于等待时间就把 previous 设置为当前时间并执行函数 fn |
| | | if (now - previous > wait) { |
| | | previous = now |
| | | fn.apply(this, args) |
| | | if (element[id] === val) { |
| | | res.push(element[id]) |
| | | return true |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 小数展示,保留五位小数 |
| | | export const formatNumber = (num) => { |
| | | if (typeof num !== 'number') { |
| | | return '--' |
| | | } |
| | | let count |
| | | if (num.toString().indexOf('.') < 0) { |
| | | // 整数 |
| | | return num |
| | | } else { |
| | | // 小数 |
| | | count = num.toString().split('.').pop().length |
| | | if (count > 5) { |
| | | return num.toFixed(5) |
| | | } else { |
| | | return num |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | // 保留两位小数 |
| | | export const fixDate = (val, language) => { |
| | | const value = val / 1 |
| | | if (isNaN(value)) { |
| | | return '--' |
| | | } |
| | | if (i18n.locale === 'CN' || language == "zh-CN" || language == "CN") { |
| | | if ((value / 10000).toString().split('.')[0].length <= 4) { |
| | | return (value / 10000).toFixed(2) + ' ' + i18n.global.t('万') |
| | | } else { |
| | | return (value / 100000000).toFixed(2) + ' ' + i18n.global.t('亿') |
| | | } |
| | | } else { |
| | | if ((value / 1000000).toString().split('.')[0].length <= 4) { |
| | | return (value / 1000000).toFixed(2) + ' ' + 'M' |
| | | } else { |
| | | return (value / 1000000000).toFixed(2) + ' ' + 'B' |
| | | } |
| | | } |
| | | } |
| | | |
| | | export const formatMoney = (val) => { |
| | | if (val === 0) { |
| | | return 0 |
| | | } |
| | | if (!val) { |
| | | return '-' |
| | | } |
| | | const num = 1000 //byte |
| | | if (val < num) { |
| | | return val |
| | | } |
| | | if (val < Math.pow(num, 2)) { |
| | | return (val / num).toFixed(2) + 'K' //k |
| | | } |
| | | if (val < Math.pow(num, 3)) { |
| | | return (val / Math.pow(num, 2)).toFixed(2) + 'M' //M |
| | | } |
| | | if (val < Math.pow(num, 4)) { |
| | | return (val / Math.pow(num, 3)).toFixed(2) + 'G' //G |
| | | } |
| | | return (val / Math.pow(num, 4)).toFixed(2) + 'T' //T |
| | | } |
| | | |
| | | export const handleImage = (url) => { |
| | | return new URL(url, import.meta.url).href |
| | | } |
| | | |
| | | export const formatNews = (str) => { |
| | | if (!str) { |
| | | return '' |
| | | } |
| | | return str |
| | | .replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/g, '') |
| | | .replace(/ /gi, '') |
| | | /** |
| | | * 清除登录信息 |
| | | */ |
| | | export function clearLoginInfo () { |
| | | Vue.cookie.delete('Authorization') |
| | | store.commit('resetStore') |
| | | router.options.isAddDynamicMenuRoutes = false |
| | | } |