tj
2025-06-05 bba272999cc546f65781bf3d20245a3f819af67f
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { cloneDeep, throttle, debounce } from 'lodash'
 
/* eslint-disable */
const Utils = {}
 
/** 参数说明:
 * 根据长度截取先使用字符串,超长部分追加…
 * str 对象字符串
 * len 目标字节长度
 * 返回值: 处理结果字符串
 */
Utils.cutString = (str, len) => {
  if (str.length * 2 <= len) {
    return str
  }
  let strlen = 0
  let s = ''
  for (let i = 0; i < str.length; i++) {
    // eslint-disable-line
    s += str.charAt(i)
    if (str.charCodeAt(i) > 128) {
      strlen += 2
      if (strlen >= len) {
        return `${s.substring(0, s.length - 1)}...`
      }
    } else {
      strlen += 1
      if (strlen >= len) {
        return `${s.substring(0, s.length - 2)}...`
      }
    }
  }
  return s
}
 
/**
 * 简单数组的交集
 * @param {Array} a
 * @param {Array} b
 */
Utils.getIntersect = (a, b) => {
  if (a.constructor === Array && b.constructor === Array) {
    const set1 = new Set(a)
    const set2 = new Set(b)
    return Array.from(new Set([...set1].filter(x => set2.has(x))))
  }
  return null
}
 
/**
 * 防抖函数
 * @param {*} func 函数体
 * @param {*} wait 延时
 */
Utils.debounce = (func, wait = 50) => debounce(func, wait)
 
/**
 * 节流函数
 * @param {*} func 函数体
 * @param {*} wait 延时
 */
Utils.throttle = (func, wait = 50) => throttle(func, wait)
 
/**
 * 返回 n 位的随机字符串
 * @param {Number} n
 */
Utils.getRandomStr = (n = 6) => {
  let str = ''
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'
  for (let i = 0; i < n; i += 1) {
    str += chars.charAt(Math.floor(Math.random() * 62))
  }
  return str
}
 
function getTypeOf(obj) {
  const { toString } = Object.prototype
  const map = {
    '[object Boolean]': 'boolean',
    '[object Number]': 'number',
    '[object String]': 'string',
    '[object Function]': 'function',
    '[object Array]': 'array',
    '[object Date]': 'date',
    '[object RegExp]': 'regExp',
    '[object Undefined]': 'undefined',
    '[object Null]': 'null',
    '[object Object]': 'object',
    '[object Symbol]': 'symbol',
  }
  return map[toString.call(obj)]
}
 
function groupByOrder(source) {
  // 有order的放这里
  const map = {}
  // 没有order放这里
  const noOrderList = []
 
  source.forEach(s => {
    const { order } = s
    if (typeof order !== 'number') {
      noOrderList.push(s)
      return
    }
 
    const list = map[order]
    if (list) {
      list.push(s)
    } else {
      map[order] = [s]
    }
  })
 
  return {
    orderMap: map,
    noOrderList,
  }
}
 
/**
 * 根据数组的 order 字段排序
 * @param {Array} source
 */
Utils.sortByOrder = (source = []) => {
  if (!Array.isArray(source)) {
    const message = 'sortByOrder 传入参数不符合要求, 应为数组'
    console.error(message)
    throw new Error(message)
  }
 
  if (!source.length) {
    return source
  }
 
  // 1.根据order对数据进行分组
  const { orderMap, noOrderList } = groupByOrder(source)
 
  // 2.获取已存在的order
  const orders = Object.keys(orderMap).map(o => Number(o))
 
  // 对order进行排序
  orders.sort((a, b) => a - b)
 
  // 小于0的order
  const ltZeroOrders = orders.filter(o => o < 0)
 
  // 大于等于0的order
  const gteZeroOrders = orders.filter(o => o >= 0)
 
  const finallyArr = []
  const gteZeroItemList = gteZeroOrders.map(o => orderMap[o]).flat()
 
  finallyArr.push(...gteZeroItemList)
  finallyArr.push(...noOrderList)
 
  // 如果没有小于0的order,则直接拼接
  if (!ltZeroOrders.length) {
    return finallyArr
  }
 
  // 将小于0的order的item插入到数组中
  ltZeroOrders.reverse().forEach(o => {
    let index = finallyArr.length + o + 1
    if (index < 0) {
      index = 0
    }
 
    const arr = orderMap[o]
    finallyArr.splice(index, 0, ...arr)
  })
 
  return finallyArr
}
 
/**
 * 深度遍历,深拷贝
 * @param {*} data
 */
Utils.deepClone = data => cloneDeep(data)
 
/**
 * 中划线转驼峰
 */
Utils.came = str => {
  return `${str}`.replace(/-\D/g, match => match.charAt(1).toUpperCase())
}
 
/**
 * 判断权限
 */
Utils.hasPermission = (permissions, route, user) => {
  // eslint-disable-line
  if (user?.admin) {
    return true
  }
  if (route.permission) {
    return permissions.some(permission => route.permission.indexOf(permission) > -1)
  }
  return true
}
 
let cached
/**
 * 获取窗口滚动条大小, From: https://github.com/react-component/util/blob/master/src/getScrollBarSize.js
 * @param {boolean} fresh 强制重新计算
 * @returns {number}
 */
export function getScrollBarSize(fresh) {
  if (fresh || cached === undefined) {
    const inner = document.createElement('div')
    inner.style.width = '100%'
    inner.style.height = '200px'
 
    const outer = document.createElement('div')
    const outerStyle = outer.style
 
    outerStyle.position = 'absolute'
    outerStyle.top = 0
    outerStyle.left = 0
    outerStyle.pointerEvents = 'none'
    outerStyle.visibility = 'hidden'
    outerStyle.width = '200px'
    outerStyle.height = '150px'
    outerStyle.overflow = 'hidden'
 
    outer.appendChild(inner)
 
    document.body.appendChild(outer)
 
    const widthContained = inner.offsetWidth
    outer.style.overflow = 'scroll'
    let widthScroll = inner.offsetWidth
 
    if (widthContained === widthScroll) {
      widthScroll = outer.clientWidth
    }
 
    document.body.removeChild(outer)
 
    cached = widthContained - widthScroll
  }
  return cached
}
 
export default Utils