import { message } from 'ant-design-vue'; import { create, all } from 'mathjs' import { useAuthStore } from "@/stores/modules/auth"; import { useBaseStore } from '@/stores/modules/base' import router from "@/router/index"; const config = { number: 'BigNumber', precision: 20 } const math = create(all, config) let time = null const hide = null let questArray = []; export default { // 判断应用是否授权 hasAuth (app) { if (!app) { return false } else { return true } }, // 检查权限是否存在 hasPermission (permission) { let hasPermission = true const store = useAuthStore() let appData = store.app let auth = appData.meta.auth if (appData.meta.auth) { hasPermission = auth.some(item=>item.option == permission) return hasPermission } }, // 获取应用表格 TBLayout (tablename,i) { try { // 获取应用数据 const store = useAuthStore() const hideAmount = store.hiddenSensitiveFields let apps = store.app // 获取当前应用表格数据 if (apps.meta.tables[tablename]) { let tablecols = apps.meta.tables[tablename].tablecols.map(e=>{ return { title:e.title, filter:e.filter, sortable:e.sortable, dataIndex:e.columnname, sort:0, align:e.align?e.align:'left', width:e.width == 0?'150':e.width, fixed:e.freezetype ? e.freezetype !== '' ? e.freezetype : false:false, fn:e.script !=='' ? (data,dataIndex)=>{ try { let func = new Function('data','hideAmount',e.script); return func(data,hideAmount); // 输出1 } catch (error) { console.log(error) } }:null, ellipsis:true } }) return tablecols } } catch (error) { console.log(error,'tool') } }, // 获取表头及表单配置 FormLayout (name,data) { var that = this /** 验证数据格式类型 */ let checkType = (dataformat,value)=>{ if (dataformat && dataformat.type && dataformat.type !== '' && dataformat.type !== 'none') { switch (dataformat.type) { case 'number': return that.formatAmount(value) break; case 'mapping': dataformat.mapping.some(e=>{ if (value == e.value) { value = e.label } }) return value break; default: break; } } else { return value } } try { // 获取应用数据 const store = useAuthStore() let apps = store.app // 获取当前应用表格数据 if (apps.meta.forms[name]) { let formlat = apps.meta.forms[name].formcols.map(e=>{ return { label:e.title, value:checkType(e.dataformat,data[e.columnname]), key:e.columnname, span:e.span, style:e.script !=='' ? ()=>{ try { let func = new Function('data','that',e.script); return func(data,that); // 输出1 } catch (error) { console.log(error) } }:null, } }) return formlat } } catch (error) { console.log(error,'tool') } }, TBLayoutID (appname) { try { // 获取应用数据 const store = useAuthStore() let apps = store.app // 获取当前应用表格数据 if (apps.meta.tables[appname]) { let id = apps.meta.tables[appname].tableid return id } } catch (error) { console.log(error,'tool') } }, // 判断参数隐藏 hideFields (name,key) { const store = useAuthStore() let appData = store.app let keyArray = appData.meta.forms[name] let isShow = keyArray.formcols.some(e=>key == e.columnname) return isShow }, // 验证按钮状态 isDisabled (status,arr,fn) { let rs = false if (fn) return fn rs = arr.some(item=>item == status) return rs }, // 数据消息提醒 message (res,msg,fn) { if (!res) return false if (res.code === 1) { msg ? message.success({content:msg,key:1}) : '' if(time !== null){ clearTimeout(time); } time = setTimeout(() => { fn ? fn() : '' },500) } else { console.log(res) message.error({content:res.data + ':' + res.msg,key:1}) } }, // 金额格式化 formatAmount(amount, fn) { console.log(fn,'------') if (fn !== undefined && !fn) return '****' const { hiddenSensitiveFields } = useAuthStore() if (hiddenSensitiveFields) return '****' let amt = math.format(Number(amount), {notation: 'fixed',precision: 2}) const amountStr = String(amt) const reg = /\B(?=(?:\d{3})+$)/g // 是否是小数 const isDecimal = amountStr.indexOf('.') > -1 if (isDecimal) { // 整数部分 const integerPart = amountStr.substring(0, amountStr.indexOf('.')) // 小数部分 const decimalPart = amountStr.substring(amountStr.length, amountStr.indexOf('.')) return `${integerPart.replace(reg, ',')}${decimalPart}` } else { return amountStr.replace(reg, ',') } }, // 验证数字输入框手动输入的值是否合法 validateInputNumber (start,value,step) { return new Promise((reslove,reject)=>{ console.log(start,value) if (!value) return reslove(start) if ((value * 100 - start * 100) % (step * 100) === 0) { reslove(value) } else { message.error(value?`输入的值${value}不符合增量规则,已修正!`:`输入的值不能为空!`); let val = value - ((value - start) % step) reslove(val) } }) }, // 设置状态颜色 statusAndColor(status) { let statusArr = [ {st:'新建',cl:"#1677ff"}, {st:'提交',cl:'#646cff'}, {st:'审核',cl:'#ff5656'}, {st:'关闭',cl:'#acbdc5'}, {st:'预提交',cl:'#005792'}, {st:'确认',cl:'#01352c'}, {st:'复核',cl:'#ff9234'}, {st:'启用',cl:"#1890ff"}, {st:'停用',cl:'#acbdc5'}, {st:'发布',cl:'#52c41a'}, ] if (statusArr.find(e=>e.st == status)) { return statusArr.find(e=>e.st == status).cl } else { return '#333' } }, // 接口防抖 questArray, anti_shake (config, axios) { const now_date = new Date().getTime(); const request_info = JSON.parse(sessionStorage.getItem("request_url")); sessionStorage.setItem( "request_url", JSON.stringify({ url: config.type, time: new Date().getTime() }) ); if (request_info === null) return true; if (config.type == 'post') { useBaseStore().canPointer = false } //存储请求队列,判断post请求是否为重复的请求 if (config.data.id) { questArray.push(Number(config.data.id)) } if (now_date - request_info.time < 500 && config.type == 'post') { let cancel; config.cancelToken = new axios.CancelToken((c) => { cancel = c; }); message.error({content:'请求过于频繁,请稍后再试!',duration:1,key:1}); useBaseStore().canPointer = true cancel(`${config.url}请求被中断`); return false; } else { return true; } }, messageLoading: { hide:()=>{ message.loading({content:'操作正在执行,请稍等..',duration:10,}); } }, date:{ yearStart:`${(new Date).getFullYear()-1}-01-01`, yearEnd:`${(new Date).getFullYear()}-12-31`, } }