tool.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. import { Message } from 'element-ui';
  2. import store from "../store/index";
  3. import router from '@/router';
  4. import api from '@/api/api';
  5. import { set } from 'nprogress';
  6. let timer = null,
  7. _uids = [];
  8. export default {
  9. // 获取应用权限
  10. checkAuth (appname,auth) {
  11. // await this.sleep(3000)
  12. let _haveAuth = false
  13. let app = JSON.parse(sessionStorage.getItem('activeApp'))
  14. // 获取当前应用权限
  15. let auth_list = app.meta.auth
  16. // 判断是否拥有权限
  17. _haveAuth = auth_list.some(item=>item.option === auth)
  18. return _haveAuth
  19. },
  20. // 获取应用表格
  21. tabelCol (appname) {
  22. try {
  23. // 获取应用数据
  24. let apps = JSON.parse(sessionStorage.getItem('active_modules'))
  25. let module_list = apps.apps
  26. // 获取当前应用数据
  27. let active_modules = module_list.filter(item => {
  28. return item.name === appname
  29. })
  30. // 获取当前应用表格数据
  31. let tablecols = active_modules[0].meta.tables
  32. return tablecols
  33. } catch (error) {
  34. console.log(error,'tool')
  35. }
  36. },
  37. // 操作响应提示
  38. showMessage (res,success) {
  39. let message = '操作成功'
  40. try {
  41. message = res.msg || '操作成功'
  42. } catch (error) {
  43. }
  44. try {
  45. message = store.state._this.$t(message)
  46. } catch (error) {
  47. }
  48. if (res.code === 0) return Message({
  49. message,
  50. type: 'error',
  51. duration:3000,
  52. showClose:true
  53. });
  54. Message({
  55. message,
  56. type: 'success',
  57. duration:3000,
  58. showClose:true
  59. });
  60. success?success():''
  61. },
  62. /* 获取基本地址 */
  63. getBaseUrl () {
  64. // if (process.env.NODE_ENV == 'development') return 'https://oms.idcgroup.com.cn:8079'
  65. // if (process.env.NODE_ENV == 'development') return '/apis'
  66. let href = window.location.href
  67. let index = href.indexOf('/')
  68. let num = 0
  69. while (index != -1) {
  70. num++
  71. index = href.indexOf('/',index+1)
  72. if (num++ == 3) {
  73. return href.slice(0,index)
  74. }
  75. }
  76. },
  77. // 处理省市县数据结构
  78. createMenu (node) {
  79. var that = this
  80. let obj = Object.keys(node).map((key,index,item)=>{
  81. var elNode = {
  82. label: key,
  83. value: key,
  84. item:node[key],
  85. }
  86. return elNode;
  87. })
  88. obj.forEach(e=>{
  89. if ((e.item) instanceof Array) {
  90. e.children = []
  91. e.item.forEach(c=>{
  92. e.children.push({
  93. label:c,
  94. value:c
  95. })
  96. })
  97. } else {
  98. if (Object.keys(e.item).length !== 0) {
  99. e.children = that.createMenu(e.item)
  100. }
  101. }
  102. })
  103. return obj
  104. },
  105. // 导出excel
  106. exportExcel(a,b,c,d) {
  107. let formatJson = function(filterVal, jsonData) {
  108. return jsonData.map(v => filterVal.map(j => v[j]))
  109. }
  110. require.ensure([], () => {
  111. const { export_json_to_excel } = require('./excel/Export2Excel');
  112. const tHeader = a; // 设置Excel的表格第一行的标题
  113. const filterVal = b; // index、nickName、name是tableData里对象的属性
  114. const list = c; //把data里的tableData存到list
  115. const data = formatJson(filterVal, list);
  116. export_json_to_excel(tHeader, data, d); //导出Excel 文件名
  117. })
  118. },
  119. // 数组去重
  120. unique(arr, uniId){
  121. const res = new Map();
  122. return arr.filter((item) => !res.has(item[uniId]) && res.set(item[uniId], 1));
  123. },
  124. //国际化金融单位转换 10000 转换为 1000 100000000 转化为 100000000
  125. unitConversion(amount, dividend) {
  126. let lang = localStorage.getItem('lang') || 'ZH';
  127. if (lang != 'ZH') {
  128. if (dividend == 10000) {
  129. dividend = 1000
  130. } else if (dividend == 100000000) {
  131. dividend = 1000000000
  132. }
  133. }
  134. return amount / dividend
  135. },
  136. //非中文环境下,返回空
  137. onlyZh(value) {
  138. let lang = localStorage.getItem('lang') || 'ZH';
  139. return lang == 'ZH' ? value : ''
  140. },
  141. // 金额格式化
  142. formatAmount(amount, decimalDigits, unit='') {
  143. // const amountStr = String(Number(amount).toFixed(decimalDigits))
  144. let a = new Intl.NumberFormat(undefined, { minimumFractionDigits: decimalDigits, maximumFractionDigits: decimalDigits }).format(amount)
  145. const amountStr = String(a)
  146. const reg = /\B(?=(?:\d{3})+$)/g
  147. // 是否是小数
  148. const isDecimal = amountStr.indexOf('.') > -1
  149. if (isDecimal) {
  150. // 整数部分
  151. const integerPart = amountStr.substring(0, amountStr.indexOf('.'))
  152. // 小数部分
  153. const decimalPart = amountStr.substring(amountStr.length, amountStr.indexOf('.'))
  154. return unit + `${integerPart.replace(reg, ',')}${decimalPart}`
  155. } else {
  156. return unit + amountStr.replace(reg, ',')
  157. }
  158. },
  159. /* 给指定组件触发$emit事件 */
  160. dispatchFun(componentName, eventName, vm) {
  161. var parent = vm.$parent || vm.$root;
  162. var name = parent.$options.componentName;
  163. while (parent && (!name || name !== componentName)) {
  164. parent = parent.$parent;
  165. if (parent) {
  166. name = parent.$options.componentName;
  167. }
  168. }
  169. if (parent) {
  170. parent.$emit.apply(parent, [eventName]);
  171. }
  172. },
  173. // 数组深度拷贝
  174. deepClone(obj) {
  175. let res = null;
  176. const reference = [Date, RegExp, Set, WeakSet, Map, WeakMap, Error];
  177. if (reference.includes(obj?.constructor)) {
  178. res = new obj.constructor(obj);
  179. } else if (Array.isArray(obj)) {
  180. res = [];
  181. obj.forEach((e, i) => {
  182. res[i] = this.deepClone(e);
  183. });
  184. } else if (typeof obj === "object" && obj !== null) {
  185. res = {};
  186. for (const key in obj) {
  187. if (Object.hasOwnProperty.call(obj, key)) {
  188. res[key] = this.deepClone(obj[key]);
  189. }
  190. }
  191. } else {
  192. res = obj;
  193. }
  194. return res;
  195. },
  196. // 数组排序
  197. sortArr (data,str) {
  198. let cache = {} // cache存储的键是eid,值是这个eid在indices数组中的下标
  199. let indices = [] // 数组中的每一个值是一个数组,数组中的每一个元素是原数组中相同eid的下标
  200. data.forEach((item, i) => {
  201. let eid = item[str]
  202. let index = cache[eid]
  203. if (index !== undefined) {
  204. indices[index].push(i)
  205. } else {
  206. cache[eid] = indices.length
  207. indices.push([i])
  208. }
  209. })
  210. let result = []
  211. indices.forEach(item => {
  212. item.forEach(index => {
  213. result.push(data[index]) // 依次把index对应的元素data[index]添加进去即可
  214. })
  215. })
  216. return result
  217. },
  218. calculatedColumnWidth(dom, layout) {
  219. if (!dom || _uids.includes(dom._uid)) return;
  220. _uids.push(dom._uid)
  221. let lang = localStorage.getItem('lang') || 'ZH';
  222. if (lang!='ZH') setTimeout(() => {
  223. try {
  224. const ths = dom.$el.querySelector('.el-table__header-wrapper').querySelectorAll('table thead tr:first-child th'),
  225. nodes = [];
  226. for (var key of ths.keys()) {
  227. const node = ths[key].querySelector('.cell')
  228. if (node && layout.some(v => v.title == node.innerText)) nodes.push(node);
  229. }
  230. layout.forEach((v,i) => {
  231. v.width = v.width > nodes[i].offsetWidth ? v.width : nodes[i].offsetWidth
  232. })
  233. _uids = _uids.filter(v => v != _uids)
  234. } catch (error) {}
  235. });
  236. return true
  237. },
  238. getStatusColor(status,isDetail = false){
  239. const colors = store.state.statusColorList;
  240. // console.log("colors", colors)
  241. if (isDetail){
  242. return colors[status] || "#999";
  243. }else{
  244. return colors[status] ? `color:${colors[status]}` : "#999";
  245. }
  246. },
  247. /*日期转化*/
  248. getDataChange(data){
  249. const newData = new Date(data)
  250. const year = newData.getFullYear()
  251. const month = String(newData.getMonth() + 1).padStart(2,'0')
  252. const date = String(newData.getDate()).padStart(2,'0')
  253. return year + '-' + month +'-' + date
  254. },
  255. /*查重项目格式化*/
  256. getCheckFontProject(data){
  257. data.map(item => {
  258. item.chars = item.chars.reduce((acc,ite) => ({
  259. ...acc,
  260. ...ite
  261. }),{})
  262. item.projectname = item.projectname.split('').map(text => {
  263. let projectname = item.chars.projectname.join("") || ''
  264. return projectname.includes(text) ? {
  265. text,
  266. color: 'red'
  267. } : {
  268. text,
  269. color: '#666'
  270. }
  271. })
  272. return item
  273. })
  274. },
  275. /*查重客户格式化*/
  276. getCheckFontEnterprisename(data){
  277. data.map(item => {
  278. item.chars = item.chars.reduce((acc,ite) => ({
  279. ...acc,
  280. ...ite
  281. }),{})
  282. item.enterprisename = item.enterprisename.split('').map(text => {
  283. let enterprisename = item.chars.enterprisename.join("") || ''
  284. return enterprisename.includes(text) ? {
  285. text,
  286. color: 'red'
  287. } : {
  288. text,
  289. color: '#666'
  290. }
  291. })
  292. return item
  293. })
  294. },
  295. /*公称压力格式化*/
  296. nominalPressureSet(val){
  297. let newPressure = ""
  298. if (val){
  299. val.forEach((item,index)=>{
  300. if (index == 0){
  301. newPressure = item
  302. }else {
  303. newPressure = newPressure + '/' + item
  304. // let str1 = item.substring(0,2)
  305. // let str2 = val[index -1].substring(0,2)
  306. // let str3 = item.substring(2)
  307. // if (str1 == str2){
  308. // if (newPressure == val[index -1]){
  309. // newPressure = val[index -1] + '/' + str3
  310. // }else {
  311. // newPressure = newPressure + '/' + str3
  312. // }
  313. // }else {
  314. //
  315. // }
  316. }
  317. })
  318. }else {
  319. newPressure = val
  320. }
  321. return newPressure && newPressure.length > 0 && newPressure != null?newPressure:'--'
  322. },
  323. /*数组数据展示*/
  324. ArrDataShow(data){
  325. let newArr = ''
  326. data.forEach((item,index)=>{
  327. if (index == data.length -1 || index == 0){
  328. newArr = newArr + item
  329. }else {
  330. newArr = newArr + ',' + item
  331. }
  332. })
  333. return newArr
  334. }
  335. }