/** * @param data: 字典值集合 * @param value:字典值key * @description: 根据字典值获取字典翻译 * @creater: liupw * @date: 2021/06/28 09:05 */ export function getViewText( data: object, value: string | number, field?: string ): string { let _text = '' let _field = '' const _data = Object.values(data) if (_data.length > 0) { const _filterData = _data.find(f => f.value == value) _text = _filterData ? _filterData.label : '' _field = field && _filterData ? _filterData[field] : '' } if (field) return _field return _text } /** * @param value:需要格式化的日期字符串 * @param pattern:日期格式,来自枚举值中的dateFormatEnum,默认是DEFAULT * @description: 格式化日期格式 * @creater: liupw * @date: 2021/06/28 09:37 */ import moment from 'moment' import { DateFormatEnum } from '@/enums/dateFormatEnum' export function getDayText( value: string, pattern: string = DateFormatEnum.DEFAULT ): string { return value ? moment(value).format(pattern) : '' } /** * @param len:需要生成字符串长度 * @description: 生成随机数字小写英文字符串(默认生成15位) * @creater: chens * @date: 2022/06/16 */ export function genRandomString(len?: number): string { let rdmString = '' const _len = len || 15 while (rdmString.length < _len) { rdmString += ((Math.random() * 37) | 0).toString(36) } return rdmString } /** * @param val: 变量值 * @description: 判断是否为空 * @creater: liupw * @date: 2021/06/28 09:05 */ export function validatenull(val: any) { if (typeof val == 'boolean') { return false } if (typeof val == 'number') { return false } if (val instanceof Array) { if (val.length == 0) return true } else if (val instanceof Object) { if (Object.keys(val).length == 0) return true // JSON.stringify(val) === '{}' } else { if ( val == 'null' || val == null || val == 'undefined' || val == undefined || val == '' ) return true return false } return false } /** * @param date1: 时间 * @param date2: 时间 * @description: 计算时间 * @creater: chens * @date: 2021/07/05 */ export const calcDate = (date1: number, date2: number) => { const date3 = date2 - date1 const days = Math.floor(date3 / (24 * 3600 * 1000)) const leave1 = date3 % (24 * 3600 * 1000) // 计算天数后剩余的毫秒数 const hours = Math.floor(leave1 / (3600 * 1000)) const leave2 = leave1 % (3600 * 1000) // 计算小时数后剩余的毫秒数 const minutes = Math.floor(leave2 / (60 * 1000)) const leave3 = leave2 % (60 * 1000) // 计算分钟数后剩余的毫秒数 const seconds = Math.round(date3 / 1000) return { leave1, leave2, leave3, days: days, hours: hours, minutes: minutes, seconds: seconds, } } /** * 获取数据类型 * @param {All} [o] 需要检测的数据 * @returns {String} */ export function getType(o) { return Object.prototype.toString.call(o).slice(8, -1) } /** * 判断是否是指定数据类型 * @param {All} [o] 需要检测的数据 * @param {String} [type] 数据类型 * @returns {Boolean} */ export function isKeyType(o, type) { return getType(o).toLowerCase() === type.toLowerCase() } /** * 深拷贝,支持常见类型 object Date Array等引用类型 * @param {Any} sth * @return {Any} */ export function deepClone(sth) { let copy if (null == sth || 'object' != typeof sth) return sth if (isKeyType(sth, 'date')) { copy = new Date() copy.setTime(sth.getTime()) return copy } if (isKeyType(sth, 'array')) { copy = [] for (let i = 0, len = sth.length; i < len; i++) { copy[i] = deepClone(sth[i]) } return copy } if (isKeyType(sth, 'object')) { copy = {} for (const attr in sth) { if (sth.hasOwnProperty(attr)) copy[attr] = deepClone(sth[attr]) } return copy } return null } /** * array to tree * @param {Any} data 数据 * @param {Any} rootId 父级id(根id) * @param {Any} pKey 字段名称 eg:parentId、pid... * @return {Any} */ export function arrayToTree(data, rootId, pKey) { const _data = data .filter(item => item[pKey] === rootId) .map(item => ({ ...item, value: item.id, children: arrayToTree(data, item.id, pKey), })) return _data.length > 0 ? _data : null } import { AUTHORITIES_LIST_KEY } from '@/global/cacheKey' import Storage from 'web-storage-cache' /** * @param value:判断按钮权限的关键字 * @description: 判断当前登录账号是否具有按钮权限 * @creater: chens * @date: 2022/04/11 */ export function getButtonPermission(value?: string | string[]): boolean { const localStorage = new Storage() const userButtonPermission = localStorage.get(AUTHORITIES_LIST_KEY) // 登录返回的用户权限菜单 if (!userButtonPermission) return true if (Array.isArray(value)) { return value.some(val => userButtonPermission.includes(val)) } return ( Array.isArray(userButtonPermission) && userButtonPermission.indexOf(value) > -1 ) } /** * @param len:需要生成字符串长度 * @description: 生成随机数字小写英文字符串(默认生成15位) * @creater: chens * @date: 2022/06/16 */ export function getRandomString(len?: number): string { let rdmString = '' const _len = len || 15 while (rdmString.length < _len) { rdmString += ((Math.random() * 37) | 0).toString(36) } return rdmString } /** * @param arr : 对象数组 * @param key : 去重关键字,默认id * @description: 对象数组去重 * @creater: chens * @date: 2023/04/20 */ export function removeDuplicate(arr: any, key?: string): Array { const obj = {} const _key = key || 'id' const newArr = arr.reduce((cur: any, next: any) => { obj[next[_key]] ? '' : (obj[next[_key]] = true && cur.push(next)) return cur }, []) // 设置cur默认类型为数组,并且初始值为空的数组 return newArr }