152 lines
4.1 KiB
JavaScript
152 lines
4.1 KiB
JavaScript
import request from '../api'
|
||
import store from '../store'
|
||
import { Message } from 'element-ui'
|
||
import JsBarcode from 'jsbarcode'
|
||
import { Interpreter } from 'eval5'
|
||
import { transform } from "@babel/standalone"
|
||
|
||
const dict = {
|
||
url: "/api/dictionary/queryValsByCodeList",
|
||
loading: [],
|
||
resolves: [],
|
||
getStorage() {
|
||
const dicts = JSON.parse(localStorage.getItem('dicts') || null)
|
||
return dicts?.data || dicts || [];
|
||
},
|
||
setUrl(v) {
|
||
this.url = v
|
||
},
|
||
getData(codeList) {
|
||
codeList = [codeList].flat().filter(Boolean).toString()
|
||
return request.post(this.url, null, {
|
||
withoutToken: true, params: {codeList}
|
||
}).then(res => res?.data && this.setStorage(res.data))
|
||
},
|
||
load(...code) {
|
||
return new Promise(resolve => {
|
||
this.resolves.push(resolve)
|
||
if (this.loading.length == 2) {
|
||
const [timer, codes] = this.loading;
|
||
clearTimeout(timer)
|
||
code = Array.from(new Set([codes, code].flat()))
|
||
}
|
||
const timer = setTimeout(() => {
|
||
this.getData(code).then(() => Promise.all(this.resolves.map(e => e())).then(() => this.resolves = []))
|
||
}, 500)
|
||
this.loading = [timer, code]
|
||
})
|
||
},
|
||
setStorage(data) {
|
||
let ds = this.getStorage()
|
||
data.map(p => {
|
||
if (ds.some(d => d.key == p.key)) {
|
||
const index = ds.findIndex(d => d.key == p.key)
|
||
ds.splice(index, 1, p)
|
||
} else {
|
||
ds.push(p)
|
||
}
|
||
})
|
||
localStorage.setItem("dicts", JSON.stringify([ds].flat()))
|
||
},
|
||
getDict(key) {
|
||
let dict = this.getStorage().find(e => e.key == key)
|
||
!dict && console.warn("字典%s缺少加载...", key)
|
||
return dict ? dict.values : []
|
||
},
|
||
getValue(key, label) {
|
||
let dict = this.getDict(key)
|
||
if (dict) {
|
||
let item = dict.find(v => v.dictName == label)
|
||
return item ? item.dictValue : label
|
||
} else return label
|
||
},
|
||
getLabel(key, value) {
|
||
let dict = this.getDict(key)
|
||
if (dict) {
|
||
let item = dict.find(v => v.dictValue == value)
|
||
return item ? item.dictName : value
|
||
} else return value
|
||
},
|
||
getColor(key, value) {
|
||
let dict = this.getDict(key)
|
||
if (dict) {
|
||
let item = dict.find(v => v.dictValue == value)
|
||
return item ? item.dictColor : value
|
||
} else return value
|
||
},
|
||
}
|
||
|
||
const dateUtil = {
|
||
timestampToTime(timestamp) {
|
||
// 时间戳为10位需*1000,时间戳为13位不需乘1000
|
||
let date = new Date(timestamp);
|
||
let Y = date.getFullYear() + "-";
|
||
let M =
|
||
(date.getMonth() + 1 < 10
|
||
? "0" + (date.getMonth() + 1)
|
||
: date.getMonth() + 1) + "-";
|
||
let D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
|
||
let h = date.getHours() + ":";
|
||
let m = date.getMinutes() + ":";
|
||
let s = date.getSeconds();
|
||
return Y + M + D + h + m + s;
|
||
}
|
||
}
|
||
|
||
const userCheck = (mallId) => {
|
||
return new Promise((resolve, reject) => {
|
||
store.dispatch('getUserInfo').then(res => {
|
||
if (res.flag != 1) {
|
||
Message.error('您的账号未激活或已失效,请激活后使用')
|
||
store.commit('setActiveDlgShow', true)
|
||
reject('您的账号未激活或已失效,请激活后使用')
|
||
return false
|
||
}
|
||
let tempMallId = mallId
|
||
if (!tempMallId) {
|
||
tempMallId = store.state.mallId
|
||
}
|
||
if (res.type != 4 && tempMallId != store.state.userInfo.mallId) {
|
||
Message.error('您当前登录的卖家中心店铺与会员绑定店铺不一致')
|
||
reject('您当前登录的卖家中心店铺与会员绑定店铺不一致')
|
||
return false
|
||
}
|
||
|
||
resolve(res.data)
|
||
})
|
||
})
|
||
}
|
||
|
||
const sleepSync = (milliseconds) => {
|
||
return new Promise(resolve => setTimeout(resolve, milliseconds));
|
||
}
|
||
|
||
const initWindow = () => {
|
||
const transformCode = (codeStr) => {
|
||
return transform(codeStr, { presets: ['env'] }).code
|
||
}
|
||
|
||
window.JsBarcode = JsBarcode
|
||
window.eval = (code, context) => {
|
||
console.log(code)
|
||
const interpreter = new Interpreter(context || window, {
|
||
timeout: 1000,
|
||
})
|
||
try {
|
||
interpreter.evaluate(transformCode(code))
|
||
} catch (err) {
|
||
console.log(err)
|
||
}
|
||
}
|
||
}
|
||
|
||
export default {
|
||
dict,
|
||
dateUtil,
|
||
sleepSync,
|
||
userCheck,
|
||
initWindow
|
||
}
|
||
|
||
|