jinbi-utils 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +19 -0
- package/.cz-config.js +55 -0
- package/.dockerignore +3 -0
- package/.editorconfig +12 -0
- package/.eslintignore +8 -0
- package/.eslintrc.js +54 -0
- package/Dockerfile +3 -0
- package/README.md +160 -0
- package/api-extractor.json +15 -0
- package/commitlint.config.js +3 -0
- package/dist/index.esm.js +1277 -0
- package/dist/index.esm.min.js +15 -0
- package/dist/index.umd.js +1348 -0
- package/dist/index.umd.min.js +16 -0
- package/docs/assets/images/icons.png +0 -0
- package/docs/assets/images/icons@2x.png +0 -0
- package/docs/assets/images/widgets.png +0 -0
- package/docs/assets/images/widgets@2x.png +0 -0
- package/docs/assets/js/main.js +1 -0
- package/docs/assets/js/search.json +1 -0
- package/docs/globals.html +144 -0
- package/docs/index.html +147 -0
- package/docs/interfaces/file.compressimgqualitycallback.html +212 -0
- package/docs/interfaces/file.compressimgscalecallback.html +206 -0
- package/docs/interfaces/file.exportbyblobparams.html +294 -0
- package/docs/interfaces/file.filesizeobject.html +227 -0
- package/docs/interfaces/file.genexportbyblobparams.html +237 -0
- package/docs/modules/common.html +188 -0
- package/docs/modules/date.html +364 -0
- package/docs/modules/file.html +452 -0
- package/docs/modules/number.html +356 -0
- package/docs/modules/object.html +245 -0
- package/docs/modules/print.html +183 -0
- package/docs/modules/string.html +352 -0
- package/docs/modules/validate.html +389 -0
- package/jest.config.js +15 -0
- package/package.json +76 -0
- package/rollup.config.js +65 -0
- package/src/common/index.ts +323 -0
- package/src/constant/common.constant.ts +13 -0
- package/src/date/index.ts +143 -0
- package/src/file/index.ts +296 -0
- package/src/http/http.ts +79 -0
- package/src/http/httpEnums.ts +61 -0
- package/src/index.ts +10 -0
- package/src/number/index.ts +190 -0
- package/src/object/index.ts +54 -0
- package/src/print/index.ts +102 -0
- package/src/string/index.ts +111 -0
- package/src/validate/index.ts +78 -0
- package/src/wecom/wecom.ts +75 -0
- package/test/common/index.test.ts +19 -0
- package/test/date/index.test.ts +107 -0
- package/test/file/index.test.ts +104 -0
- package/test/number/index.test.ts +108 -0
- package/test/object/index.test.ts +20 -0
- package/test/string/index.test.ts +82 -0
- package/tsconfig.json +39 -0
- package/typedoc.json +9 -0
- package/types/common/index.d.ts +47 -0
- package/types/constant/common.constant.d.ts +12 -0
- package/types/date/index.d.ts +60 -0
- package/types/file/index.d.ts +96 -0
- package/types/http/http.d.ts +17 -0
- package/types/http/httpEnums.d.ts +53 -0
- package/types/index.d.ts +10 -0
- package/types/number/index.d.ts +62 -0
- package/types/object/index.d.ts +25 -0
- package/types/print/index.d.ts +11 -0
- package/types/string/index.d.ts +53 -0
- package/types/validate/index.d.ts +45 -0
- package/types/wecom/wecom.d.ts +3 -0
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 通用函数
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
* @module Common
|
|
5
|
+
* @preferred
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import {fromTypeMap, IFromType} from "@/constant/common.constant";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 判断val 是否是空值
|
|
12
|
+
#### 使用说明
|
|
13
|
+
```
|
|
14
|
+
isEmpty('') 返回 true
|
|
15
|
+
isEmpty(null) 返回 true
|
|
16
|
+
isEmpty(undefined) 返回 true
|
|
17
|
+
isEmpty(12323) 返回 false
|
|
18
|
+
```
|
|
19
|
+
*/
|
|
20
|
+
export function isEmpty(val: any): boolean {
|
|
21
|
+
return val === null || val === '' || val === undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// from jinbizhihui
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
// 获取环境变量
|
|
28
|
+
// export const getEnvValue = (type: string) => {
|
|
29
|
+
// return import.meta.env[type]
|
|
30
|
+
// }
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
export const clearLoginData = () => {
|
|
35
|
+
// 这里做指定的 key 清除 是为了 避免 清除掉 jsapiticket 等一些需要有的字段 防止多次请求
|
|
36
|
+
localStorage.removeItem('wecom_userinfo')
|
|
37
|
+
localStorage.removeItem('wecom_token')
|
|
38
|
+
localStorage.removeItem('projectActive')
|
|
39
|
+
localStorage.removeItem('loginTime')
|
|
40
|
+
localStorage.removeItem('projectInfo')
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// 获取url中参数
|
|
44
|
+
export const getQueryString = (url: string, queryKey: string) => {
|
|
45
|
+
const reg = new RegExp(`&{1}${queryKey}\\=[a-zA-Z0-9_-]+`, 'g')
|
|
46
|
+
const matchResult = url.replace(/\?/g, '&').match(reg)![0]
|
|
47
|
+
return matchResult.substr(matchResult.indexOf('=') + 1)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export const getQueryVariable = (variable) => {
|
|
51
|
+
var query = window.location.search.substring(1)
|
|
52
|
+
var vars = query.split('&')
|
|
53
|
+
for (var i = 0; i < vars.length; i++) {
|
|
54
|
+
var pair = vars[i].split('=')
|
|
55
|
+
if (pair[0] === variable) { return pair[1] }
|
|
56
|
+
}
|
|
57
|
+
return (false)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export const getWecomToken = () => {
|
|
61
|
+
return localStorage.getItem('wecom_token')
|
|
62
|
+
}
|
|
63
|
+
// 判断设备类型
|
|
64
|
+
export const getDeviceType = (): IFromType => {
|
|
65
|
+
const ua = navigator.userAgent.toLowerCase();
|
|
66
|
+
const isWxWork = /wxwork/.test(ua); // 企业微信
|
|
67
|
+
const isWeixin = /micromessenger/.test(ua) && !isWxWork; // 微信,排除企业微信
|
|
68
|
+
|
|
69
|
+
// 使用屏幕宽度判断是否移动设备
|
|
70
|
+
const isMobileScreen = window.innerWidth <= 768;
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
isWxWork,
|
|
74
|
+
isWeixin,
|
|
75
|
+
isMobileScreen,
|
|
76
|
+
isMobileAny: isWxWork || isWeixin || isMobileScreen,
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export const getFromType = (res: IFromType) => {
|
|
81
|
+
let result = 'wecom'
|
|
82
|
+
for( let key in res) {
|
|
83
|
+
const value = res[key]
|
|
84
|
+
if (value) {
|
|
85
|
+
result = fromTypeMap[key]
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
export function randomString(e) {
|
|
92
|
+
e = e || 32
|
|
93
|
+
let t = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
|
|
94
|
+
let a = t.length
|
|
95
|
+
let n = ''
|
|
96
|
+
for (let i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a))
|
|
97
|
+
return n
|
|
98
|
+
}
|
|
99
|
+
export const getCookie = (cookieName) => {
|
|
100
|
+
let cookie = ''
|
|
101
|
+
let cookieArr = document.cookie.split('; ')
|
|
102
|
+
for (let i = 0; i < cookieArr.length; i++) {
|
|
103
|
+
var valueArr = cookieArr[i].split('=')
|
|
104
|
+
if (valueArr[0] === cookieName) {
|
|
105
|
+
cookie = valueArr[1]
|
|
106
|
+
break
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return cookie
|
|
110
|
+
}
|
|
111
|
+
// || 'cc51882c781d8035b98335ccb44787b42dd9d91ba3b85a45ff3e867857a526bfbbddcdc27b8edce9a52882d1838e0b8e6ccf658ed105df9a898b1c977c588bd1'
|
|
112
|
+
export const buildUUID = () => {
|
|
113
|
+
const hexList: string[] = []
|
|
114
|
+
for (let i = 0; i <= 15; i++) {
|
|
115
|
+
hexList[i] = i.toString(16) as string
|
|
116
|
+
}
|
|
117
|
+
let uuid = ''
|
|
118
|
+
for (let i = 1; i <= 36; i++) {
|
|
119
|
+
if (i === 9 || i === 14 || i === 19 || i === 24) {
|
|
120
|
+
uuid += '-'
|
|
121
|
+
} else if (i === 15) {
|
|
122
|
+
uuid += 4
|
|
123
|
+
} else if (i === 20) {
|
|
124
|
+
uuid += hexList[(Math.random() * 4) | 8];
|
|
125
|
+
} else {
|
|
126
|
+
uuid += hexList[(Math.random() * 16) | 0];
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return uuid.replace(/-/g, '')
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export const getTicket = () => {
|
|
133
|
+
return localStorage.getItem('jsapiTicket')
|
|
134
|
+
}
|
|
135
|
+
export const setTicket = (jsapiTicket) => {
|
|
136
|
+
return localStorage.setItem('jsapiTicket', jsapiTicket)
|
|
137
|
+
}
|
|
138
|
+
export const removeTicket = () => {
|
|
139
|
+
return localStorage.removeItem('jsapiTicket')
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
export const formateTimestamp = (timestamp: number | null, type = 'YYYY年MM月DD日 hh:mm:ss') => {
|
|
144
|
+
if (!timestamp) {
|
|
145
|
+
return '-'
|
|
146
|
+
}
|
|
147
|
+
const date = new Date(timestamp)
|
|
148
|
+
const year = date.getFullYear() + ''
|
|
149
|
+
const month = date.getMonth() + 1
|
|
150
|
+
const day = date.getDate()
|
|
151
|
+
const houre = date.getHours() > 9 ? date.getHours() : '0' + date.getHours()
|
|
152
|
+
const minute = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes()
|
|
153
|
+
const second = date.getSeconds() > 9 ? date.getSeconds() : '0' + date.getSeconds()
|
|
154
|
+
const result = type.replace('YYYY', year + '').replace('MM', month + '').replace('DD', day + '').replace('hh', houre + '').replace('mm', minute + '').replace('ss', second + '')
|
|
155
|
+
return result
|
|
156
|
+
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
export const convertBase64UrlToBlob = (urlData) => {
|
|
161
|
+
// console.log('urlData', urlData)
|
|
162
|
+
// 将以base64的图片url数据转换为Blob
|
|
163
|
+
var bytes = window.atob(urlData.split(',')[1]) // 去掉url的头,并转换为byte
|
|
164
|
+
// 处理异常,将ascii码小于0的转换为大于0
|
|
165
|
+
var ab = new ArrayBuffer(bytes.length)
|
|
166
|
+
var ia = new Uint8Array(ab)
|
|
167
|
+
for (var i = 0; i < bytes.length; i++) {
|
|
168
|
+
ia[i] = bytes.charCodeAt(i)
|
|
169
|
+
}
|
|
170
|
+
return new Blob([ab], { type: 'image/png' })
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
export const getEnvironment = () => {
|
|
175
|
+
const isMobile = window.navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i); // 是否手机端
|
|
176
|
+
const isWx = /micromessenger/i.test(navigator.userAgent); // 是否微信
|
|
177
|
+
const isComWx = /wxwork/i.test(navigator.userAgent); // 是否企业微信
|
|
178
|
+
|
|
179
|
+
if (isComWx && isMobile) { //手机端企业微信
|
|
180
|
+
return 'com-wx-mobile'
|
|
181
|
+
}
|
|
182
|
+
else if (isComWx && !isMobile) { //PC端企业微信
|
|
183
|
+
return 'com-wx-pc'
|
|
184
|
+
}
|
|
185
|
+
else if (isWx && isMobile) { // 手机端微信
|
|
186
|
+
return 'wx-mobile';
|
|
187
|
+
}
|
|
188
|
+
else if (isWx && !isMobile) { // PC端微信
|
|
189
|
+
return 'wx-pc';
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
return 'other';
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export const getIsComWx = () => {
|
|
197
|
+
const isComWx = /wxwork/i.test(navigator.userAgent); // 是否企业微信
|
|
198
|
+
return isComWx
|
|
199
|
+
}
|
|
200
|
+
export const getIsDevelopment = () => {
|
|
201
|
+
let result = false
|
|
202
|
+
if (location.origin === 'uat2-h5-wecom.hengdayun.com' || location.origin === '127.0.0.1:8081' || location.origin === 'localhost:8081') {
|
|
203
|
+
result = true
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return result
|
|
207
|
+
}
|
|
208
|
+
// 处理时间,企微不能通过dayjs以及moment处理时间
|
|
209
|
+
|
|
210
|
+
// 过滤数组重复数据
|
|
211
|
+
export const filterRepeat = (arr: any[], fieldName: string) => {
|
|
212
|
+
const set = new Set()
|
|
213
|
+
return arr.filter(item => {
|
|
214
|
+
const key = item[fieldName]
|
|
215
|
+
if (set.has(key)) {
|
|
216
|
+
return false
|
|
217
|
+
} else {
|
|
218
|
+
set.add(key)
|
|
219
|
+
return true
|
|
220
|
+
}
|
|
221
|
+
})
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// 将 base64 转换为 Blob
|
|
225
|
+
export const base64ToBlob = (base64) => {
|
|
226
|
+
var arr = base64.split(","),
|
|
227
|
+
mime = arr[0].match(/:(.\*?);/)[1],
|
|
228
|
+
bstr = atob(arr[1]),
|
|
229
|
+
n = bstr.length,
|
|
230
|
+
u8arr = new Uint8Array(n);
|
|
231
|
+
while (n--) {
|
|
232
|
+
u8arr[n] = bstr.charCodeAt(n);
|
|
233
|
+
}
|
|
234
|
+
return new Blob([u8arr], {
|
|
235
|
+
type: mime,
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
// 将 base64 转换为 File
|
|
239
|
+
export const base64ToFile = (base64, fileName) => {
|
|
240
|
+
let arr = base64.split(",");
|
|
241
|
+
let mime = arr[0].match(/:(.*);/)[1];
|
|
242
|
+
|
|
243
|
+
let bstr = atob(arr[1]);
|
|
244
|
+
let n = bstr.length;
|
|
245
|
+
let u8arr = new Uint8Array(n);
|
|
246
|
+
|
|
247
|
+
while (n--) {
|
|
248
|
+
u8arr[n] = bstr.charCodeAt(n);
|
|
249
|
+
}
|
|
250
|
+
return new File([u8arr], fileName, { type: mime });
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// 处理时间,企微不能通过dayjs以及moment处理时间
|
|
254
|
+
export const isImage = (type: string) => {
|
|
255
|
+
return /^image\//i.test(type);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// 获取文件数据
|
|
259
|
+
export const getFileData = (optionName: string) => {
|
|
260
|
+
const [name = '', url = ''] = optionName.split(',')
|
|
261
|
+
return {
|
|
262
|
+
name: decodeURIComponent(name),
|
|
263
|
+
url: decodeURIComponent(url)
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
// 验证是否为数字
|
|
267
|
+
export const validateTwoDecimal = (v: number | string) => {
|
|
268
|
+
if (!v || v === '') return true
|
|
269
|
+
return /^(\d+)(\.\d{1,2})?$/.test(v.toString())
|
|
270
|
+
}
|
|
271
|
+
// 生成uuid
|
|
272
|
+
export const uuid = (len, radix) => {
|
|
273
|
+
let chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
|
|
274
|
+
let uuid: string[] = [], i: string | number;
|
|
275
|
+
radix = radix || chars.length;
|
|
276
|
+
|
|
277
|
+
if (len) {
|
|
278
|
+
// Compact form
|
|
279
|
+
for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
|
|
280
|
+
} else {
|
|
281
|
+
// rfc4122, version 4 form
|
|
282
|
+
let r;
|
|
283
|
+
|
|
284
|
+
// rfc4122 requires these characters
|
|
285
|
+
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
|
|
286
|
+
uuid[14] = '4';
|
|
287
|
+
|
|
288
|
+
// Fill in random data. At i==19 set the high bits of clock sequence as
|
|
289
|
+
// per rfc4122, sec. 4.1.5
|
|
290
|
+
for (i = 0; i < 36; i++) {
|
|
291
|
+
if (!uuid[i]) {
|
|
292
|
+
r = 0 | Math.random() * 16;
|
|
293
|
+
uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return uuid.join('');
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export const getToken = (cacheType: string, key = 'token') => {
|
|
302
|
+
let token = ''
|
|
303
|
+
const cacheTypeToLower = cacheType.toLowerCase();
|
|
304
|
+
if (cacheTypeToLower === 'localstorage') {
|
|
305
|
+
token = localStorage.getItem('token' || key) || '';
|
|
306
|
+
} else if (cacheTypeToLower === 'sessionstorage') {
|
|
307
|
+
token = sessionStorage.getItem('token' || key) || '';
|
|
308
|
+
} else {
|
|
309
|
+
token = localStorage.getItem('token' || key) || sessionStorage.getItem('token') || '';
|
|
310
|
+
}
|
|
311
|
+
return token;
|
|
312
|
+
}
|
|
313
|
+
export const removeToken = (cacheType: string, key = 'token') => {
|
|
314
|
+
const cacheTypeToLower = cacheType.toLowerCase();
|
|
315
|
+
if (cacheTypeToLower === 'localstorage') {
|
|
316
|
+
localStorage.removeItem('token' || key);
|
|
317
|
+
} else if (cacheTypeToLower === 'sessionstorage') {
|
|
318
|
+
sessionStorage.removeItem('token' || key);
|
|
319
|
+
} else {
|
|
320
|
+
localStorage.removeItem('token' || key);
|
|
321
|
+
sessionStorage.getItem('token' || key);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// 判断设备类型
|
|
2
|
+
export interface IFromType {
|
|
3
|
+
isWxWork: boolean;
|
|
4
|
+
isWeixin: boolean;
|
|
5
|
+
isMobileScreen: boolean;
|
|
6
|
+
isMobileAny: boolean;
|
|
7
|
+
}
|
|
8
|
+
export const fromTypeMap = {
|
|
9
|
+
isWxWork: 'wecom',
|
|
10
|
+
isWeixin: 'wechat',
|
|
11
|
+
isMobile: 'mobile',
|
|
12
|
+
isMobileAny: 'mobile',
|
|
13
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 时间处理相关
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
* @module Date
|
|
5
|
+
* @preferred
|
|
6
|
+
*/
|
|
7
|
+
import { isEmpty } from '../common';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 解决ios不支持 new Date('2020-02-02')问题
|
|
11
|
+
#### 使用说明
|
|
12
|
+
```
|
|
13
|
+
compatibleDate('2020-03-04') 返回 2020/03/04
|
|
14
|
+
compatibleDate(Date.now()) 返回 Date.now()
|
|
15
|
+
compatibleDate(new Date()) 返回 new Date()
|
|
16
|
+
```
|
|
17
|
+
*/
|
|
18
|
+
export function compatibleDate(date: string | number | Date) {
|
|
19
|
+
if (typeof date === 'string') {
|
|
20
|
+
return date.replace(/-/g, '/');
|
|
21
|
+
}
|
|
22
|
+
return date;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 调用原生 new Date 返回 Date实例 解决ios不支持 yyyy-MM-dd HH:mm:ss 格式new Date
|
|
27
|
+
#### 使用说明
|
|
28
|
+
```
|
|
29
|
+
newDate() 返回 new Date()
|
|
30
|
+
newDate('2020-03-04') 返回 new Date('2020-03-04')
|
|
31
|
+
```
|
|
32
|
+
*/
|
|
33
|
+
export function newDate(date?: string | number | Date): Date {
|
|
34
|
+
if (!date) return new Date();
|
|
35
|
+
return new Date(compatibleDate(date));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 格式化时间
|
|
40
|
+
#### 使用说明
|
|
41
|
+
```
|
|
42
|
+
formatTime() 返回 yyyy-MM-dd HH:mm:ss 格式的当前时间
|
|
43
|
+
formatTime('2018-02-02 00:00:00') 返回 2018-02-02 00:00:00
|
|
44
|
+
formatTime('2018-02-02', 'yyyy/MM/dd') 返回 2018/02/02
|
|
45
|
+
formatTime('2018-02-02 23:50:50', 'yyyy/MM/dd HH:mm:ss') 返回 2018/02/02 23:50:50
|
|
46
|
+
formatTime('2018-02-02 23:50:50', 'MM/dd HH:mm:ss') 返回 02/02 23:50:50
|
|
47
|
+
formatTime(new Date('2018-02-02 23:50:50') + 200, 'yyyy/MM/dd HH:mm:ss S') 返回 2018/02/02 23:50:50 200
|
|
48
|
+
```
|
|
49
|
+
*/
|
|
50
|
+
export function formatTime(date: string | number | Date = Date.now(), fmt = 'yyyy-MM-dd HH:mm:ss') {
|
|
51
|
+
date = compatibleDate(date);
|
|
52
|
+
|
|
53
|
+
date = new Date(date);
|
|
54
|
+
const o: any = {
|
|
55
|
+
'M+': date.getMonth() + 1, // 月份
|
|
56
|
+
'd+': date.getDate(), // 日
|
|
57
|
+
'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, // 小时
|
|
58
|
+
'H+': date.getHours(), // 小时
|
|
59
|
+
'm+': date.getMinutes(), // 分
|
|
60
|
+
's+': date.getSeconds(), // 秒
|
|
61
|
+
'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
|
|
62
|
+
S: date.getMilliseconds(), // 毫秒
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
if (/(y+)/.test(fmt)) {
|
|
66
|
+
fmt = fmt.replace(RegExp.$1, (`${date.getFullYear()}`).substr(4 - RegExp.$1.length));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
Object.keys(o).forEach((k) => {
|
|
70
|
+
if (new RegExp(`(${k})`).test(fmt)) {
|
|
71
|
+
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : ((`00${o[k]}`).substr((`${o[k]}`).length)));
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
return fmt;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 格式化相对时间
|
|
80
|
+
#### 使用说明
|
|
81
|
+
```
|
|
82
|
+
fromNow('') 返回 -
|
|
83
|
+
fromNow(Date.now() - 2000) 返回 刚刚
|
|
84
|
+
fromNow(now - 3420 * 1000) 返回 58分钟前面
|
|
85
|
+
fromNow(now - 3600 * 1000 * 3) 返回 3小时前
|
|
86
|
+
fromNow(now - 3600 * 1000 * 24 * 1.5) 返回 1天前
|
|
87
|
+
fromNow(now - day5) 返回 当前时间的5天前日期 格式 yyyy年MM月dd日 HH时mm分ss秒
|
|
88
|
+
fromNow(Date.now() + 2000) 返回 Date.now() + 2000
|
|
89
|
+
fromNow('', '', 'defaultValue') 等于 defaultValue
|
|
90
|
+
```
|
|
91
|
+
*/
|
|
92
|
+
export function fromNow(time: Date | string | number, fmt: string = 'yyyy年MM月dd日 HH时mm分ss秒', defaultValue = '-') {
|
|
93
|
+
if (isEmpty(time)) {
|
|
94
|
+
return defaultValue;
|
|
95
|
+
}
|
|
96
|
+
time = compatibleDate(time);
|
|
97
|
+
const d = new Date(time);
|
|
98
|
+
const now = Date.now();
|
|
99
|
+
const diff = (now - d.getTime()) / 1000;
|
|
100
|
+
if (diff < 0) {
|
|
101
|
+
return time;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (diff < 30) {
|
|
105
|
+
return '刚刚';
|
|
106
|
+
}
|
|
107
|
+
if (diff < 3600) {
|
|
108
|
+
return `${Math.ceil(diff / 60)}分钟前`;
|
|
109
|
+
}
|
|
110
|
+
if (diff < 3600 * 24) {
|
|
111
|
+
return `${Math.floor(diff / 3600)}小时前`;
|
|
112
|
+
}
|
|
113
|
+
if (diff < 3600 * 24 * 2) {
|
|
114
|
+
return '1天前';
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return formatTime(time, fmt);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* 增加日期天数
|
|
122
|
+
#### 使用说明
|
|
123
|
+
```
|
|
124
|
+
addDays('') 返回 -
|
|
125
|
+
addDays('2018-02-02') 返回 2018-02-02
|
|
126
|
+
addDays('2018-02-02', 2) 返回 2018-02-04
|
|
127
|
+
addDays('2018-02-02', 2, 'yyyy/MM/dd') 返回 2018/02/04
|
|
128
|
+
addDays(new Date('2020-03-04'), 2, 'yyyy/MM/dd') 返回 2020/03/06
|
|
129
|
+
addDays(Date.now(), 2, 'yyyy/MM/dd') 返回 今天 + 2天
|
|
130
|
+
```
|
|
131
|
+
*/
|
|
132
|
+
export function addDays(date: Date | string | number, days: string | number = 0, fmt: string = 'yyyy-MM-dd') {
|
|
133
|
+
if (isEmpty(date)) {
|
|
134
|
+
return '-';
|
|
135
|
+
}
|
|
136
|
+
date = compatibleDate(date);
|
|
137
|
+
const d = new Date(date);
|
|
138
|
+
d.setDate(d.getDate() + Number(days));
|
|
139
|
+
|
|
140
|
+
return formatTime(d, fmt);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// 获取当前时间月份
|