zxl
3 小时以前 172933f098017bc4c4f57dcda0d490ea12bb13bb
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/** 公共方法类 */
import { getToken } from "@/utils/auth";
import config from "@/config/config";
 
/**
 * 人性化时间显示
 *
 * @param {Object} datetime
 */
export function formatTime (datetime) {
  if (datetime == null) return "";
 
  datetime = datetime.replace(/-/g, "/");
 
  let time = new Date();
  let outTime = new Date(datetime);
  if (/^[1-9]\d*$/.test(datetime)) {
    outTime = new Date(parseInt(datetime) * 1000);
  }
 
  if (
    time.getTime() < outTime.getTime() ||
    time.getFullYear() != outTime.getFullYear()
  ) {
    return parseTime(outTime, "{y}-{m}-{d} {h}:{i}");
  }
 
  if (time.getMonth() != outTime.getMonth()) {
    return parseTime(outTime, "{m}-{d} {h}:{i}");
  }
 
  if (time.getDate() != outTime.getDate()) {
    let day = outTime.getDate() - time.getDate();
    if (day == -1) {
      return parseTime(outTime, "昨天 {h}:{i}");
    }
 
    if (day == -2) {
      return parseTime(outTime, "前天 {h}:{i}");
    }
 
    return parseTime(outTime, "{m}-{d} {h}:{i}");
  }
 
  if (time.getHours() != outTime.getHours()) {
    return parseTime(outTime, "{h}:{i}");
  }
 
  let minutes = outTime.getMinutes() - time.getMinutes();
  if (minutes == 0) {
    return "刚刚";
  }
 
  minutes = Math.abs(minutes);
  return `${minutes}分钟前`;
}
 
/**
 * 格式化文件大小
 *
 * @param {String} value 文件大小(字节)
 */
export function formatSize (value) {
  if (null == value || value == "") {
    return "0";
  }
  let unitArr = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
  let index = 0;
  let srcsize = parseFloat(value);
  index = Math.floor(Math.log(srcsize) / Math.log(1024));
  let size = srcsize / Math.pow(1024, index);
  size = size.toFixed(2); //保留的小数位数
  return size + unitArr[index];
}
/**
 * 获取文件后缀名
 *
 * @param {String} fileName
 */
export function getFileExt (fileName) {
  let ext = fileName.split(".");
  ext = ext[ext.length - 1]; // 获取文件后缀名
  return ext;
}
 
/**
 * 根据图片url下载图片
 * @param {String} imgsrc
 * @param {String} name
 */
export function downloadIamge (imgsrc, name) {
  //下载图片地址和图片名
  let image = new Image();
  // 解决跨域 Canvas 污染问题
  image.setAttribute("crossOrigin", "anonymous");
  image.onload = function () {
    let canvas = document.createElement("canvas");
    canvas.width = image.width;
    canvas.height = image.height;
    let context = canvas.getContext("2d");
    context.drawImage(image, 0, 0, image.width, image.height);
    let url = canvas.toDataURL("image/png"); //得到图片的base64编码数据
    let a = document.createElement("a"); // 生成一个a元素
    let event = new MouseEvent("click"); // 创建一个单击事件
    a.download = name || "photo"; // 设置图片名称
    a.href = url; // 将生成的URL设置为a.href属性
    a.dispatchEvent(event); // 触发a的单击事件
  };
  image.src = imgsrc;
}
 
/**
 * 通过图片url获取图片大小
 *
 * @param {String} imgsrc 例如图片名: D8x5f13a53dbc4b9_350x345.png
 */
export function getImageInfo (imgsrc) {
  let data = {
    width: 0,
    height: 0,
  };
 
  let arr = imgsrc.split("_");
  if (arr.length == 1) return data;
 
  let info = arr[arr.length - 1].match(/\d+x\d+/g);
  if (info == null) return data;
 
  info = info[0].split("x");
  return {
    width: parseInt(info[0]),
    height: parseInt(info[1]),
  };
}
 
/**
 * 文件下载方法
 *
 * @param {Number} cr_id
 */
export function download (cr_id) {
  let api = config.BASE_API_URL;
  let token = getToken();
  try {
    let link = document.createElement("a");
    link.href = `${api}/download/user-chat-file?cr_id=${cr_id}&token=${token}`;
    link.click();
  } catch (e) { }
}
 
/**
 * 时间格式化方法
 *
 * @param {(Object|string|number)} time
 * @param {String} cFormat
 * @returns {String | null}
 */
export function parseTime (time, cFormat) {
  if (arguments.length === 0) {
    return null;
  }
 
  let date;
  const format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}";
 
  if (typeof time === "object") {
    date = time;
  } else {
    if (typeof time === "string" && /^[0-9]+$/.test(time)) {
      time = parseInt(time);
    }
    if (typeof time === "number" && time.toString().length === 10) {
      time = time * 1000;
      console.log("时间判断为number");
    }
 
    date = new Date(time.replace(/-/g, "/"));
  }
 
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay(),
  };
 
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key];
    // Note: getDay() returns 0 on Sunday
    if (key === "a") {
      return ["日", "一", "二", "三", "四", "五", "六"][value];
    }
 
    return value.toString().padStart(2, "0");
  });
 
  return time_str;
}
 
/**
 * 去除字符串控制
 *
 * @param {String} str
 */
export function trim (str, type = null) {
  if (type) {
    return str.replace(/(^\s*)|(\s*$)/g, "");
  } else if (type == "l") {
    return str.replace(/(^\s*)/g, "");
  } else {
    return str.replace(/(\s*$)/g, "");
  }
}
 
/**
 * 解析url中参数
 *
 * @param {String} url
 * @returns {Object}
 */
export function param2Obj (url) {
  const search = url.split("?")[1];
 
  if (!search) return {};
 
  return JSON.parse(
    '{"' +
    decodeURIComponent(search)
      .replace(/"/g, '\\"')
      .replace(/&/g, '","')
      .replace(/=/g, '":"')
      .replace(/\+/g, " ") +
    '"}'
  );
}
 
/**
 * @param {Object} json
 * @returns {Array}
 */
export function param (json) {
  if (!json) return "";
  return cleanArray(
    Object.keys(json).map((key) => {
      if (json[key] === undefined) return "";
 
      return encodeURIComponent(key) + "=" + encodeURIComponent(json[key]);
    })
  ).join("&");
}
 
/**
 * @param {Array} actual
 * @returns {Array}
 */
export function cleanArray (actual) {
  const newArray = [];
  for (let i = 0; i < actual.length; i++) {
    if (actual[i]) {
      newArray.push(actual[i]);
    }
  }
 
  return newArray;
}
 
/**
 * @param {HTMLElement} element
 * @param {String} className
 */
export function toggleClass (element, className) {
  if (!element || !className) {
    return;
  }
 
  let classString = element.className;
  let nameIndex = classString.indexOf(className);
  if (nameIndex === -1) {
    classString += "" + className;
  } else {
    classString =
      classString.substr(0, nameIndex) +
      classString.substr(nameIndex + className.length);
  }
  element.className = classString;
}
 
/**
 * Check if an element has a class
 *
 * @param {HTMLElement} elm
 * @param {String} cls
 * @returns {Boolean}
 */
export function hasClass (ele, cls) {
  return !!ele.className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"));
}
 
/**
 * Add class to element
 *
 * @param {HTMLElement} elm
 * @param {String} cls
 */
export function addClass (ele, cls) {
  if (!hasClass(ele, cls)) ele.className += " " + cls;
}
 
/**
 * Remove class from element
 *
 * @param {HTMLElement} elm
 * @param {String} cls
 */
export function removeClass (ele, cls) {
  if (hasClass(ele, cls)) {
    const reg = new RegExp("(\\s|^)" + cls + "(\\s|$)");
    ele.className = ele.className.replace(reg, " ");
  }
}
 
/**
 * 通过图片Url获取图片等比例缩放的宽度和高度信息
 *
 * @param {String} src
 * @param {Number} width
 */
export function imgZoom (src, width = 200) {
  const info = getImageInfo(src);
 
  if (info.width < width) {
    return {
      width: `${info.width}px`,
      height: `${info.height}px`,
    };
  }
 
  return {
    width: width + "px",
    height: parseInt(info.height / (info.width / width)) + "px",
  };
}
 
/**
 * 获取浏览器光标选中内容
 *
 * @export
 * @returns
 */
export function getSelection () {
  return window.getSelection
    ? window.getSelection().toString()
    : document.selection.createRange().text;
}
 
/**
 * 剪贴板复制功能
 *
 * @param {String} value 复制内容
 * @param {Function} callback 复制成功回调方法
 */
export const copyTextToClipboard = (value, callback) => {
  let textArea = document.createElement("textarea");
  textArea.style.background = "transparent";
  textArea.value = value;
 
  document.body.appendChild(textArea);
 
  textArea.select();
 
  try {
    document.execCommand("copy");
    if (callback) callback();
  } catch (err) {
    this.$message.error("Oops, unable to copy");
  }
 
  document.body.removeChild(textArea);
};
 
/**
 * 隐藏用户手机号中间四位
 *
 * @param {String} phone  手机号
 */
export function hidePhone (phone) {
  return phone.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
}
 
/**
 * 人性化显示时间
 *
 * @param {Object} datetime
 */
export function beautifyTime (datetime = "") {
  if (datetime == null) {
    return "";
  }
 
  datetime = datetime.replace(/-/g, "/");
 
  let time = new Date();
  let outTime = new Date(datetime);
  if (/^[1-9]\d*$/.test(datetime)) {
    outTime = new Date(parseInt(datetime) * 1000);
  }
 
  if (time.getTime() < outTime.getTime()) {
    return parseTime(outTime, "{y}/{m}/{d}");
  }
 
  if (time.getFullYear() != outTime.getFullYear()) {
    return parseTime(outTime, "{y}/{m}/{d}");
  }
 
  if (time.getMonth() != outTime.getMonth()) {
    return parseTime(outTime, "{m}/{d}");
  }
 
  if (time.getDate() != outTime.getDate()) {
    let day = outTime.getDate() - time.getDate();
    if (day == -1) {
      return parseTime(outTime, "昨天 {h}:{i}");
    }
 
    if (day == -2) {
      return parseTime(outTime, "前天 {h}:{i}");
    }
 
    return parseTime(outTime, "{m}-{d}");
  }
 
  if (time.getHours() != outTime.getHours()) {
    return parseTime(outTime, "{h}:{i}");
  }
 
  let minutes = outTime.getMinutes() - time.getMinutes();
  if (minutes == 0) {
    return "刚刚";
  }
 
  minutes = Math.abs(minutes);
  return `${minutes}分钟前`;
}
 
export function getSort (fn) {
  return function (a, b) {
    let ret = 0;
 
    if (fn.call(this, a, b)) {
      ret = -1;
    } else if (fn.call(this, b, a)) {
      ret = 1;
    }
 
    return ret;
  };
}
 
/**
 * 批量排序
 *
 * @param {*} arr
 */
export function getMutipSort (arr) {
  return function (a, b) {
    let tmp;
    let i = 0;
 
    do {
      tmp = arr[i++](a, b);
    } while (tmp == 0 && i < arr.length);
 
    return tmp;
  };
}
 
/**
 * Url 替换超链接
 *
 * @param {String} text 文本
 * @param {String} color 超链接颜色
 */
export function textReplaceLink (text, color = "#409eff") {
  let exp =
    /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi;
  return text.replace(
    exp,
    `<a href='$1' target="_blank" style="color:${color};text-decoration: revert;">$1</a >`
  );
}
 
/**
 * 防抖
 *
 * @param {*} func
 * @param {*} wait
 * @param {*} immediate
 * @returns
 */
export function debounce (func, wait, immediate) {
  let timeout;
 
  return function () {
    let context = this;
    let args = arguments;
 
    if (timeout) clearTimeout(timeout); // timeout 不为null
    if (immediate) {
      let callNow = !timeout; // 第一次会立即执行,以后只有事件执行后才会再次触发
      timeout = setTimeout(function () {
        timeout = null;
      }, wait);
      if (callNow) func.apply(context, args);
    } else {
      timeout = setTimeout(function () {
        func.apply(context, args);
      }, wait);
    }
  };
}