tool.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. }
  21. },
  22. // 获取应用表格
  23. tabelCol (appname) {
  24. console.log(appname)
  25. try {
  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. } catch (error) {
  37. console.log(error,'tool')
  38. }
  39. },
  40. // 操作响应提示
  41. showMessage (res,success) {
  42. if (res.code === 0) return Notification({
  43. title: '失败',
  44. message: res.msg,
  45. type: 'error',
  46. });
  47. Notification({
  48. title: '成功',
  49. message: '操作成功',
  50. type: 'success'
  51. });
  52. success?success():''
  53. },
  54. // 处理省市县数据结构
  55. createMenu (node) {
  56. var that = this
  57. let obj = Object.keys(node).map((key,index,item)=>{
  58. var elNode = {
  59. label: key,
  60. value: key,
  61. item:node[key],
  62. }
  63. return elNode;
  64. })
  65. obj.forEach(e=>{
  66. if ((e.item) instanceof Array) {
  67. e.children = []
  68. e.item.forEach(c=>{
  69. e.children.push({
  70. label:c,
  71. value:c
  72. })
  73. })
  74. } else {
  75. if (Object.keys(e.item).length !== 0) {
  76. e.children = that.createMenu(e.item)
  77. }
  78. }
  79. })
  80. return obj
  81. },
  82. // 导出excel
  83. exportExcel(a,b,c,d) {
  84. let formatJson = function(filterVal, jsonData) {
  85. return jsonData.map(v => filterVal.map(j => v[j]))
  86. }
  87. require.ensure([], () => {
  88. const { export_json_to_excel } = require('./excel/Export2Excel');
  89. const tHeader = a; // 设置Excel的表格第一行的标题
  90. const filterVal = b; // index、nickName、name是tableData里对象的属性
  91. const list = c; //把data里的tableData存到list
  92. const data = formatJson(filterVal, list);
  93. export_json_to_excel(tHeader, data, d); //导出Excel 文件名
  94. })
  95. },
  96. // 数组去重
  97. unique(arr, uniId){
  98. const res = new Map();
  99. return arr.filter((item) => !res.has(item[uniId]) && res.set(item[uniId], 1));
  100. },
  101. // 金额格式化
  102. formatAmount(amount, decimalDigits = 0) {
  103. const amountStr = String(Number(amount).toFixed(decimalDigits))
  104. const reg = /\B(?=(?:\d{3})+$)/g
  105. // 是否是小数
  106. const isDecimal = amountStr.indexOf('.') > -1
  107. if (isDecimal) {
  108. // 整数部分
  109. const integerPart = amountStr.substring(0, amountStr.indexOf('.'))
  110. // 小数部分
  111. const decimalPart = amountStr.substring(amountStr.length, amountStr.indexOf('.'))
  112. return `${integerPart.replace(reg, ',')}${decimalPart}`
  113. } else {
  114. return amountStr.replace(reg, ',')
  115. }
  116. },
  117. /* 给指定组件触发$emit事件 */
  118. dispatchFun(componentName, eventName, vm) {
  119. var parent = vm.$parent || vm.$root;
  120. var name = parent.$options.componentName;
  121. while (parent && (!name || name !== componentName)) {
  122. parent = parent.$parent;
  123. if (parent) {
  124. name = parent.$options.componentName;
  125. }
  126. }
  127. if (parent) {
  128. parent.$emit.apply(parent, [eventName]);
  129. }
  130. },
  131. // 数组深度拷贝
  132. deepClone(obj) {
  133. let res = null;
  134. const reference = [Date, RegExp, Set, WeakSet, Map, WeakMap, Error];
  135. if (reference.includes(obj?.constructor)) {
  136. res = new obj.constructor(obj);
  137. } else if (Array.isArray(obj)) {
  138. res = [];
  139. obj.forEach((e, i) => {
  140. res[i] = this.deepClone(e);
  141. });
  142. } else if (typeof obj === "object" && obj !== null) {
  143. res = {};
  144. for (const key in obj) {
  145. if (Object.hasOwnProperty.call(obj, key)) {
  146. res[key] = this.deepClone(obj[key]);
  147. }
  148. }
  149. } else {
  150. res = obj;
  151. }
  152. return res;
  153. },
  154. // 数组排序
  155. sortArr (data,str) {
  156. let cache = {} // cache存储的键是eid,值是这个eid在indices数组中的下标
  157. let indices = [] // 数组中的每一个值是一个数组,数组中的每一个元素是原数组中相同eid的下标
  158. data.forEach((item, i) => {
  159. let eid = item[str]
  160. let index = cache[eid]
  161. if (index !== undefined) {
  162. indices[index].push(i)
  163. } else {
  164. cache[eid] = indices.length
  165. indices.push([i])
  166. }
  167. })
  168. let result = []
  169. indices.forEach(item => {
  170. item.forEach(index => {
  171. result.push(data[index]) // 依次把index对应的元素data[index]添加进去即可
  172. })
  173. })
  174. return result
  175. }
  176. }