| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- import { message } from 'ant-design-vue';
- import { create, all } from 'mathjs'
- import { useAuthStore } from "@/stores/modules/auth";
- import { useBaseStore } from '@/stores/modules/base'
- import Api from '../api/api'
- 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')
- }
- },
- getFormSetting(formname, col, key) {
- const store = useAuthStore()
- let appData = store.app
- const map = appData.meta.forms[formname].formcols.reduce((acc, item) => {
- acc[item.columnname] = item;
- return acc[col][key];
- }, {});
- return map
- },
- // 判断参数隐藏
- hideFields(name, key) {
- try {
- const store = useAuthStore()
- let appData = store.app
- let keyArray = appData.meta.forms[name]
- let isShow = keyArray.formcols.some(e => key == e.columnname)
- return isShow
- } catch (error) {
- }
- },
- // 验证按钮状态
- 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, data) {
- if (data && !this.hideFields(data.name, data.key)) 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: '#f5222d' },
- { st: '停用', cl: '#acbdc5' },
- { st: '发布', cl: '#52c41a' },
- { st: '上架', cl: '#52c41a' },
- { st: '下架', cl: '#f5222d' },
- ]
- if (statusArr.find(e => e.st == status)) {
- return statusArr.find(e => e.st == status).cl
- } else {
- return '#333'
- }
- },
- // 接口防抖
- questArray,
- anti_shake(config) {
- const now_date = new Date().getTime();
- const request_info = JSON.parse(sessionStorage.getItem("request_url"));
- if (config.type == 'post') {
- 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 (config.type == 'post' && now_date - request_info.time < 500) {
- 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, });
- setTimeout(() => {
- useBaseStore().canPointer = true
- }, 10000);
- }
- },
- date: {
- yearStart: `${(new Date).getFullYear() - 1}-01-01`,
- yearEnd: `${(new Date).getFullYear()}-12-31`,
- },
- //识别附件类型
- fileList(list) {
- let suffixList = {
- image: ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'JPG', 'webp', 'svg', 'tiff'],
- video: ['mp4', 'ogg', 'webm'],
- word: ['doc', 'docx'],
- excel: ['xls', 'xlsx'],
- PPT: ['ppt', 'pptx'],
- txt: ['txt', 'md', 'js', 'json'],
- PDF: ['pdf'],
- rar: ['7z', 'zip', 'rar', 'kz', 'ace', 'arj', 'bz2', 'cab', 'gz', 'iso', 'jar', 'lzh', 'tar', 'z'],
- folder: ['"folder"']
- },
- typeList = [];
- for (let key in suffixList) typeList.push(key);
- for (let i = 0; i < list.length; i++) {
- const suffix = list[i].postfix;
- if (suffix != "folder") {
- for (var key in suffixList) {
- if (suffixList[key].some(value => value == suffix)) {
- list[i].fileType = key;
- if (key == 'image') {
- list[i].cover = list[i].url;
- } else if (typeList.includes(key)) {
- list[i].cover = key;
- }
- }
- }
- } else {
- list[i].fileType = "folder";
- list[i].cover = 'folder';
- }
- }
- return list;
- },
- //compressed压缩图;thumbnail缩略图,hls转码视频,cover封面
- getSpecifiedImage (item, type = 'thumbnail') {
- if (!item) return "";
- let v = item.subfiles.find(v => v.type == type);
- return v ? v.url : item.url;
- },
- /**
- *
- * @param {接口数组} urls
- * @param {并发数} num
- * @param {请求完成回调} callback
- */
- async concurRequest(urls, num, callback) {
- console.log(urls);
- return new Promise((resole, reject) => {
- let index = 0 // 请求下标
- let count = 0 //完成数据
- let result = [] //结果
- async function _requested() {
- let url = urls[index]
- let i = index //记录请求下标
- index++
- try {
- let res = await Api.requested(url)
- result[i] = res
- }
- catch (err) {
- result[i] = err
- }
- finally {
- count++
- callback && callback(count, index)
- count >= urls.length && resole(result)
- index < urls.length && _requested()
- }
- }
- for (let index = 0; index < Math.min(urls.length,num); index++) {
- _requested()
- }
- })
- }
- }
|