123
gaoshp
34 分钟以前 4c2a27b9d7604287c4f94c88a84a1f10a44ad39c
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/**
 * 通用工具类
 */
export default class func {
  /**
   * 不为空
   * @param val
   * @returns {boolean}
   */
  static notEmpty(val) {
    return !this.isEmpty(val);
  }
 
  /**
   * 是否为定义
   * @param val
   * @returns {boolean}
   */
  static isUndefined(val) {
    return val === null || typeof val === 'undefined';
  }
 
  /**
   * 为空
   * @param val
   * @returns {boolean}
   */
  static isEmpty(val) {
    if (
      val === null ||
      typeof val === 'undefined' ||
      (typeof val === 'string' && val === '' && val !== 'undefined')
    ) {
      return true;
    }
    return false;
  }
 
  /**
   * 强转int型
   * @param val
   * @param defaultValue
   * @returns {number}
   */
  static toInt(val, defaultValue) {
    if (this.isEmpty(val)) {
      return defaultValue === undefined ? -1 : defaultValue;
    }
    const num = parseInt(val, 0);
    return Number.isNaN(num) ? (defaultValue === undefined ? -1 : defaultValue) : num;
  }
 
  /**
   * 转为数字型(转换失败则返回原值)
   * @param val
   */
  static toNumber(val) {
    if (this.isEmpty(val)) {
      return '';
    }
    const num = parseFloat(val);
    return Number.isNaN(num) ? val : num;
  }
 
  /**
   * Json强转为Form类型
   * @param obj
   * @returns {FormData}
   */
  static toFormData(obj) {
    const data = new FormData();
    Object.keys(obj).forEach(key => {
      data.append(key, Array.isArray(obj[key]) ? obj[key].join(',') : obj[key]);
    });
    return data;
  }
 
  /**
   * date类转为字符串格式
   * @param date
   * @param format
   * @returns {null}
   */
  static format(date, format = 'YYYY-MM-DD HH:mm:ss') {
    return date ? date.format(format) : null;
  }
 
  /**
   * data类格式化
   * @param timestamp
   * @returns {string}
   */
  static formatDateTime(timestamp) {
    return this.formatDate(new Date(timestamp));
  }
 
  /**
   * data类格式化
   * @param date
   * @returns {string}
   */
  static formatDate(date) {
    const pad = num => (num < 10 ? '0' + num : num);
 
    const year = date.getFullYear();
    const month = pad(date.getMonth() + 1); // 月份从0开始,所以+1
    const day = pad(date.getDate());
    const hour = pad(date.getHours());
    const minute = pad(date.getMinutes());
    const second = pad(date.getSeconds());
 
    return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
  }
 
  /**
   * 格式化时区解决时间差问题
   * @param datetime
   * @returns {string}
   */
  static toLocalISOString(datetime) {
    let timezoneOffset = datetime.getTimezoneOffset() * 60000; // 获取当前时区与UTC的时间差(以毫秒为单位)
    let localDatetime = new Date(datetime - timezoneOffset); // 调整时间,得到当前时区时间
    return localDatetime.toISOString();
  }
 
  /**
   * 根据逗号联合
   * @param arr
   * @returns {string}
   */
  static join(arr) {
    return Array.isArray(arr) ? arr.join(',') : arr;
  }
 
  /**
   * 根据逗号分隔
   * @param str
   * @returns {string}
   */
  static split(str) {
    return str ? String(str).split(',') : '';
  }
 
  /**
   * 转换空字符串
   * @param str
   * @returns {string|*}
   */
  static toStr(str) {
    if (typeof str === 'undefined' || str === null) {
      return '';
    }
    return str;
  }
 
  /**
   * 判断是否为数组
   * @param param
   * @returns {boolean}
   */
  static isArrayAndNotEmpty(param) {
    return Array.isArray(param) && param.length > 0;
  }
 
  /**
   * 格式化URL
   * @param url
   * @returns {*|string}
   */
  static formatUrl(url) {
    if (!url) return url;
    if (url.startsWith('http://') || url.startsWith('https://')) {
      return url;
    } else {
      return `http://${url}`;
    }
  }
 
  /**
   * bytes转换为kb单位
   * @param bytes
   * @returns {string}
   */
  static bytesToKB(bytes) {
    const kb = bytes / 1024;
    return kb.toFixed(2);
  }
 
  /**
   * json数组转换成key value字符串
   * @param jsonArray "[{enumKey: 'key', enumValue: 'value'}]"
   * @returns {*}
   */
  static jsonArrayToKeyValue(jsonArray) {
    if (this.isEmpty(jsonArray)) {
      return '';
    }
    return jsonArray.map(item => `${item.enumKey}:${item.enumValue}`).join(';');
  }
 
  /**
   * key value字符串转换成json数组
   * @param keyValue key:value;key:value
   * @returns {*[]}
   */
  static keyValueToJsonArray(keyValue) {
    if (this.isEmpty(keyValue)) {
      return [];
    }
    return keyValue.split(';').map((kv, index) => {
      const [key, value] = kv.split(':');
      return {
        id: index,
        enumKey: key,
        enumValue: value,
      };
    });
  }
 
  /**
   * 检查字符串str中是否包含子字符串val
   * @param {string} str 要检查的字符串
   * @param {string} val 要查找的子字符串
   * @return {boolean} 如果str包含val则返回true,否则返回false
   */
  static contains(str, val) {
    // 检查str是否为字符串且不为空
    if (typeof str === 'string' && str.length > 0) {
      return str.includes(val);
    }
    return false;
  }
 
  /**
   * 截取字符串
   * @param str 字符串
   * @param len 截取长度
   * @returns {*|string}
   */
  static truncateString(str, len = 20) {
    if (str.length > len) {
      return str.slice(0, len) + '...';
    }
    return str;
  }
 
  /**
   * 驼峰转下划线
   * @param str
   * @returns {*}
   */
  static camelCaseString(str) {
    return str.replace(/_([a-z])/g, g => g[1].toUpperCase());
  }
 
  /**
   * 生成随机字符串
   * @param length 长度
   * @returns {string}
   */
  static strGenerate(length) {
    const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    const maxLength = 256;
    if (length > maxLength) {
      throw new Error(`长度最大值不能超过 ${maxLength}`);
    }
 
    return Array.from({ length }, () =>
      characters.charAt(Math.floor(Math.random() * characters.length))
    ).join('');
  }
 
  /**
   * 生成UUID
   * @returns {string}
   */
  static generateUUID() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
      const r = (Math.random() * 16) | 0,
        v = c === 'x' ? r : (r & 0x3) | 0x8;
      return v.toString(16);
    });
  }
 
  /**
   * 过滤空对象
   * @param obj
   * @returns {Object}
   */
  static filterEmptyObject(obj) {
    return Object.fromEntries(
      Object.entries(obj).filter(
        ([, value]) => value !== '' && value !== null && value !== undefined
      )
    );
  }
 
  /**
   * 获取用户租户ID
   * @param userInfo
   * @returns {string}
   */
  static getUserTenantId(userInfo) {
    if (userInfo && userInfo.tenant_id) {
      return userInfo.tenant_id;
    } else if (userInfo && userInfo.tenantId) {
      return userInfo.tenantId;
    }
  }
}