ludc
2024-11-27 a55d22468017679f4503c1f4c5f595300e8d2f34
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
/**
 * 通用工具类
 */
import CryptoJS from 'crypto-js'
 
export default class func {
  /**
   * 单选表格行
   * 时间戳格式化
   */
  static formattedDate(val) {
    // 创建一个 Date 对象
    const date = new Date(val);
 
    // 格式化时间的辅助函数
    const formatNumber = (number) => String(number).padStart(2, '0');
 
    // 提取并格式化年、月、日、小时、分钟和秒
    const year = date.getFullYear();
    const month = formatNumber(date.getMonth() + 1); // 月份从0开始,需要+1
    const day = formatNumber(date.getDate());
    const hours = formatNumber(date.getHours());
    const minutes = formatNumber(date.getMinutes());
    const seconds = formatNumber(date.getSeconds());
 
    // 格式化为 YYYY-MM-DD HH:MM:SS
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  }
 
  /**
   * 单选表格行
   * row 当前行数据
   * CrudRef 表格ref绑定值
   * lastIndex 判断二次点击index是否和第一次点击一致
   * setLastIndex 更新lastIndex值
   * setSelectList 将下拉框保存数组置空
   */
 
  static rowClickHandler(row, CrudRef, lastIndex, setLastIndex, setSelectList) {
    /*if (lastIndex === row.$index) {
      setSelectList();
      CrudRef.clearSelection();
    } else {
      setSelectList();
      CrudRef.clearSelection();
      CrudRef.toggleRowSelection(row);
    }*/
    setSelectList();
    CrudRef.toggleSelection();
    CrudRef.toggleRowSelection(row);//选中当前行
    setLastIndex(row.$index);
  }
 
  /**
   * 不为空
   * @param val
   * @returns {boolean}
   * 不能判断数组和对象 如判断"null"、"undefined"会返回不为空
   */
  static notEmpty(val) {
    return !this.isEmpty(val);
  }
 
  /**
   * 是否为定义
   * @param val
   * @returns {boolean}
   */
  static isUndefined(val) {
    return val === null || typeof val === 'undefined';
  }
 
  /**
   * 为空
   * @param val
   * @returns {boolean}
   * 不能判断数组和对象 如判断"null"、"undefined"会返回不为空
   */
  static isEmpty(val) {
    if (
      val === null ||
      typeof val === 'undefined' ||
      (typeof val === 'string' && val === '' && val !== 'undefined')
    ) {
      return true;
    }
    return false;
  }
 
  /**
   * 判断对象为空
   */
 
  static isEmptyObject(obj) {
    if(obj){
      return Object.keys(obj).length === 0;
    }else {
      return true;
    }
  }
 
  /**
   * 强转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;
  }
 
  /**
   * 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;
  }
 
  /**
   * 根据逗号联合
   * @param arr
   * @returns {string}
   */
  static join(arr) {
    return arr ? arr.join(',') : '';
  }
 
  /**
   * 根据逗号分隔
   * @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 value
   * @returns {Boolean}
   */
  static isValuableObj(value) {
    return typeof value === 'object' && value !== null && Object.keys(value).length > 0;
  }
 
  /**
   * 获取流文件进行文件下
   * @param value
   * @returns {Boolean}
   */
  static downloadFileByBlobHandler(res) {
    if (res.headers['content-disposition']) {
      const fileNames = res.headers['content-disposition'].split(";");
      let characterSet = fileNames[2].split("filename*=")[1];
      let fileName = decodeURI(fileNames[1].split("filename=")[1], characterSet);  // 下载的文件名
      let blob = new Blob([res.data], {
        type: res.data.type + ";charset=utf-8",
      });
      const url = URL.createObjectURL(blob); // 生成下载链接
      const link = document.createElement("a");
      link.href = url;
      link.download = fileName; // 指定文件名和格式
      document.body.appendChild(link); // 添加下载链接到页面
      link.click(); // 触发下载
      document.body.removeChild(link); //删除下载
      window.URL.revokeObjectURL(url); //释放掉blob对象
    }
  }
 
  /**
   * 获取流文件进行文件下
   * @param value
   * @returns {Boolean}
   */
  static downloadFileByBlob(blobData, name) {
    const url = URL.createObjectURL(blobData); // 生成下载链接
    const link = document.createElement("a");
    link.href = url;
    link.download = name; // 指定文件名和格式
    document.body.appendChild(link); // 添加下载链接到页面
    link.click(); // 触发下载
    document.body.removeChild(link); //删除下载
  }
 
  /**
   * 将数组中的属性拼接成字符串
   * @param {数组} array
   * @param {属性} attr
   * @returns
   */
  static joinedString(array, attr) {
    return array.reduce((accumulator, item) => {
      return accumulator + (accumulator ? ',' : '') + item[attr];
    }, '');
  }
 
  /**
   * 比对数组中的对象的某个属性是否全部一致
   * @param {数组} array
   * @param {属性} attr
   * @returns 一致返回fasle,不一致返回true
   */
  static isAttrInconsistent(array, attr) {
    return Object.values(array).some((item, index, array) => {
      return item[attr] !== array[0][attr];
    });
  }
 
  /**
   * 3des加密
   * @param message
   * @param key
   * @returns {string}
   */
  encryptByDES(message, key) {
    const keyHex = CryptoJS.enc.Utf8.parse(key);
    const encrypted = CryptoJS.DES.encrypt(message, keyHex, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    return encrypted.toString();
  }
 
  /**
   * HmacMD5加密
   * @param message
   * @param key
   * @returns {string}
   * @constructor
   */
  static HmacMD5(message, key) {
    const encrypted = CryptoJS.HmacMD5(message, key);
    return encrypted.toString();
  }
 
  /**
   * aes加密
   * @param data
   * @param secretKey
   * @returns {string}
   */
  static encryptData(data, secretKey) {
    const key = CryptoJS.enc.Utf8.parse(secretKey);
    const iv = CryptoJS.enc.Utf8.parse(secretKey.substr(0, 16)); // AES block size is 128 bits (16 bytes)
    const encrypted = CryptoJS.AES.encrypt(data, key, {
      iv: iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7
    });
    return encrypted.toString();
  }
}