zhengyiming
2025-03-28 8bb9173a95a87e3ceab4f48b5b34041af38b7c70
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
import dayjs from 'dayjs';
import _ from 'lodash';
 
export function object2query(object: Record<string, string | number>) {
  return Object.keys(object)
    .map((key) => (object[key] ? `${key}=${object[key]}` : ''))
    .filter(Boolean)
    .join('&');
}
 
export function query2object<T extends { [key: string]: string }>(query: string): T {
  return Object.fromEntries(
    decodeURIComponent(query)
      .split('&')
      .map((x) => x.split('='))
  );
}
 
export function getRouterPath(path: string) {
  const pathList = path.split('?');
  return pathList[0];
}
 
export function formatTimeWithoutCurrentYear(date: string) {
  if (!date) return date;
  const targetDate = dayjs(date);
  const targetYear = targetDate.year();
  const nowYear = dayjs().year();
  return targetYear >= nowYear
    ? targetDate.format('MM-DD HH:mm')
    : targetDate.format('YYYY-MM-DD HH:mm');
}
 
//去收尾空格
export function trim(str: string) {
  return str.replace(/(^\s*)|(\s*$)/g, '');
}
 
export function paginateList<T>(list: T[], pageIndex: number, pageSize: number) {
  const startIndex = (pageIndex - 1) * pageSize;
  const endIndex = startIndex + pageSize;
  return list.slice(startIndex, endIndex);
}
 
/**
 * 添加星号
 * @param str
 * @param start
 * @param end
 * @returns
 */
export function addStarForString(str: string, start = 0, end = 0) {
  return str.substring(0, start) + '*'.repeat(end - start) + str.substring(end);
}
 
export function addStarForEmail(str: string) {
  const end = str.lastIndexOf('.');
  return addStarForString(str, 2, end);
}
 
export function formatTimeAgo(date: string, format = 'YYYY-MM-DD HH:mm') {
  const diff = dayjs().diff(date, 'seconds'); // 计算时间差,单位为秒
 
  if (diff < 60) {
    return `${diff}秒前`;
  } else if (diff < 60 * 60) {
    const minutes = Math.floor(diff / 60);
    return `${minutes}分钟前`;
  } else if (diff < 60 * 60 * 24) {
    const hours = Math.floor(diff / (60 * 60));
    return `${hours}小时前`;
  } else if (dayjs(date).isSame(dayjs(), 'day')) {
    return dayjs(date).format('HH:mm');
  } else {
    // const days = Math.floor(diff / (60 * 60 * 24));
    // return `${days}天前`;
    return formatTimeWithoutCurrentYear(date); //dayjs(date).format(format);
  }
}
 
export function toRound(val: number, num = 2) {
  if (val >= 0) {
    return _.round(val, num);
  } else {
    const abs = Math.abs(val);
    return 0 - _.round(abs, num);
  }
}
 
export function toThousand(input: string | number, toFixed = 2) {
  const num = toRound(Number(input)).toFixed(toFixed);
  if (Number(num) >= 0) {
    return num.toString().replace(/(^|\s)\d+/g, (m) => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','));
  } else {
    const posNum = (0 - Number(num)).toFixed(2);
    return (
      '-' + posNum.toString().replace(/(^|\s)\d+/g, (m) => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
    );
  }
}
 
export function format(date: string | Date, fmt = 'YYYY-MM-DD') {
  return date ? dayjs(date).format(fmt) : '';
}
 
export function formatMaxNumber(count: number, max = 9999) {
  return count > max ? `${max}+` : count;
}
 
export function guid() {
  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);
  });
}
 
export function delay(timeout = 500) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(1);
    }, timeout);
  });
}
 
export function filterJoin(list: any[], separator = '-') {
  return list.filter(Boolean).join(separator);
}
 
export const hiddenIDNumberForEnd6 = (realIDNumber: string) =>
  realIDNumber.replace(/^(\d+)(.{6})$/, '$1******');
 
export const hiddenPhoneNumber = (realPhoneNumbe: string) =>
  realPhoneNumbe.replace(/^(.{3})(?:\d+)(.{4})$/, '$1****$2');