utils.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. import { message } from 'ant-design-vue';
  2. import { create, all } from 'mathjs'
  3. import { useAuthStore } from "@/stores/modules/auth";
  4. import { useBaseStore } from '@/stores/modules/base'
  5. import Api from '../api/api'
  6. const config = {
  7. number: 'BigNumber',
  8. precision: 20
  9. }
  10. const math = create(all, config)
  11. let time = null
  12. const hide = null
  13. let questArray = [];
  14. export default {
  15. // 判断应用是否授权
  16. hasAuth(app) {
  17. if (!app) {
  18. return false
  19. } else {
  20. return true
  21. }
  22. },
  23. // 检查权限是否存在
  24. hasPermission(permission) {
  25. let hasPermission = true
  26. const store = useAuthStore()
  27. let appData = store.app
  28. let auth = appData.meta.auth
  29. if (appData.meta.auth) {
  30. hasPermission = auth.some(item => item.option == permission)
  31. return hasPermission
  32. }
  33. },
  34. // 获取应用表格
  35. TBLayout(tablename, i) {
  36. try {
  37. // 获取应用数据
  38. const store = useAuthStore()
  39. const hideAmount = store.hiddenSensitiveFields
  40. let apps = store.app
  41. // 获取当前应用表格数据
  42. if (apps.meta.tables[tablename]) {
  43. let tablecols = apps.meta.tables[tablename].tablecols.map(e => {
  44. return {
  45. title: e.title,
  46. filter: e.filter,
  47. sortable: e.sortable,
  48. dataIndex: e.columnname,
  49. sort: 0,
  50. align: e.align ? e.align : 'left',
  51. width: e.width == 0 ? '150' : e.width,
  52. fixed: e.freezetype ? e.freezetype !== '' ? e.freezetype : false : false,
  53. fn: e.script !== '' ? (data, dataIndex) => {
  54. try {
  55. let func = new Function('data', 'hideAmount', e.script);
  56. return func(data, hideAmount); // 输出1
  57. } catch (error) {
  58. console.log(error)
  59. }
  60. } : null,
  61. ellipsis: true
  62. }
  63. })
  64. return tablecols
  65. }
  66. } catch (error) {
  67. console.log(error, 'tool')
  68. }
  69. },
  70. // 获取表头及表单配置
  71. FormLayout(name, data) {
  72. var that = this
  73. /** 验证数据格式类型 */
  74. let checkType = (dataformat, value) => {
  75. if (dataformat && dataformat.type && dataformat.type !== '' && dataformat.type !== 'none') {
  76. switch (dataformat.type) {
  77. case 'number':
  78. return that.formatAmount(value)
  79. break;
  80. case 'mapping':
  81. dataformat.mapping.some(e => {
  82. if (value == e.value) {
  83. value = e.label
  84. }
  85. })
  86. return value
  87. break;
  88. default:
  89. break;
  90. }
  91. } else {
  92. return value
  93. }
  94. }
  95. try {
  96. // 获取应用数据
  97. const store = useAuthStore()
  98. let apps = store.app
  99. // 获取当前应用表格数据
  100. if (apps.meta.forms[name]) {
  101. let formlat = apps.meta.forms[name].formcols.map(e => {
  102. return {
  103. label: e.title,
  104. value: checkType(e.dataformat, data[e.columnname]),
  105. key: e.columnname,
  106. span: e.span,
  107. style: e.script !== '' ? () => {
  108. try {
  109. let func = new Function('data', 'that', e.script);
  110. return func(data, that); // 输出1
  111. } catch (error) {
  112. console.log(error)
  113. }
  114. } : null,
  115. }
  116. })
  117. return formlat
  118. }
  119. } catch (error) {
  120. console.log(error, 'tool')
  121. }
  122. },
  123. TBLayoutID(appname) {
  124. try {
  125. // 获取应用数据
  126. const store = useAuthStore()
  127. let apps = store.app
  128. // 获取当前应用表格数据
  129. if (apps.meta.tables[appname]) {
  130. let id = apps.meta.tables[appname].tableid
  131. return id
  132. }
  133. } catch (error) {
  134. console.log(error, 'tool')
  135. }
  136. },
  137. getFormSetting(formname, col, key) {
  138. const store = useAuthStore()
  139. let appData = store.app
  140. const map = appData.meta.forms[formname].formcols.reduce((acc, item) => {
  141. acc[item.columnname] = item;
  142. return acc[col][key];
  143. }, {});
  144. return map
  145. },
  146. // 判断参数隐藏
  147. hideFields(name, key) {
  148. try {
  149. const store = useAuthStore()
  150. let appData = store.app
  151. let keyArray = appData.meta.forms[name]
  152. let isShow = keyArray.formcols.some(e => key == e.columnname)
  153. return isShow
  154. } catch (error) {
  155. }
  156. },
  157. // 验证按钮状态
  158. isDisabled(status, arr, fn) {
  159. let rs = false
  160. if (fn) return fn
  161. rs = arr.some(item => item == status)
  162. return rs
  163. },
  164. // 数据消息提醒
  165. message(res, msg, fn) {
  166. if (!res) return false
  167. if (res.code === 1) {
  168. msg ? message.success({ content: msg, key: 1 }) : ''
  169. if (time !== null) {
  170. clearTimeout(time);
  171. }
  172. time = setTimeout(() => {
  173. fn ? fn() : ''
  174. }, 500)
  175. } else {
  176. console.log(res)
  177. message.error({ content: res.data + ':' + res.msg, key: 1 })
  178. }
  179. },
  180. // 金额格式化
  181. formatAmount(amount, data) {
  182. if (data && !this.hideFields(data.name, data.key)) return '****'
  183. let amt = math.format(Number(amount), { notation: 'fixed', precision: 2 })
  184. const amountStr = String(amt)
  185. const reg = /\B(?=(?:\d{3})+$)/g
  186. // 是否是小数
  187. const isDecimal = amountStr.indexOf('.') > -1
  188. if (isDecimal) {
  189. // 整数部分
  190. const integerPart = amountStr.substring(0, amountStr.indexOf('.'))
  191. // 小数部分
  192. const decimalPart = amountStr.substring(amountStr.length, amountStr.indexOf('.'))
  193. return `${integerPart.replace(reg, ',')}${decimalPart}`
  194. } else {
  195. return amountStr.replace(reg, ',')
  196. }
  197. },
  198. // 验证数字输入框手动输入的值是否合法
  199. validateInputNumber(start, value, step) {
  200. return new Promise((reslove, reject) => {
  201. console.log(start, value)
  202. if (!value) return reslove(start)
  203. if ((value * 100 - start * 100) % (step * 100) === 0) {
  204. reslove(value)
  205. } else {
  206. message.error(value ? `输入的值${value}不符合增量规则,已修正!` : `输入的值不能为空!`);
  207. let val = value - ((value - start) % step)
  208. reslove(val)
  209. }
  210. })
  211. },
  212. // 设置状态颜色
  213. statusAndColor(status) {
  214. let statusArr = [
  215. { st: '新建', cl: "#1677ff" },
  216. { st: '提交', cl: '#646cff' },
  217. { st: '审核', cl: '#ff5656' },
  218. { st: '关闭', cl: '#acbdc5' },
  219. { st: '预提交', cl: '#005792' },
  220. { st: '确认', cl: '#01352c' },
  221. { st: '复核', cl: '#ff9234' },
  222. { st: '启用', cl: "#1890ff" },
  223. { st: '待付款', cl: '#f5222d' },
  224. { st: '停用', cl: '#acbdc5' },
  225. { st: '发布', cl: '#52c41a' },
  226. { st: '上架', cl: '#52c41a' },
  227. { st: '下架', cl: '#f5222d' },
  228. ]
  229. if (statusArr.find(e => e.st == status)) {
  230. return statusArr.find(e => e.st == status).cl
  231. } else {
  232. return '#333'
  233. }
  234. },
  235. // 接口防抖
  236. questArray,
  237. anti_shake(config) {
  238. const now_date = new Date().getTime();
  239. const request_info = JSON.parse(sessionStorage.getItem("request_url"));
  240. if (config.type == 'post') {
  241. sessionStorage.setItem(
  242. "request_url",
  243. JSON.stringify({ url: config.type, time: new Date().getTime() })
  244. );
  245. }
  246. if (request_info === null) return true;
  247. if (config.type == 'post') {
  248. useBaseStore().canPointer = false
  249. }
  250. //存储请求队列,判断post请求是否为重复的请求
  251. if (config.data.id) {
  252. questArray.push(Number(config.data.id))
  253. }
  254. if (config.type == 'post' && now_date - request_info.time < 500) {
  255. let cancel;
  256. config.cancelToken = new axios.CancelToken((c) => {
  257. cancel = c;
  258. });
  259. message.error({ content: '请求过于频繁,请稍后再试!', duration: 1, key: 1 });
  260. useBaseStore().canPointer = true
  261. cancel(`${config.url}请求被中断`);
  262. return false;
  263. } else {
  264. return true;
  265. }
  266. },
  267. messageLoading: {
  268. hide: () => {
  269. message.loading({ content: '操作正在执行,请稍等..', duration: 10, });
  270. setTimeout(() => {
  271. useBaseStore().canPointer = true
  272. }, 10000);
  273. }
  274. },
  275. date: {
  276. yearStart: `${(new Date).getFullYear() - 1}-01-01`,
  277. yearEnd: `${(new Date).getFullYear()}-12-31`,
  278. },
  279. //识别附件类型
  280. fileList(list) {
  281. let suffixList = {
  282. image: ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'JPG', 'webp', 'svg', 'tiff'],
  283. video: ['mp4', 'ogg', 'webm'],
  284. word: ['doc', 'docx'],
  285. excel: ['xls', 'xlsx'],
  286. PPT: ['ppt', 'pptx'],
  287. txt: ['txt', 'md', 'js', 'json'],
  288. PDF: ['pdf'],
  289. rar: ['7z', 'zip', 'rar', 'kz', 'ace', 'arj', 'bz2', 'cab', 'gz', 'iso', 'jar', 'lzh', 'tar', 'z'],
  290. folder: ['"folder"']
  291. },
  292. typeList = [];
  293. for (let key in suffixList) typeList.push(key);
  294. for (let i = 0; i < list.length; i++) {
  295. const suffix = list[i].postfix;
  296. if (suffix != "folder") {
  297. for (var key in suffixList) {
  298. if (suffixList[key].some(value => value == suffix)) {
  299. list[i].fileType = key;
  300. if (key == 'image') {
  301. list[i].cover = list[i].url;
  302. } else if (typeList.includes(key)) {
  303. list[i].cover = key;
  304. }
  305. }
  306. }
  307. } else {
  308. list[i].fileType = "folder";
  309. list[i].cover = 'folder';
  310. }
  311. }
  312. return list;
  313. },
  314. //compressed压缩图;thumbnail缩略图,hls转码视频,cover封面
  315. getSpecifiedImage (item, type = 'thumbnail') {
  316. if (!item) return "";
  317. let v = item.subfiles.find(v => v.type == type);
  318. return v ? v.url : item.url;
  319. },
  320. /**
  321. *
  322. * @param {接口数组} urls
  323. * @param {并发数} num
  324. * @param {请求完成回调} callback
  325. */
  326. async concurRequest(urls, num, callback) {
  327. console.log(urls);
  328. return new Promise((resole, reject) => {
  329. let index = 0 // 请求下标
  330. let count = 0 //完成数据
  331. let result = [] //结果
  332. async function _requested() {
  333. let url = urls[index]
  334. let i = index //记录请求下标
  335. index++
  336. try {
  337. let res = await Api.requested(url)
  338. result[i] = res
  339. }
  340. catch (err) {
  341. result[i] = err
  342. }
  343. finally {
  344. count++
  345. callback && callback(count, index)
  346. count >= urls.length && resole(result)
  347. index < urls.length && _requested()
  348. }
  349. }
  350. for (let index = 0; index < Math.min(urls.length,num); index++) {
  351. _requested()
  352. }
  353. })
  354. }
  355. }