tool.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { Message } from 'element-ui';
  2. import router from '@/router';
  3. export default {
  4. // 获取应用权限
  5. checkAuth (appname,auth) {
  6. // console.log(appname)
  7. try {
  8. let _haveAuth = true
  9. let modules = JSON.parse(sessionStorage.getItem('active_modules'))
  10. let apps = modules.apps
  11. // 获取当前应用数据
  12. let active_apps = apps.filter(item => {
  13. return item.name === appname
  14. })
  15. // 获取当前应用权限
  16. let auth_list = active_apps[0].meta.auth
  17. // 判断是否拥有权限
  18. _haveAuth = auth_list.some(item=>item.option === auth)
  19. return _haveAuth
  20. } catch (error) {
  21. }
  22. },
  23. // 获取应用表格
  24. tabelCol (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 Message({
  43. message: res.msg,
  44. type: 'error',
  45. });
  46. Message({
  47. message: '操作成功',
  48. type: 'success'
  49. });
  50. success?success():''
  51. },
  52. /* 获取基本地址 */
  53. getBaseUrl () {
  54. if (process.env.NODE_ENV == 'development') return 'https://oms.idcgroup.com.cn:8079'
  55. let href = window.location.href
  56. let index = href.indexOf('/')
  57. let num = 0
  58. while (index != -1) {
  59. num++
  60. index = href.indexOf('/',index+1)
  61. if (num++ == 3) {
  62. return href.slice(0,index)
  63. }
  64. }
  65. },
  66. // 处理省市县数据结构
  67. createMenu (node) {
  68. var that = this
  69. let obj = Object.keys(node).map((key,index,item)=>{
  70. var elNode = {
  71. label: key,
  72. value: key,
  73. item:node[key],
  74. }
  75. return elNode;
  76. })
  77. obj.forEach(e=>{
  78. if ((e.item) instanceof Array) {
  79. e.children = []
  80. e.item.forEach(c=>{
  81. e.children.push({
  82. label:c,
  83. value:c
  84. })
  85. })
  86. } else {
  87. if (Object.keys(e.item).length !== 0) {
  88. e.children = that.createMenu(e.item)
  89. }
  90. }
  91. })
  92. return obj
  93. },
  94. // 导出excel
  95. exportExcel(a,b,c,d) {
  96. let formatJson = function(filterVal, jsonData) {
  97. return jsonData.map(v => filterVal.map(j => v[j]))
  98. }
  99. require.ensure([], () => {
  100. const { export_json_to_excel } = require('./excel/Export2Excel');
  101. const tHeader = a; // 设置Excel的表格第一行的标题
  102. const filterVal = b; // index、nickName、name是tableData里对象的属性
  103. const list = c; //把data里的tableData存到list
  104. const data = formatJson(filterVal, list);
  105. export_json_to_excel(tHeader, data, d); //导出Excel 文件名
  106. })
  107. },
  108. // 数组去重
  109. unique(arr, uniId){
  110. const res = new Map();
  111. return arr.filter((item) => !res.has(item[uniId]) && res.set(item[uniId], 1));
  112. },
  113. // 金额格式化
  114. formatAmount(amount, decimalDigits = 0) {
  115. const amountStr = String(Number(amount).toFixed(decimalDigits))
  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 `${integerPart.replace(reg, ',')}${decimalPart}`
  125. } else {
  126. return 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. }