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. export default {
  10. // 检查权限是否存在
  11. hasPermission (permission) {
  12. let hasPermission = true
  13. let appData = JSON.parse(sessionStorage.getItem('app'))
  14. let auth = appData.meta.auth
  15. hasPermission = auth.some(item=>item.option == permission)
  16. return hasPermission
  17. },
  18. // 验证按钮状态
  19. isDisabled (status,arr,fn) {
  20. let rs = false
  21. if (fn) return fn
  22. rs = arr.some(item=>item == status)
  23. return rs
  24. },
  25. // 数据消息提醒
  26. message (res,msg,fn) {
  27. if (res.code === 1) {
  28. msg ? message.success(msg) : ''
  29. fn ? fn() : ''
  30. } else {
  31. message.error(res.data + ':' + res.msg);
  32. }
  33. },
  34. // 金额格式化
  35. formatAmount(amount, decimalDigits = 0) {
  36. amount = Number(amount)
  37. const { hiddenSensitiveFields } = useAuthStore()
  38. if (hiddenSensitiveFields) return '****'
  39. let amt = math.format(amount, {notation: 'fixed',precision: 2})
  40. const amountStr = String(amt)
  41. const reg = /\B(?=(?:\d{3})+$)/g
  42. // 是否是小数
  43. const isDecimal = amountStr.indexOf('.') > -1
  44. if (isDecimal) {
  45. // 整数部分
  46. const integerPart = amountStr.substring(0, amountStr.indexOf('.'))
  47. // 小数部分
  48. const decimalPart = amountStr.substring(amountStr.length, amountStr.indexOf('.'))
  49. return `${integerPart.replace(reg, ',')}${decimalPart}`
  50. } else {
  51. return amountStr.replace(reg, ',')
  52. }
  53. },
  54. // 验证数字输入框手动输入的值是否合法
  55. validateInputNumber (start,value,step) {
  56. return new Promise((reslove,reject)=>{
  57. if ((value * 100 - start * 100) % (step * 100) === 0) {
  58. reslove(value)
  59. } else {
  60. message.error(value?`输入的值${value}不符合规则,已修正!`:`输入的值不能为空!`);
  61. let val = value - ((value - start) % step)
  62. reslove(val)
  63. }
  64. })
  65. },
  66. // 设置状态颜色
  67. statusAndColor(status) {
  68. let statusArr = [
  69. {st:'新建',cl:"#1890ff"},
  70. {st:'提交',cl:'#646cff'},
  71. {st:'审核',cl:'#ff5656'},
  72. {st:'关闭',cl:'#acbdc5'},
  73. {st:'预提交',cl:'#005792'},
  74. {st:'确认',cl:'#01352c'},
  75. {st:'复核',cl:'#ff9234'},
  76. {st:'发布',cl:"#646cff"},
  77. {st:'启用',cl:"#1890ff"},
  78. {st:'停用',cl:'#acbdc5'},
  79. ]
  80. if (statusArr.find(e=>e.st == status)) {
  81. return statusArr.find(e=>e.st == status).cl
  82. } else {
  83. return '#333'
  84. }
  85. },
  86. // 获取app信息
  87. getAppData (appName,allsystem) {
  88. function findAppByName(systems, targetName) {
  89. let foundApp = null;
  90. function search(systems) {
  91. for (let i = 0; i < systems.length; i++) {
  92. const system = systems[i];
  93. if (system.modules && system.modules.length > 0) {
  94. search(system.modules); // 递归调用搜索函数
  95. }
  96. if (system.apps) {
  97. const app = system.apps.find((app) => app.name === targetName);
  98. if (app) {
  99. foundApp = app;
  100. break; // 找到目标 app 后停止搜索
  101. }
  102. }
  103. }
  104. }
  105. search(systems);
  106. return foundApp;
  107. }
  108. return findAppByName(allsystem,appName)
  109. }
  110. }