utils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { message } from 'ant-design-vue';
  2. import { create, all } from 'mathjs'
  3. import { useAuthStore } from "@/stores/modules/auth";
  4. const config = {
  5. number: 'BigNumber',
  6. precision: 20
  7. }
  8. const math = create(all, config)
  9. let time = null
  10. export default {
  11. // 检查权限是否存在
  12. hasPermission (permission) {
  13. let hasPermission = true
  14. let appData = JSON.parse(sessionStorage.getItem('app'))
  15. let auth = appData.meta.auth
  16. hasPermission = auth.some(item=>item.option == permission)
  17. return hasPermission
  18. },
  19. // 获取应用表格
  20. TBLayout (appname) {
  21. try {
  22. // 获取应用数据
  23. let apps = JSON.parse(sessionStorage.getItem('app'))
  24. // 获取当前应用表格数据
  25. let tablecols = apps.meta.tables[appname].tablecols.map(e=>{
  26. return {
  27. title:e.title,
  28. filter:e.filter,
  29. dataIndex:e.columnname,
  30. width:e.width == 0?'150':e.width,
  31. ellipsis:true,
  32. fixed:e.columnname == 'operation'?'right':''
  33. }
  34. })
  35. return tablecols
  36. } catch (error) {
  37. console.log(error,'tool')
  38. }
  39. },
  40. // 验证按钮状态
  41. isDisabled (status,arr,fn) {
  42. let rs = false
  43. if (fn) return fn
  44. rs = arr.some(item=>item == status)
  45. return rs
  46. },
  47. // 数据消息提醒
  48. message (res,msg,fn) {
  49. if (res.code === 1) {
  50. msg ? message.success({content:msg,key:1}) : ''
  51. if(time !== null){
  52. clearTimeout(time);
  53. }
  54. time = setTimeout(() => {
  55. fn ? fn() : ''
  56. },500)
  57. } else {
  58. message.error({content:res.data + ':' + res.msg,key:1})
  59. }
  60. },
  61. // 金额格式化
  62. formatAmount(amount, decimalDigits = 0) {
  63. const { hiddenSensitiveFields } = useAuthStore()
  64. if (hiddenSensitiveFields) return '****'
  65. let amt = math.format(amount, {notation: 'fixed',precision: 2})
  66. const amountStr = String(amt)
  67. const reg = /\B(?=(?:\d{3})+$)/g
  68. // 是否是小数
  69. const isDecimal = amountStr.indexOf('.') > -1
  70. if (isDecimal) {
  71. // 整数部分
  72. const integerPart = amountStr.substring(0, amountStr.indexOf('.'))
  73. // 小数部分
  74. const decimalPart = amountStr.substring(amountStr.length, amountStr.indexOf('.'))
  75. return `${integerPart.replace(reg, ',')}${decimalPart}`
  76. } else {
  77. return amountStr.replace(reg, ',')
  78. }
  79. },
  80. // 验证数字输入框手动输入的值是否合法
  81. validateInputNumber (start,value,step) {
  82. return new Promise((reslove,reject)=>{
  83. if ((value * 100 - start * 100) % (step * 100) === 0) {
  84. reslove(value)
  85. } else {
  86. message.error(value?`输入的值${value}不符合规则,已修正!`:`输入的值不能为空!`);
  87. let val = value - ((value - start) % step)
  88. reslove(val)
  89. }
  90. })
  91. },
  92. // 设置状态颜色
  93. statusAndColor(status) {
  94. let statusArr = [
  95. {st:'新建',cl:"#1890ff"},
  96. {st:'提交',cl:'#646cff'},
  97. {st:'审核',cl:'#ff5656'},
  98. {st:'关闭',cl:'#acbdc5'},
  99. {st:'预提交',cl:'#005792'},
  100. {st:'确认',cl:'#01352c'},
  101. {st:'复核',cl:'#ff9234'},
  102. {st:'启用',cl:"#1890ff"},
  103. {st:'停用',cl:'#acbdc5'},
  104. ]
  105. if (statusArr.find(e=>e.st == status)) {
  106. return statusArr.find(e=>e.st == status).cl
  107. } else {
  108. return '#333'
  109. }
  110. },
  111. }