tool.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { Notification } from 'element-ui';
  2. import router from '@/router';
  3. export default {
  4. // 获取应用权限
  5. checkAuth (appname,auth) {
  6. // console.log(appname)
  7. try {
  8. let modules = JSON.parse(sessionStorage.getItem('active_modules'))
  9. let apps = modules.apps
  10. // 获取当前应用数据
  11. let active_apps = apps.filter(item => {
  12. return item.name === appname
  13. })
  14. // 获取当前应用权限
  15. let auth_list = active_apps[0].meta.auth
  16. // 判断是否拥有权限
  17. let _haveAuth = auth_list.some(item=>item.option === auth)
  18. return _haveAuth
  19. } catch (error) {
  20. // console.log(error)
  21. // router.replace('/home')
  22. }
  23. },
  24. // 获取应用表格
  25. tabelCol (appname) {
  26. // 获取应用数据
  27. let apps = JSON.parse(sessionStorage.getItem('active_modules'))
  28. let module_list = apps.apps
  29. // 获取当前应用数据
  30. let active_modules = module_list.filter(item => {
  31. return item.name === appname
  32. })
  33. // 获取当前应用表格数据
  34. let tablecols = active_modules[0].meta.tables
  35. return tablecols
  36. },
  37. // 操作响应提示
  38. showMessage (res,success) {
  39. if (res.code === 0) return Notification({
  40. title: '失败',
  41. message: res.msg,
  42. type: 'error'
  43. });
  44. Notification({
  45. title: '成功',
  46. message: '操作成功',
  47. type: 'success'
  48. });
  49. success?success():''
  50. },
  51. // 处理省市县数据结构
  52. createMenu (node) {
  53. var that = this
  54. let obj = Object.keys(node).map((key,index,item)=>{
  55. var elNode = {
  56. label: key,
  57. value: key,
  58. item:node[key],
  59. }
  60. return elNode;
  61. })
  62. obj.forEach(e=>{
  63. if ((e.item) instanceof Array) {
  64. e.children = []
  65. e.item.forEach(c=>{
  66. e.children.push({
  67. label:c,
  68. value:c
  69. })
  70. })
  71. } else {
  72. if (Object.keys(e.item).length !== 0) {
  73. e.children = that.createMenu(e.item)
  74. }
  75. }
  76. })
  77. return obj
  78. },
  79. // 导出excel
  80. exportExcel(a,b,c,d) {
  81. let formatJson = function(filterVal, jsonData) {
  82. return jsonData.map(v => filterVal.map(j => v[j]))
  83. }
  84. require.ensure([], () => {
  85. const { export_json_to_excel } = require('./excel/Export2Excel');
  86. const tHeader = a; // 设置Excel的表格第一行的标题
  87. const filterVal = b; // index、nickName、name是tableData里对象的属性
  88. const list = c; //把data里的tableData存到list
  89. const data = formatJson(filterVal, list);
  90. export_json_to_excel(tHeader, data, d); //导出Excel 文件名
  91. })
  92. },
  93. // 数组去重
  94. unique(arr, uniId){
  95. const res = new Map();
  96. return arr.filter((item) => !res.has(item[uniId]) && res.set(item[uniId], 1));
  97. },
  98. // 金额格式化
  99. formatAmount(amount, decimalDigits = 0) {
  100. const amountStr = String(Number(amount).toFixed(decimalDigits))
  101. const reg = /\B(?=(?:\d{3})+$)/g
  102. // 是否是小数
  103. const isDecimal = amountStr.indexOf('.') > -1
  104. if (isDecimal) {
  105. // 整数部分
  106. const integerPart = amountStr.substring(0, amountStr.indexOf('.'))
  107. // 小数部分
  108. const decimalPart = amountStr.substring(amountStr.length, amountStr.indexOf('.'))
  109. return `${integerPart.replace(reg, ',')}${decimalPart}`
  110. } else {
  111. return amountStr.replace(reg, ',')
  112. }
  113. }
  114. }