tool.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { Message } from 'element-ui';
  2. import router from '@/router';
  3. import api from '@/api/api';
  4. export default {
  5. // 获取应用权限
  6. checkAuth (appname,auth) {
  7. // await this.sleep(3000)
  8. let _haveAuth = false
  9. let app = JSON.parse(sessionStorage.getItem('activeApp'))
  10. // 获取当前应用权限
  11. let auth_list = app.meta.auth
  12. // 判断是否拥有权限
  13. _haveAuth = auth_list.some(item=>item.option === auth)
  14. return _haveAuth
  15. },
  16. // 获取应用表格
  17. tabelCol (appname) {
  18. try {
  19. // 获取应用数据
  20. let apps = JSON.parse(sessionStorage.getItem('active_modules'))
  21. let module_list = apps.apps
  22. // 获取当前应用数据
  23. let active_modules = module_list.filter(item => {
  24. return item.name === appname
  25. })
  26. // 获取当前应用表格数据
  27. let tablecols = active_modules[0].meta.tables
  28. return tablecols
  29. } catch (error) {
  30. console.log(error,'tool')
  31. }
  32. },
  33. // 操作响应提示
  34. showMessage (res,success) {
  35. if (res.code === 0) return Message({
  36. message: res.msg,
  37. type: 'error',
  38. duration:3000,
  39. showClose:true
  40. });
  41. Message({
  42. message: '操作成功',
  43. type: 'success',
  44. duration:3000,
  45. showClose:true
  46. });
  47. success?success():''
  48. },
  49. /* 获取基本地址 */
  50. getBaseUrl () {
  51. // if (process.env.NODE_ENV == 'development') return 'https://oms.idcgroup.com.cn:8079'
  52. // if (process.env.NODE_ENV == 'development') return '/apis'
  53. let href = window.location.href
  54. let index = href.indexOf('/')
  55. let num = 0
  56. while (index != -1) {
  57. num++
  58. index = href.indexOf('/',index+1)
  59. if (num++ == 3) {
  60. return href.slice(0,index)
  61. }
  62. }
  63. },
  64. // 处理省市县数据结构
  65. createMenu (node) {
  66. var that = this
  67. let obj = Object.keys(node).map((key,index,item)=>{
  68. var elNode = {
  69. label: key,
  70. value: key,
  71. item:node[key],
  72. }
  73. return elNode;
  74. })
  75. obj.forEach(e=>{
  76. if ((e.item) instanceof Array) {
  77. e.children = []
  78. e.item.forEach(c=>{
  79. e.children.push({
  80. label:c,
  81. value:c
  82. })
  83. })
  84. } else {
  85. if (Object.keys(e.item).length !== 0) {
  86. e.children = that.createMenu(e.item)
  87. }
  88. }
  89. })
  90. return obj
  91. },
  92. // 导出excel
  93. exportExcel(a,b,c,d) {
  94. let formatJson = function(filterVal, jsonData) {
  95. return jsonData.map(v => filterVal.map(j => v[j]))
  96. }
  97. require.ensure([], () => {
  98. const { export_json_to_excel } = require('./excel/Export2Excel');
  99. const tHeader = a; // 设置Excel的表格第一行的标题
  100. const filterVal = b; // index、nickName、name是tableData里对象的属性
  101. const list = c; //把data里的tableData存到list
  102. const data = formatJson(filterVal, list);
  103. export_json_to_excel(tHeader, data, d); //导出Excel 文件名
  104. })
  105. },
  106. // 数组去重
  107. unique(arr, uniId){
  108. const res = new Map();
  109. return arr.filter((item) => !res.has(item[uniId]) && res.set(item[uniId], 1));
  110. },
  111. // 金额格式化
  112. formatAmount(amount, decimalDigits, unit='') {
  113. // const amountStr = String(Number(amount).toFixed(decimalDigits))
  114. let a = new Intl.NumberFormat(undefined, { minimumFractionDigits: decimalDigits, maximumFractionDigits: decimalDigits }).format(amount)
  115. const amountStr = String(a)
  116. const reg = /\B(?=(?:\d{3})+$)/g
  117. // 是否是小数
  118. const isDecimal = amountStr.indexOf('.') > -1
  119. if (isDecimal) {
  120. // 整数部分
  121. const integerPart = amountStr.substring(0, amountStr.indexOf('.'))
  122. // 小数部分
  123. const decimalPart = amountStr.substring(amountStr.length, amountStr.indexOf('.'))
  124. return unit + `${integerPart.replace(reg, ',')}${decimalPart}`
  125. } else {
  126. return unit + amountStr.replace(reg, ',')
  127. }
  128. },
  129. /* 给指定组件触发$emit事件 */
  130. dispatchFun(componentName, eventName, vm) {
  131. var parent = vm.$parent || vm.$root;
  132. var name = parent.$options.componentName;
  133. while (parent && (!name || name !== componentName)) {
  134. parent = parent.$parent;
  135. if (parent) {
  136. name = parent.$options.componentName;
  137. }
  138. }
  139. if (parent) {
  140. parent.$emit.apply(parent, [eventName]);
  141. }
  142. },
  143. // 数组深度拷贝
  144. deepClone(obj) {
  145. let res = null;
  146. const reference = [Date, RegExp, Set, WeakSet, Map, WeakMap, Error];
  147. if (reference.includes(obj?.constructor)) {
  148. res = new obj.constructor(obj);
  149. } else if (Array.isArray(obj)) {
  150. res = [];
  151. obj.forEach((e, i) => {
  152. res[i] = this.deepClone(e);
  153. });
  154. } else if (typeof obj === "object" && obj !== null) {
  155. res = {};
  156. for (const key in obj) {
  157. if (Object.hasOwnProperty.call(obj, key)) {
  158. res[key] = this.deepClone(obj[key]);
  159. }
  160. }
  161. } else {
  162. res = obj;
  163. }
  164. return res;
  165. },
  166. // 数组排序
  167. sortArr (data,str) {
  168. let cache = {} // cache存储的键是eid,值是这个eid在indices数组中的下标
  169. let indices = [] // 数组中的每一个值是一个数组,数组中的每一个元素是原数组中相同eid的下标
  170. data.forEach((item, i) => {
  171. let eid = item[str]
  172. let index = cache[eid]
  173. if (index !== undefined) {
  174. indices[index].push(i)
  175. } else {
  176. cache[eid] = indices.length
  177. indices.push([i])
  178. }
  179. })
  180. let result = []
  181. indices.forEach(item => {
  182. item.forEach(index => {
  183. result.push(data[index]) // 依次把index对应的元素data[index]添加进去即可
  184. })
  185. })
  186. return result
  187. }
  188. }