utils.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 {nextTick} from 'vue'
  6. import router from "@/router/index";
  7. const config = {
  8. number: 'BigNumber',
  9. precision: 20
  10. }
  11. const math = create(all, config)
  12. let time = null
  13. const hide = null
  14. let questArray = [];
  15. export default {
  16. // 判断应用是否授权
  17. hasAuth (app) {
  18. if (!app) {
  19. return false
  20. } else {
  21. return true
  22. }
  23. },
  24. // 检查权限是否存在
  25. hasPermission (permission) {
  26. let hasPermission = true
  27. const store = useAuthStore()
  28. let appData = store.app
  29. let auth = appData.meta.auth
  30. if (appData.meta.auth) {
  31. hasPermission = auth.some(item=>item.option == permission)
  32. return hasPermission
  33. }
  34. },
  35. // 获取应用表格
  36. TBLayout (tablename,i) {
  37. try {
  38. // 获取应用数据
  39. const store = useAuthStore()
  40. const hideAmount = store.hiddenSensitiveFields
  41. let apps = store.app
  42. // 获取当前应用表格数据
  43. if (apps.meta.tables[tablename]) {
  44. let tablecols = apps.meta.tables[tablename].tablecols.map(e=>{
  45. return {
  46. title:e.title,
  47. filter:e.filter,
  48. sortable:e.sortable,
  49. dataIndex:e.columnname,
  50. sort:0,
  51. align:e.align?e.align:'left',
  52. width:e.width == 0?'150':e.width,
  53. fixed:e.freezetype ? e.freezetype !== '' ? e.freezetype : false:false,
  54. fn:e.script !=='' ? (data,dataIndex)=>{
  55. try {
  56. let func = new Function('data','hideAmount',e.script);
  57. return func(data,hideAmount); // 输出1
  58. } catch (error) {
  59. console.log(error)
  60. }
  61. }:null,
  62. ellipsis:true
  63. }
  64. })
  65. return tablecols
  66. }
  67. } catch (error) {
  68. console.log(error,'tool')
  69. }
  70. },
  71. // 获取表头及表单配置
  72. FormLayout (name,data) {
  73. var that = this
  74. /** 验证数据格式类型 */
  75. let checkType = (dataformat,value)=>{
  76. if (dataformat && dataformat.type && dataformat.type !== '' && dataformat.type !== 'none') {
  77. switch (dataformat.type) {
  78. case 'number':
  79. return that.formatAmount(value)
  80. break;
  81. case 'mapping':
  82. dataformat.mapping.some(e=>{
  83. if (value == e.value) {
  84. value = e.label
  85. }
  86. })
  87. return value
  88. break;
  89. default:
  90. break;
  91. }
  92. } else {
  93. return value
  94. }
  95. }
  96. try {
  97. // 获取应用数据
  98. const store = useAuthStore()
  99. let apps = store.app
  100. // 获取当前应用表格数据
  101. if (apps.meta.forms[name]) {
  102. let formlat = apps.meta.forms[name].formcols.map(e=>{
  103. return {
  104. label:e.title,
  105. value:checkType(e.dataformat,data[e.columnname]),
  106. key:e.columnname,
  107. span:e.span,
  108. style:e.script !=='' ? ()=>{
  109. try {
  110. let func = new Function('data','that',e.script);
  111. return func(data,that); // 输出1
  112. } catch (error) {
  113. console.log(error)
  114. }
  115. }:null,
  116. }
  117. })
  118. return formlat
  119. }
  120. } catch (error) {
  121. console.log(error,'tool')
  122. }
  123. },
  124. TBLayoutID (appname) {
  125. try {
  126. // 获取应用数据
  127. const store = useAuthStore()
  128. let apps = store.app
  129. // 获取当前应用表格数据
  130. if (apps.meta.tables[appname]) {
  131. let id = apps.meta.tables[appname].tableid
  132. return id
  133. }
  134. } catch (error) {
  135. console.log(error,'tool')
  136. }
  137. },
  138. // 判断参数隐藏
  139. hideFields (name,key) {
  140. try {
  141. const store = useAuthStore()
  142. let appData = store.app
  143. let keyArray = appData.meta.forms[name]
  144. let isShow = keyArray.formcols.some(e=>key == e.columnname)
  145. return isShow
  146. } catch (error) {
  147. }
  148. },
  149. // 验证按钮状态
  150. isDisabled (status,arr,fn) {
  151. let rs = false
  152. if (fn) return fn
  153. rs = arr.some(item=>item == status)
  154. return rs
  155. },
  156. // 数据消息提醒
  157. message (res,msg,fn) {
  158. if (!res) return false
  159. if (res.code === 1) {
  160. msg ? message.success({content:msg,key:1}) : ''
  161. if(time !== null){
  162. clearTimeout(time);
  163. }
  164. time = setTimeout(() => {
  165. fn ? fn() : ''
  166. },500)
  167. } else {
  168. console.log(res)
  169. message.error({content:res.data + ':' + res.msg,key:1})
  170. }
  171. },
  172. // 金额格式化
  173. formatAmount(amount, data) {
  174. console.log(data)
  175. if (data && !this.hideFields(data.name,data.key)) return '****'
  176. let amt = math.format(Number(amount), {notation: 'fixed',precision: 2})
  177. const amountStr = String(amt)
  178. const reg = /\B(?=(?:\d{3})+$)/g
  179. // 是否是小数
  180. const isDecimal = amountStr.indexOf('.') > -1
  181. if (isDecimal) {
  182. // 整数部分
  183. const integerPart = amountStr.substring(0, amountStr.indexOf('.'))
  184. // 小数部分
  185. const decimalPart = amountStr.substring(amountStr.length, amountStr.indexOf('.'))
  186. return `${integerPart.replace(reg, ',')}${decimalPart}`
  187. } else {
  188. return amountStr.replace(reg, ',')
  189. }
  190. },
  191. // 验证数字输入框手动输入的值是否合法
  192. validateInputNumber (start,value,step) {
  193. return new Promise((reslove,reject)=>{
  194. console.log(start,value)
  195. if (!value) return reslove(start)
  196. if ((value * 100 - start * 100) % (step * 100) === 0) {
  197. reslove(value)
  198. } else {
  199. message.error(value?`输入的值${value}不符合增量规则,已修正!`:`输入的值不能为空!`);
  200. let val = value - ((value - start) % step)
  201. reslove(val)
  202. }
  203. })
  204. },
  205. // 设置状态颜色
  206. statusAndColor(status) {
  207. let statusArr = [
  208. {st:'新建',cl:"#1677ff"},
  209. {st:'提交',cl:'#646cff'},
  210. {st:'审核',cl:'#ff5656'},
  211. {st:'关闭',cl:'#acbdc5'},
  212. {st:'预提交',cl:'#005792'},
  213. {st:'确认',cl:'#01352c'},
  214. {st:'复核',cl:'#ff9234'},
  215. {st:'启用',cl:"#1890ff"},
  216. {st:'停用',cl:'#acbdc5'},
  217. {st:'发布',cl:'#52c41a'},
  218. ]
  219. if (statusArr.find(e=>e.st == status)) {
  220. return statusArr.find(e=>e.st == status).cl
  221. } else {
  222. return '#333'
  223. }
  224. },
  225. // 接口防抖
  226. questArray,
  227. anti_shake (config, axios) {
  228. const now_date = new Date().getTime();
  229. const request_info = JSON.parse(sessionStorage.getItem("request_url"));
  230. sessionStorage.setItem(
  231. "request_url",
  232. JSON.stringify({ url: config.type, time: new Date().getTime() })
  233. );
  234. if (request_info === null) return true;
  235. if (config.type == 'post') {
  236. useBaseStore().canPointer = false
  237. }
  238. //存储请求队列,判断post请求是否为重复的请求
  239. if (config.data.id) {
  240. questArray.push(Number(config.data.id))
  241. }
  242. if (now_date - request_info.time < 500 && config.type == 'post') {
  243. let cancel;
  244. config.cancelToken = new axios.CancelToken((c) => {
  245. cancel = c;
  246. });
  247. message.error({content:'请求过于频繁,请稍后再试!',duration:1,key:1});
  248. useBaseStore().canPointer = true
  249. cancel(`${config.url}请求被中断`);
  250. return false;
  251. } else {
  252. return true;
  253. }
  254. },
  255. messageLoading: {
  256. hide:()=>{
  257. message.loading({content:'操作正在执行,请稍等..',duration:10,});
  258. }
  259. },
  260. date:{
  261. yearStart:`${(new Date).getFullYear()-1}-01-01`,
  262. yearEnd:`${(new Date).getFullYear()}-12-31`,
  263. }
  264. }