Bang Hu
2025-09-03 a4188f41f3bc981fe8fa92c253a1195b76be04dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * @Description:
 * @Version: 2.0
 * @Autor: wuyun
 * @Date: 2022-08-05 15:17:36
 * @LastEditors: wuyun
 * @LastEditTime: 2023-02-27 10:16:24
 */
// 公用方法
import { Menu } from '@element-plus/icons-vue'
import _ from 'lodash'
// import { useUserInfo } from '@/stores/userInfo'
 
// // 获取用户token
// export function getAdminToken() {
//   const info = useUserInfo()
//   return info.token
// }
// // 删除用户token
// export function removeAdminToken() {
//   const info = useUserInfo()
//   info.removeToken()
// }
 
// /**
//  * @param {string} path
//  * @returns {Boolean}
//  */
export function isExternal(path: string) {
  const isExternal = /^(https?:|http?:|mailto:|tel:)/.test(path)
  return isExternal
}
/**
 * 获取本地静态图片
 * @param name // 文件名 如 404.svg
 * @returns {*|string}
 */
export function getAssetsImages(name: string) {
  return new URL(`/src/assets/images/${name}`, import.meta.url).href
}
 
/**
 * 防抖
 * @param fn 执行函数
 * @param ms 间隔毫秒数
 */
// export const debounce = (fn: Function, ms: number) => {
//   return (...args: any[]) => {
//     if (window.lazy) {
//       clearTimeout(window.lazy)
//     }
//     window.lazy = setTimeout(() => {
//       fn(...args)
//     }, ms)
//   }
// }
 
/**
 * 格式化接口返回的路由配置json或者树形菜单json
 * @param treeList {Object[]} 接口返回的元原始数组
 * @param ancestorVal {string} 最大一级菜单对应的parentId或者说是祖先节点的父节点标识
 * @param parentId {string} 父级id对应的字段名称
 * @return {Array} 整合后的树形结构
 * 返回值示例 [ {id:'',name: '', children: []} ...]  具体字段名称根据接口实际返回值
 */
export function formatTree(
  treeList: Menu[] = [],
  ancestorVal: string = 'null',
  parentId: string = 'parentId',
  selfCode: string = 'id'
) {
  treeList.map((element: any) => {
    // 由于后端接口中存在null为祖父级,''为祖父级的混乱现象所以需要统一处理为null
    if (element[parentId] == '') {
      element[parentId] = null
    }
    return element
  })
  let treeObj = _.groupBy(treeList, parentId)
  Object.keys(treeObj).forEach((key: any) => {
    treeList.forEach((element: any) => {
      if (element[selfCode] == key) {
        element.children = _.orderBy(treeObj[key], ['sort'], ['asc'])
      }
    })
  })
 
  let result: Menu[] = []
  if (treeObj[ancestorVal]) {
    result = treeObj[ancestorVal]
  } else {
    result = treeList
  }
 
  return result
}
// 数组对象 通过键名进行分组生成map
export function getGroupByKey(arr: [], key: string) {
  let result = []
  result = arr.reduce((r, a) => {
    r[a[key]] = r[a[key]] || []
    r[a[key]].push(a)
    return r
  }, Object.create(null))
  return result
}
 
// 产品页面根据dataSource的值跳转不同的查询和修改 新增 1 是旧  的其他的是新的 
export function returnProductDetailPath(dataSource: string,type:string) {
  let pathStr=''
  if(dataSource!='1'){  
    //新路由
    pathStr='/safety/performance/meritRatingNewDetail'
  }else{
    // 旧路由
    if(type=='edit'){
      pathStr='/safety/performance/meritRatingDetail' 
    }
    else if(type=='view'){
      pathStr='/safety/performance/meritRatingView'
    }
  }
  return  pathStr
}
 
export function numberToUpperCase(textIndex:number){
    let newString = '';
    let newTextIndex:any = (textIndex + 1) + '';
    function sum(value:number, index:number) {
      var newValue = '';
      if ((textIndex === 9)) {
        return !index ? '十' : '';
      }
     let isSeat = (~~textIndex > 9 && ~~textIndex < 19);
      switch (~~value) {
        case 1:
          newValue = !index ? (isSeat ? '' : '一') : '十一';
          break;
        case 2:
          newValue = !index ? (isSeat ? '' : '二') : '十二';
          break;
        case 3:
          newValue = !index ? (isSeat ? '' : '三') : '十三';
          break;
        case 4:
          newValue = !index ? (isSeat ? '' : '四') : '十四';
          break;
        case 5:
          newValue = !index ? (isSeat ? '' : '五') : '十五';
          break;
        case 6:
          newValue = !index ? (isSeat ? '' : '六') : '十六';
          break;
        case 7:
          newValue = !index ? (isSeat ? '' : '七') : '十七';
          break;
        case 8:
          newValue = !index ? (isSeat ? '' : '八') : '十八';
          break;
        case 9:
          newValue = !index ? (isSeat ? '' : '九') : '十九';
          break;
        case 0:
          newValue = '十';
          break;
        default:
          break;
      }
      return newValue;
    }
 
    for (let i = 0; i < newTextIndex.length; i++) {
      newString += sum(newTextIndex.substring(i, i + 1), i);
    }
    return newString;
  }