zhengyiming
2 天以前 80dc90be027ee26869c63860b7d6a0759a03546b
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
import dayjs from 'dayjs';
import _ from 'lodash';
import { LifeRechargeConstants } from '@life-payment/core-vue';
 
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 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 addStarForPhone(str: string) {
  return `*${str.substring(str.length - 4)}`;
}
 
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 function orderStatusEnum(
  payStatus?: LifeRechargeConstants.LifePayStatusEnum,
  lifePayOrderStatus?: LifeRechargeConstants.LifePayOrderStatusEnum
) {
  if (
    (lifePayOrderStatus === LifeRechargeConstants.LifePayOrderStatusEnum.充值中 &&
      payStatus === LifeRechargeConstants.LifePayStatusEnum.已支付) ||
    payStatus === LifeRechargeConstants.LifePayStatusEnum.待退款
  ) {
    return '支付成功';
  }
  if (payStatus === LifeRechargeConstants.LifePayStatusEnum.已退款) {
    return '已退款';
  }
  if (
    lifePayOrderStatus === LifeRechargeConstants.LifePayOrderStatusEnum.已完成 &&
    payStatus === LifeRechargeConstants.LifePayStatusEnum.已支付
  ) {
    return '充值成功';
  }
  return '';
}
 
export function convertOrderFrontStatus(
  payStatus?: LifeRechargeConstants.LifePayStatusEnum,
  lifePayOrderStatus?: LifeRechargeConstants.LifePayOrderStatusEnum,
  lifePayRefundStatus?: LifeRechargeConstants.LifePayRefundStatusEnum
) {
  // if (
  //   (lifePayOrderStatus === LifeRechargeConstants.LifePayOrderStatusEnum.充值中 &&
  //     payStatus === LifeRechargeConstants.LifePayStatusEnum.已支付) ||
  //   payStatus === LifeRechargeConstants.LifePayStatusEnum.待退款
  // ) {
  //   return LifeRechargeConstants.LifePayOrderFrontStatusEnum.支付成功;
  // }
  if (
    lifePayOrderStatus === LifeRechargeConstants.LifePayOrderStatusEnum.已退款 ||
    payStatus === LifeRechargeConstants.LifePayStatusEnum.已退款
  ) {
    return LifeRechargeConstants.LifePayOrderFrontStatusEnum.已退款;
  }
  if (lifePayOrderStatus === LifeRechargeConstants.LifePayOrderStatusEnum.待退款) {
    return LifeRechargeConstants.LifePayOrderFrontStatusEnum.退款待审核;
  }
  if (
    lifePayOrderStatus === LifeRechargeConstants.LifePayOrderStatusEnum.退款失败 ||
    lifePayRefundStatus === LifeRechargeConstants.LifePayRefundStatusEnum.退款驳回
  ) {
    return LifeRechargeConstants.LifePayOrderFrontStatusEnum.退款失败;
  }
  if (
    lifePayOrderStatus === LifeRechargeConstants.LifePayOrderStatusEnum.已完成 &&
    payStatus === LifeRechargeConstants.LifePayStatusEnum.已支付
  ) {
    return LifeRechargeConstants.LifePayOrderFrontStatusEnum.充值成功;
  }
  return LifeRechargeConstants.LifePayOrderFrontStatusEnum.支付成功;
}
 
export class StringUtils {
  static insertSpaces(str: string, space = 4) {
    if (!str) return '';
    const regex = new RegExp(`(.{${space}})`, 'g');
    return str.replace(regex, '$1 ');
  }
 
  static societyCreditCodeInsertSpaces(str: string) {
    if (!str) return '';
    return str.replace(/(.{4})(.{4})(.{4})(.{6})/g, '$1 $2 $3 $4');
  }
 
  static idNumberInsertSpaces(str: string) {
    if (!str) return '';
    return str.replace(/(.{3})(.{3})(.{4})(.{4})(.{4})/g, '$1 $2 $3 $4 $5');
  }
 
  static phoneNumberAddSpace(realPhoneNumber: string) {
    if (!realPhoneNumber) return '';
    return realPhoneNumber.replace(/^(\d{3})(\d*)(\d{4})$/, '$1 $2 $3');
  }
 
  static formatterNumber(str: string) {
    const cleanedValue = str.replace(/[^\d.]/g, '');
    const singleDotValue = cleanedValue.replace(/(\..*)\./g, '$1');
    const numberValue = parseFloat(singleDotValue);
    return isNaN(numberValue) ? '' : singleDotValue;
  }
}