@indfnd/utils 0.0.31
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/CHANGELOG.md +247 -0
- package/dist/ind-utils.es.js +10831 -0
- package/dist/ind-utils.umd.cjs +36 -0
- package/global.d.ts +8 -0
- package/package.json +63 -0
- package/src/api/com.ts +8 -0
- package/src/api/index.ts +5 -0
- package/src/api/item.ts +20 -0
- package/src/api/platform/dict.ts +59 -0
- package/src/api/platform/index.ts +4 -0
- package/src/api/platform/menu.ts +44 -0
- package/src/api/platform/oss.ts +8 -0
- package/src/api/platform/user.ts +29 -0
- package/src/api/user.ts +8 -0
- package/src/config/base.config.ts +47 -0
- package/src/config/dev.config.ts +5 -0
- package/src/config/index.ts +11 -0
- package/src/config/prod.config.ts +5 -0
- package/src/index.ts +3 -0
- package/src/utils/cache/dict-cache.ts +11 -0
- package/src/utils/cache/index.ts +3 -0
- package/src/utils/cache/permission-cache.ts +15 -0
- package/src/utils/cache/user-cache.ts +15 -0
- package/src/utils/date.ts +57 -0
- package/src/utils/enum.ts +133 -0
- package/src/utils/event.ts +53 -0
- package/src/utils/excel.js +642 -0
- package/src/utils/half-year.ts +53 -0
- package/src/utils/index.ts +18 -0
- package/src/utils/is-type.ts +164 -0
- package/src/utils/mime-type.ts +67 -0
- package/src/utils/number.ts +144 -0
- package/src/utils/quarter.ts +58 -0
- package/src/utils/request/axios.extends.ts +9 -0
- package/src/utils/request/cache-rules.ts +19 -0
- package/src/utils/request/content-type.ts +16 -0
- package/src/utils/request/index.ts +20 -0
- package/src/utils/request/interceptors.ts +99 -0
- package/src/utils/request/url-params.ts +18 -0
- package/src/utils/sm3/index.ts +29 -0
- package/src/utils/sm3/sm3.ts +238 -0
- package/src/utils/storage.ts +51 -0
- package/src/utils/table.ts +252 -0
- package/src/utils/token.ts +57 -0
- package/src/utils/uuid.ts +10 -0
- package/src/utils/validate.ts +149 -0
- package/types/api/com.d.ts +2 -0
- package/types/api/com.d.ts.map +1 -0
- package/types/api/index.d.ts +5 -0
- package/types/api/index.d.ts.map +1 -0
- package/types/api/item.d.ts +5 -0
- package/types/api/item.d.ts.map +1 -0
- package/types/api/platform/dict.d.ts +4 -0
- package/types/api/platform/dict.d.ts.map +1 -0
- package/types/api/platform/index.d.ts +5 -0
- package/types/api/platform/index.d.ts.map +1 -0
- package/types/api/platform/menu.d.ts +17 -0
- package/types/api/platform/menu.d.ts.map +1 -0
- package/types/api/platform/oss.d.ts +2 -0
- package/types/api/platform/oss.d.ts.map +1 -0
- package/types/api/platform/user.d.ts +17 -0
- package/types/api/platform/user.d.ts.map +1 -0
- package/types/api/user.d.ts +2 -0
- package/types/api/user.d.ts.map +1 -0
- package/types/config/base.config.d.ts +31 -0
- package/types/config/base.config.d.ts.map +1 -0
- package/types/config/dev.config.d.ts +4 -0
- package/types/config/dev.config.d.ts.map +1 -0
- package/types/config/index.d.ts +61 -0
- package/types/config/index.d.ts.map +1 -0
- package/types/config/prod.config.d.ts +4 -0
- package/types/config/prod.config.d.ts.map +1 -0
- package/types/index.d.ts +4 -0
- package/types/index.d.ts.map +1 -0
- package/types/utils/cache/dict-cache.d.ts +3 -0
- package/types/utils/cache/dict-cache.d.ts.map +1 -0
- package/types/utils/cache/index.d.ts +3 -0
- package/types/utils/cache/index.d.ts.map +1 -0
- package/types/utils/cache/permission-cache.d.ts +4 -0
- package/types/utils/cache/permission-cache.d.ts.map +1 -0
- package/types/utils/cache/user-cache.d.ts +4 -0
- package/types/utils/cache/user-cache.d.ts.map +1 -0
- package/types/utils/date.d.ts +4 -0
- package/types/utils/date.d.ts.map +1 -0
- package/types/utils/enum.d.ts +90 -0
- package/types/utils/enum.d.ts.map +1 -0
- package/types/utils/event.d.ts +17 -0
- package/types/utils/event.d.ts.map +1 -0
- package/types/utils/excel.d.ts +48 -0
- package/types/utils/excel.d.ts.map +1 -0
- package/types/utils/half-year.d.ts +6 -0
- package/types/utils/half-year.d.ts.map +1 -0
- package/types/utils/index.d.ts +18 -0
- package/types/utils/index.d.ts.map +1 -0
- package/types/utils/is-type.d.ts +34 -0
- package/types/utils/is-type.d.ts.map +1 -0
- package/types/utils/mime-type.d.ts +68 -0
- package/types/utils/mime-type.d.ts.map +1 -0
- package/types/utils/number.d.ts +20 -0
- package/types/utils/number.d.ts.map +1 -0
- package/types/utils/quarter.d.ts +7 -0
- package/types/utils/quarter.d.ts.map +1 -0
- package/types/utils/request/axios.extends.d.ts +9 -0
- package/types/utils/request/axios.extends.d.ts.map +1 -0
- package/types/utils/request/cache-rules.d.ts +7 -0
- package/types/utils/request/cache-rules.d.ts.map +1 -0
- package/types/utils/request/content-type.d.ts +9 -0
- package/types/utils/request/content-type.d.ts.map +1 -0
- package/types/utils/request/index.d.ts +8 -0
- package/types/utils/request/index.d.ts.map +1 -0
- package/types/utils/request/interceptors.d.ts +4 -0
- package/types/utils/request/interceptors.d.ts.map +1 -0
- package/types/utils/request/url-params.d.ts +8 -0
- package/types/utils/request/url-params.d.ts.map +1 -0
- package/types/utils/sm3/index.d.ts +7 -0
- package/types/utils/sm3/index.d.ts.map +1 -0
- package/types/utils/sm3/sm3.d.ts +3 -0
- package/types/utils/sm3/sm3.d.ts.map +1 -0
- package/types/utils/storage.d.ts +8 -0
- package/types/utils/storage.d.ts.map +1 -0
- package/types/utils/table.d.ts +56 -0
- package/types/utils/table.d.ts.map +1 -0
- package/types/utils/token.d.ts +3 -0
- package/types/utils/token.d.ts.map +1 -0
- package/types/utils/uuid.d.ts +5 -0
- package/types/utils/uuid.d.ts.map +1 -0
- package/types/utils/validate.d.ts +9 -0
- package/types/utils/validate.d.ts.map +1 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
const toString = {}.toString
|
|
2
|
+
const hasOwnProperty = Object.prototype.hasOwnProperty
|
|
3
|
+
const PRECISION = 0.00001 // numbers less than this is considered as 0
|
|
4
|
+
const objectProto = Object.prototype
|
|
5
|
+
|
|
6
|
+
export const isNull = (value: any): value is null => value === null
|
|
7
|
+
export const isUndefined = (value: any): value is undefined => value === undefined
|
|
8
|
+
export const isNil = (value: any): value is null => isNull(value) || isUndefined(value)
|
|
9
|
+
|
|
10
|
+
export const getType = (value: any): string =>
|
|
11
|
+
toString
|
|
12
|
+
.call(value)
|
|
13
|
+
.replace(/^\[object /, '')
|
|
14
|
+
.replace(/]$/, '')
|
|
15
|
+
|
|
16
|
+
export const isType = (value: any, type: string): boolean =>
|
|
17
|
+
toString.call(value) === `[object ${type}]`
|
|
18
|
+
|
|
19
|
+
export const isPrototype = (value: any): boolean => {
|
|
20
|
+
const Ctor = value && value.constructor
|
|
21
|
+
const proto = (typeof Ctor === 'function' && Ctor.prototype) || objectProto
|
|
22
|
+
return value === proto
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const isBoolean = (value: any): value is boolean => isType(value, 'Boolean')
|
|
26
|
+
|
|
27
|
+
export const isNumber = (value: any): value is number => isType(value, 'Number')
|
|
28
|
+
|
|
29
|
+
export const isString = (str: any): str is string => isType(str, 'String')
|
|
30
|
+
|
|
31
|
+
export const isArray = (value: any): value is Array<any> =>
|
|
32
|
+
Array.isArray ? Array.isArray(value) : isType(value, 'Array')
|
|
33
|
+
|
|
34
|
+
export const isArrayLike = (value: any): boolean => {
|
|
35
|
+
/**
|
|
36
|
+
* isArrayLike([1, 2, 3]) => true
|
|
37
|
+
* isArrayLike(document.body.children) => true
|
|
38
|
+
* isArrayLike('abc') => true
|
|
39
|
+
* isArrayLike(Function) => false
|
|
40
|
+
*/
|
|
41
|
+
return value !== null && typeof value !== 'function' && Number.isFinite(value.length)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const isObject = <T = object>(value: any): value is T => {
|
|
45
|
+
/**
|
|
46
|
+
* isObject({}) => true
|
|
47
|
+
* isObject([1, 2, 3]) => true
|
|
48
|
+
* isObject(Function) => true
|
|
49
|
+
* isObject(null) => false
|
|
50
|
+
*/
|
|
51
|
+
const type = typeof value
|
|
52
|
+
return (value !== null && type === 'object') || type === 'function'
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const isObjectLike = (value: any): value is object => {
|
|
56
|
+
/**
|
|
57
|
+
* isObjectLike({}) => true
|
|
58
|
+
* isObjectLike([1, 2, 3]) => true
|
|
59
|
+
* isObjectLike(Function) => false
|
|
60
|
+
* isObjectLike(null) => false
|
|
61
|
+
*/
|
|
62
|
+
return typeof value === 'object' && value !== null
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export const isPlainObject = (value: any): value is object => {
|
|
66
|
+
/**
|
|
67
|
+
* isObjectLike(new Foo) => false
|
|
68
|
+
* isObjectLike([1, 2, 3]) => false
|
|
69
|
+
* isObjectLike({ x: 0, y: 0 }) => true
|
|
70
|
+
* isObjectLike(Object.create(null)) => true
|
|
71
|
+
*/
|
|
72
|
+
if (!isObjectLike(value) || !isType(value, 'Object')) return false
|
|
73
|
+
if (Object.getPrototypeOf(value) === null) return true
|
|
74
|
+
let proto = value
|
|
75
|
+
while (Object.getPrototypeOf(proto) !== null) {
|
|
76
|
+
proto = Object.getPrototypeOf(proto)
|
|
77
|
+
}
|
|
78
|
+
return Object.getPrototypeOf(value) === proto
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
82
|
+
export const isFunction = (value: any): value is Function => isType(value, 'Function')
|
|
83
|
+
|
|
84
|
+
export const isPromise = (value: any): boolean => value && typeof value.then === 'function'
|
|
85
|
+
|
|
86
|
+
export const isDate = (value: any): value is Date => isType(value, 'Date')
|
|
87
|
+
|
|
88
|
+
export const isArguments = (value: any): boolean => isType(value, 'Arguments')
|
|
89
|
+
|
|
90
|
+
export const isRegExp = (str: any): str is RegExp => isType(str, 'RegExp')
|
|
91
|
+
|
|
92
|
+
export const isError = (value: any): value is Error => isType(value, 'Error')
|
|
93
|
+
|
|
94
|
+
export const isElement = (o: any): boolean => o instanceof Element || o instanceof HTMLDocument
|
|
95
|
+
|
|
96
|
+
export const isEmpty = (value: any): boolean => {
|
|
97
|
+
/**
|
|
98
|
+
* isEmpty(null) => true
|
|
99
|
+
* isEmpty() => true
|
|
100
|
+
* isEmpty(true) => true
|
|
101
|
+
* isEmpty(1) => true
|
|
102
|
+
* isEmpty([1, 2, 3]) => false
|
|
103
|
+
* isEmpty('abc') => false
|
|
104
|
+
* isEmpty({ a: 1 }) => false
|
|
105
|
+
*/
|
|
106
|
+
if (isNil(value)) return true
|
|
107
|
+
if (isArrayLike(value)) return !value.length
|
|
108
|
+
const type = getType(value)
|
|
109
|
+
if (['Map', 'Set'].includes(type)) return !value.size
|
|
110
|
+
if (isPrototype(value)) return !Object.keys(value).length
|
|
111
|
+
for (const key in value) {
|
|
112
|
+
if (hasOwnProperty.call(value, key)) return false
|
|
113
|
+
}
|
|
114
|
+
return true
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export const isEqual = (value: any, other: any): boolean => {
|
|
118
|
+
if (value === other) return true
|
|
119
|
+
if (!value || !other) return false
|
|
120
|
+
if (isString(value) || isString(other)) return false
|
|
121
|
+
if (isArrayLike(value) || isArrayLike(other)) {
|
|
122
|
+
if (value.length !== other.length) return false
|
|
123
|
+
let rst = true
|
|
124
|
+
for (let i = 0; i < value.length; i++) {
|
|
125
|
+
rst = isEqual(value[i], other[i])
|
|
126
|
+
if (!rst) break
|
|
127
|
+
}
|
|
128
|
+
return rst
|
|
129
|
+
}
|
|
130
|
+
if (isObjectLike(value) || isObjectLike(other)) {
|
|
131
|
+
const valueKeys = Object.keys(value)
|
|
132
|
+
const otherKeys = Object.keys(other)
|
|
133
|
+
if (valueKeys.length !== otherKeys.length) return false
|
|
134
|
+
let rst = true
|
|
135
|
+
for (let i = 0; i < valueKeys.length; i++) {
|
|
136
|
+
rst = isEqual(value[valueKeys[i]], other[valueKeys[i]])
|
|
137
|
+
if (!rst) break
|
|
138
|
+
}
|
|
139
|
+
return rst
|
|
140
|
+
}
|
|
141
|
+
return false
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** number */
|
|
145
|
+
export const isEven = (num: any): boolean => isNumber(num) && num % 2 === 0
|
|
146
|
+
export const isOdd = (num: any): boolean => isNumber(num) && num % 2 !== 0
|
|
147
|
+
|
|
148
|
+
export const isFinite = (value: any): value is number => isNumber(value) && Number.isFinite(value)
|
|
149
|
+
|
|
150
|
+
export const isInteger = Number.isInteger
|
|
151
|
+
? Number.isInteger
|
|
152
|
+
: (num: any): boolean => isNumber(num) && num % 1 === 0
|
|
153
|
+
export const isDecimal = (num: any): boolean => isNumber(num) && num % 1 !== 0
|
|
154
|
+
|
|
155
|
+
export const isNegative = (num: any): boolean => isNumber(num) && num < 0
|
|
156
|
+
export const isPositive = (num: any): boolean => isNumber(num) && num > 0
|
|
157
|
+
|
|
158
|
+
export const isNumberEqual = (a: number, b: number, precision = PRECISION): boolean =>
|
|
159
|
+
Math.abs(a - b) < precision
|
|
160
|
+
|
|
161
|
+
export const isEqualWith = <T>(value: T, other: T, fn: (v1: T, v2: T) => boolean): boolean => {
|
|
162
|
+
if (!isFunction(fn)) return isEqual(value, other)
|
|
163
|
+
return !!fn(value, other)
|
|
164
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export const MIME_TYPE = {
|
|
2
|
+
aac: 'audio/aac',
|
|
3
|
+
abw: 'application/x-abiword',
|
|
4
|
+
arc: 'application/x-freearc',
|
|
5
|
+
avi: 'video/x-msvideo',
|
|
6
|
+
azw: 'application/vnd.amazon.ebook',
|
|
7
|
+
bin: 'application/octet-stream',
|
|
8
|
+
bmp: 'image/bmp',
|
|
9
|
+
bz: 'application/x-bzip',
|
|
10
|
+
bz2: 'application/x-bzip2',
|
|
11
|
+
csh: 'application/x-csh',
|
|
12
|
+
css: 'text/css',
|
|
13
|
+
csv: 'text/csv',
|
|
14
|
+
doc: 'application/msword',
|
|
15
|
+
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
16
|
+
eot: 'application/vnd.ms-fontobject',
|
|
17
|
+
epub: 'application/epub+zip',
|
|
18
|
+
gif: 'image/gif',
|
|
19
|
+
html: 'text/html',
|
|
20
|
+
ico: 'image/vnd.microsoft.icon',
|
|
21
|
+
ics: 'text/calendar',
|
|
22
|
+
jar: 'application/java-archive',
|
|
23
|
+
jpg: 'image/jpeg',
|
|
24
|
+
jpeg: 'image/jpeg',
|
|
25
|
+
js: 'text/javascript',
|
|
26
|
+
json: 'application/json',
|
|
27
|
+
jsonld: 'application/ld+json',
|
|
28
|
+
mid: 'audio/midi',
|
|
29
|
+
midi: 'audio/x-midi',
|
|
30
|
+
mjs: 'text/javascript',
|
|
31
|
+
mp3: 'audio/mpeg',
|
|
32
|
+
mpeg: 'video/mpeg',
|
|
33
|
+
mpkg: 'application/vnd.apple.installer+xml',
|
|
34
|
+
odp: 'application/vnd.oasis.opendocument.presentation',
|
|
35
|
+
ods: 'application/vnd.oasis.opendocument.spreadsheet',
|
|
36
|
+
odt: 'application/vnd.oasis.opendocument.text',
|
|
37
|
+
oga: 'audio/ogg',
|
|
38
|
+
ogv: 'video/ogg',
|
|
39
|
+
ogx: 'application/ogg',
|
|
40
|
+
otf: 'font/otf',
|
|
41
|
+
png: 'image/png',
|
|
42
|
+
pdf: 'application/pdf',
|
|
43
|
+
ppt: 'application/vnd.ms-powerpoint',
|
|
44
|
+
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
45
|
+
rar: 'application/x-rar-compressed',
|
|
46
|
+
rtf: 'application/rtf',
|
|
47
|
+
sh: 'application/x-sh',
|
|
48
|
+
svg: 'image/svg+xml',
|
|
49
|
+
swf: 'application/x-shockwave-flash',
|
|
50
|
+
tar: 'application/x-tar',
|
|
51
|
+
tiff: 'image/tiff',
|
|
52
|
+
ttf: 'font/ttf',
|
|
53
|
+
txt: 'text/plain',
|
|
54
|
+
vsd: 'application/vnd.visio',
|
|
55
|
+
wav: 'audio/wav',
|
|
56
|
+
weba: 'audio/webm',
|
|
57
|
+
webm: 'video/webm',
|
|
58
|
+
webp: 'image/webp',
|
|
59
|
+
woff: 'font/woff',
|
|
60
|
+
woff2: 'font/woff2',
|
|
61
|
+
xhtml: 'application/xhtml+xml',
|
|
62
|
+
xls: 'application/vnd.ms-excel',
|
|
63
|
+
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
64
|
+
xul: 'application/vnd.mozilla.xul+xml',
|
|
65
|
+
zip: 'application/zip',
|
|
66
|
+
'7z': 'application/x-7z-compressed',
|
|
67
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import _ from 'lodash'
|
|
2
|
+
|
|
3
|
+
// 数字每三位加逗号
|
|
4
|
+
export function toThousands(numStr: string) {
|
|
5
|
+
let result = ''
|
|
6
|
+
numStr = (numStr || 0).toString()
|
|
7
|
+
while (numStr.length > 3) {
|
|
8
|
+
result = ',' + numStr.slice(-3) + result
|
|
9
|
+
numStr = numStr.slice(0, numStr.length - 3)
|
|
10
|
+
}
|
|
11
|
+
if (numStr) {
|
|
12
|
+
result = numStr + result
|
|
13
|
+
}
|
|
14
|
+
return result
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 数字金额转为汉字
|
|
19
|
+
*/
|
|
20
|
+
export function toChies(numStr: string) {
|
|
21
|
+
const len = numStr.length //统计出长度
|
|
22
|
+
const chin_list = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'] //所有的数值对应的汉字
|
|
23
|
+
const chin_lisp = ['仟', '佰', '拾', '亿', '仟', '佰', '拾', '万', '仟', '佰', '拾'] //进制
|
|
24
|
+
|
|
25
|
+
let numbers: number[] = []
|
|
26
|
+
let hanStr: string[] = []
|
|
27
|
+
let result: string = ''
|
|
28
|
+
|
|
29
|
+
for (let i = 0; i < len; i++) {
|
|
30
|
+
numbers.push(parseInt(numStr[i])) //输入的数据按下标存进去 存进去的只是数字
|
|
31
|
+
hanStr[i] = chin_list[numbers[i]] //是根据我们输入的输入的数字,对应着我们的chin_list这个数组
|
|
32
|
+
} //123['壹','佰','贰','拾','叁']
|
|
33
|
+
|
|
34
|
+
for (let i = len - 1, j = 1; i > 0; i--) {
|
|
35
|
+
//i =2 1 //倒序 为了添加进制,方便我们去观看
|
|
36
|
+
hanStr.splice(i, 0, chin_lisp[chin_lisp.length - j++]) //j=2
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
result = hanStr.join('')
|
|
40
|
+
if (len >= 1) {
|
|
41
|
+
result += '元整'
|
|
42
|
+
}
|
|
43
|
+
return result
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function numToDX(numStr: string) {
|
|
47
|
+
if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(numStr)) return '数据非法'
|
|
48
|
+
let unit = '千百拾亿千百拾万千百拾元角分',
|
|
49
|
+
str = ''
|
|
50
|
+
numStr += '00'
|
|
51
|
+
let p = numStr.indexOf('.')
|
|
52
|
+
if (p >= 0) numStr = numStr.substring(0, p) + numStr.substr(p + 1, 2)
|
|
53
|
+
unit = unit.substr(unit.length - numStr.length)
|
|
54
|
+
for (let i = 0; i < numStr.length; i++)
|
|
55
|
+
str += '零壹贰叁肆伍陆柒捌玖'.charAt(parseInt(numStr.charAt(i))) + unit.charAt(i)
|
|
56
|
+
return str
|
|
57
|
+
.replace(/零(千|百|拾|角)/g, '零')
|
|
58
|
+
.replace(/(零)+/g, '零')
|
|
59
|
+
.replace(/零(万|亿|元)/g, '$1')
|
|
60
|
+
.replace(/(亿)万|壹(拾)/g, '$1$2')
|
|
61
|
+
.replace(/^元零?|零分/g, '')
|
|
62
|
+
.replace(/元$/g, '元整')
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function formatDecimal(num: number | string, decimal = 2) {
|
|
66
|
+
num = num.toString()
|
|
67
|
+
let index = num.indexOf('.')
|
|
68
|
+
if (index !== -1) {
|
|
69
|
+
num = num.substring(0, decimal + index + 1)
|
|
70
|
+
} else {
|
|
71
|
+
num = num.substring(0)
|
|
72
|
+
}
|
|
73
|
+
return parseFloat(num).toFixed(decimal)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 保留指定位数的小数,对最后一位进行四舍五入
|
|
78
|
+
*/
|
|
79
|
+
export function round(number: number, precision = 2): number {
|
|
80
|
+
if (typeof number === 'number') {
|
|
81
|
+
// 指数表示法表示的数字 幂指数为-7以后 +number不转换为数字 只好用正则匹配了
|
|
82
|
+
const reg = /([0-9])+e([-0-9]+)/
|
|
83
|
+
const result = reg.exec(number + '') || []
|
|
84
|
+
if (result.length > 2) {
|
|
85
|
+
const num = result[1]
|
|
86
|
+
const e = result[2]
|
|
87
|
+
return Math.round(parseInt(+num + 'e' + (+e + precision))) / Math.pow(10, precision)
|
|
88
|
+
}
|
|
89
|
+
return Math.round(parseInt(+number + 'e' + precision)) / Math.pow(10, precision)
|
|
90
|
+
}
|
|
91
|
+
return number
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 保留指定位数的小数,对最后一位进行四舍五入,并返回字符串
|
|
96
|
+
*/
|
|
97
|
+
export function toFixed(num: number | string, precision = 2): string {
|
|
98
|
+
const number = parseFloat(num + '')
|
|
99
|
+
if (!isNaN(number) && (number || number === 0)) {
|
|
100
|
+
return round(number, precision).toFixed(precision)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return num as string
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 数字转中文
|
|
108
|
+
*/
|
|
109
|
+
export const numToChineseNumerals = (num) => {
|
|
110
|
+
if (isNaN(num)) {
|
|
111
|
+
throw 'numToChineseNumerals: 数据非法'
|
|
112
|
+
}
|
|
113
|
+
num = Number(num)
|
|
114
|
+
return numToChineseNumeralsFormat(num).replace(/^零/, '').replace(/^一十/, '十')
|
|
115
|
+
}
|
|
116
|
+
const chineseNumeralList = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
|
|
117
|
+
const orderOfMagnitudeList = ['', '十', '百', '千']
|
|
118
|
+
function numToChineseNumeralsFormat(num) {
|
|
119
|
+
if (num === 0) {
|
|
120
|
+
return ''
|
|
121
|
+
}
|
|
122
|
+
const power = Math.floor(Math.log10(num))
|
|
123
|
+
const y = Math.floor(power / 8)
|
|
124
|
+
const x = Math.floor((power % 8) / 4)
|
|
125
|
+
let n1 = Math.floor(num / Math.pow(10, 4 * x + 8 * y))
|
|
126
|
+
let n2 = num % Math.pow(10, 4 * x + 8 * y)
|
|
127
|
+
let str = _(String(n1).padStart(4, '0'))
|
|
128
|
+
.split('')
|
|
129
|
+
.map(
|
|
130
|
+
(num, index) =>
|
|
131
|
+
chineseNumeralList[num] + (num === '0' ? '' : orderOfMagnitudeList[3 - index]),
|
|
132
|
+
)
|
|
133
|
+
.uniq()
|
|
134
|
+
.value()
|
|
135
|
+
.join('')
|
|
136
|
+
.replace(/零$/, '')
|
|
137
|
+
for (let i = 0; i < x; i++) {
|
|
138
|
+
str += '万'
|
|
139
|
+
}
|
|
140
|
+
for (let i = 0; i < y; i++) {
|
|
141
|
+
str += '亿'
|
|
142
|
+
}
|
|
143
|
+
return str + numToChineseNumeralsFormat(n2)
|
|
144
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// 获取季度汉字
|
|
2
|
+
export function quarter2Chinese(quarterNum: number) {
|
|
3
|
+
let quarterList = ['一', '二', '三', '四']
|
|
4
|
+
return quarterNum ? quarterList[quarterNum - 1] : ''
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
// 获取季度
|
|
8
|
+
export function getQuarter(date: string) {
|
|
9
|
+
if (!date || date.length < 6) {
|
|
10
|
+
return ''
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const year = date.substring(0, 4)
|
|
14
|
+
const month = date.substring(4, 6)
|
|
15
|
+
const quarter = getQuarterNum(parseInt(month))
|
|
16
|
+
return `${year}Q${quarter}`
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function getQuarterNum(month: number | string) {
|
|
20
|
+
if (typeof month === 'string') {
|
|
21
|
+
if (isNaN(parseInt(month))) return 0
|
|
22
|
+
month = parseInt(month)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return Math.ceil(month / 3)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function formatQuarter(quarter: string) {
|
|
29
|
+
if (!quarter || quarter.length < 6) {
|
|
30
|
+
return quarter
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const year = quarter.substring(0, 4)
|
|
34
|
+
const quarterNum = parseInt(quarter.substring(5))
|
|
35
|
+
return `${year}第${quarter2Chinese(quarterNum)}季度`
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function getQuarterBeginMonth(quarter: string) {
|
|
39
|
+
if (!quarter || quarter.length < 6) {
|
|
40
|
+
return quarter
|
|
41
|
+
}
|
|
42
|
+
const year = quarter.substring(0, 4)
|
|
43
|
+
const quarterNum = parseInt(quarter.substring(5))
|
|
44
|
+
const beginMonth = quarterNum * 3 - 2
|
|
45
|
+
const monthStr = beginMonth < 10 ? `0${beginMonth}` : `${beginMonth}`
|
|
46
|
+
return `${year}${monthStr}`
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function getQuarterEndMonth(quarter: string) {
|
|
50
|
+
if (!quarter || quarter.length < 6) {
|
|
51
|
+
return quarter
|
|
52
|
+
}
|
|
53
|
+
const year = quarter.substring(0, 4)
|
|
54
|
+
const quarterNum = parseInt(quarter.substring(5))
|
|
55
|
+
const endMonth = quarterNum * 3
|
|
56
|
+
const monthStr = endMonth < 10 ? `0${endMonth}` : `${endMonth}`
|
|
57
|
+
return `${year}${monthStr}`
|
|
58
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
|
|
2
|
+
|
|
3
|
+
export interface AxiosExtendsInstance extends AxiosInstance {
|
|
4
|
+
formPost?<T = any, R = AxiosResponse<T>>(
|
|
5
|
+
url: string,
|
|
6
|
+
data: any,
|
|
7
|
+
config?: AxiosRequestConfig,
|
|
8
|
+
): Promise<R>
|
|
9
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 配置在该文件下的正则匹配都会被缓存
|
|
3
|
+
* @type {RegExp[]}
|
|
4
|
+
*/
|
|
5
|
+
const cacheRules = [
|
|
6
|
+
/manage\/menu\/list/,
|
|
7
|
+
/manage\/menu\/list/,
|
|
8
|
+
/dict\/getDictsMap/,
|
|
9
|
+
/dict\/getDicts/,
|
|
10
|
+
/dict\/getDictsForMulti/,
|
|
11
|
+
/upmsOrg\/getSubOrgCodes/,
|
|
12
|
+
/upmsOrg\/getSubDeptCodesByOrgCode/,
|
|
13
|
+
/upmsOrg\/getProvincesCodes/,
|
|
14
|
+
/upmsOrg\/getCitysCodes/,
|
|
15
|
+
/upmsOrg\/getIndustrysCodes/,
|
|
16
|
+
/manage\/permission\/check/,
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
export default cacheRules
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const contentTypeKey = 'Content-Type'
|
|
2
|
+
|
|
3
|
+
export function getContentType(config: any) {
|
|
4
|
+
return config?.[contentTypeKey] || config?.['content-type']
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function setContentType(headers: any, type: string) {
|
|
8
|
+
headers[contentTypeKey] = type
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const CONTENT_TYPE = {
|
|
12
|
+
form: 'application/x-www-form-urlencoded',
|
|
13
|
+
'multi-form': 'multipart/form-data',
|
|
14
|
+
body: 'application/json',
|
|
15
|
+
os: 'application/octet-stream',
|
|
16
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import axios from 'axios'
|
|
2
|
+
import type { AxiosExtendsInstance } from './axios.extends'
|
|
3
|
+
import { requestInterceptors, responseInterceptors, inspectorError } from './interceptors'
|
|
4
|
+
import { CONTENT_TYPE, setContentType } from './content-type'
|
|
5
|
+
|
|
6
|
+
export * from './url-params'
|
|
7
|
+
export * from './content-type'
|
|
8
|
+
export { responseInterceptors }
|
|
9
|
+
|
|
10
|
+
const instance: AxiosExtendsInstance = axios.create({ timeout: 60 * 1000 })
|
|
11
|
+
instance.interceptors.request.use(requestInterceptors, inspectorError)
|
|
12
|
+
instance.interceptors.response.use(responseInterceptors, inspectorError)
|
|
13
|
+
|
|
14
|
+
instance.formPost = function (url, data, config) {
|
|
15
|
+
const headers = config?.headers || {}
|
|
16
|
+
setContentType(headers, CONTENT_TYPE.form)
|
|
17
|
+
return instance.post(url, data, { ...config, headers })
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { instance as axios }
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import qs from 'qs'
|
|
2
|
+
import { getUrlParams } from './url-params'
|
|
3
|
+
import { CONTENT_TYPE, getContentType } from './content-type'
|
|
4
|
+
// import cacheRules from './cache-rules'
|
|
5
|
+
import { getToken } from '@/utils/token'
|
|
6
|
+
import { config } from '@/config'
|
|
7
|
+
|
|
8
|
+
const SUCCESS_CODE = 1
|
|
9
|
+
const NO_SESSION_CODE = 10106
|
|
10
|
+
|
|
11
|
+
let timer: any = null
|
|
12
|
+
function _debounce(callback: Function) {
|
|
13
|
+
if (timer) {
|
|
14
|
+
clearTimeout(timer)
|
|
15
|
+
}
|
|
16
|
+
let callNow = !timer
|
|
17
|
+
timer = setTimeout(() => {
|
|
18
|
+
timer = null
|
|
19
|
+
}, 1000 * 20)
|
|
20
|
+
callNow && callback()
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// 请求的拦截器,处理Content-Type以及加入token
|
|
24
|
+
export function requestInterceptors(config) {
|
|
25
|
+
let token = getToken()
|
|
26
|
+
if (token) {
|
|
27
|
+
config.headers.token = token
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 以下拷自lambo-design
|
|
31
|
+
const params = getUrlParams()
|
|
32
|
+
// sso_token认证
|
|
33
|
+
if (params.hasOwnProperty('sso_token') || sessionStorage.getItem('sso_token')) {
|
|
34
|
+
config.params = Object.assign(
|
|
35
|
+
{
|
|
36
|
+
sso_token: params.sso_token || sessionStorage.getItem('sso_token'),
|
|
37
|
+
sso_id: params.sso_id,
|
|
38
|
+
},
|
|
39
|
+
config.params,
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const contentType = getContentType(config.headers)
|
|
44
|
+
if (config.method === 'post') {
|
|
45
|
+
// form 表单方式提交
|
|
46
|
+
if (contentType === CONTENT_TYPE.form) {
|
|
47
|
+
config.data = qs.stringify(config.data)
|
|
48
|
+
}
|
|
49
|
+
} else if (config.method === 'get') {
|
|
50
|
+
// 暂时不要这个逻辑
|
|
51
|
+
// for (let rule of cacheRules) {
|
|
52
|
+
// if (rule.test(config.url)) {
|
|
53
|
+
// return config
|
|
54
|
+
// }
|
|
55
|
+
// }
|
|
56
|
+
// if (config.url.indexOf('?') === -1) {
|
|
57
|
+
// config.url = config.url + '?t=' + new Date().getTime()
|
|
58
|
+
// } else {
|
|
59
|
+
// config.url = config.url + '&t=' + new Date().getTime()
|
|
60
|
+
// }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return config
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function responseInterceptors(response) {
|
|
67
|
+
let data = response.data
|
|
68
|
+
|
|
69
|
+
// 下载文件返回格式不是json
|
|
70
|
+
const contentType = getContentType(response.headers)
|
|
71
|
+
if (contentType === CONTENT_TYPE.os) {
|
|
72
|
+
return data
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (typeof response.data === 'string') {
|
|
76
|
+
data = JSON.parse(response.data)
|
|
77
|
+
}
|
|
78
|
+
const code = data.code
|
|
79
|
+
switch (code) {
|
|
80
|
+
case SUCCESS_CODE: {
|
|
81
|
+
return data
|
|
82
|
+
}
|
|
83
|
+
case NO_SESSION_CODE: {
|
|
84
|
+
_debounce(function () {
|
|
85
|
+
window.sessionNotValidHandler && window.sessionNotValidHandler()
|
|
86
|
+
})
|
|
87
|
+
break
|
|
88
|
+
}
|
|
89
|
+
default: {
|
|
90
|
+
let msg = data.message || data.msg || config.errorMessage
|
|
91
|
+
window.apiErrorHandler && window.apiErrorHandler(msg)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return Promise.reject(data)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function inspectorError(error) {
|
|
98
|
+
return Promise.reject(error)
|
|
99
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
interface UrlParams {
|
|
2
|
+
sso_token?: string
|
|
3
|
+
sso_id?: string
|
|
4
|
+
[propName: string]: any
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function getUrlParams(): UrlParams {
|
|
8
|
+
const url = location.search //获取url中"?"符后的字串
|
|
9
|
+
const theRequest = new Object()
|
|
10
|
+
if (url.indexOf('?') != -1) {
|
|
11
|
+
const str = url.substr(1)
|
|
12
|
+
let strs = str.split('&')
|
|
13
|
+
for (let i = 0; i < strs.length; i++) {
|
|
14
|
+
theRequest[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1])
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return theRequest
|
|
18
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import md5 from 'md5'
|
|
2
|
+
import sm3 from './sm3'
|
|
3
|
+
|
|
4
|
+
export const cryptor = {
|
|
5
|
+
encrypt: sm3,
|
|
6
|
+
encryptForLogin(s, method, add) {
|
|
7
|
+
let result = ''
|
|
8
|
+
if (!method) {
|
|
9
|
+
method = 'md5'
|
|
10
|
+
}
|
|
11
|
+
if (method == 'md5') {
|
|
12
|
+
if (add) {
|
|
13
|
+
result = md5(this.genSalt(s))
|
|
14
|
+
} else {
|
|
15
|
+
result = md5(s)
|
|
16
|
+
}
|
|
17
|
+
} else {
|
|
18
|
+
if (add) {
|
|
19
|
+
result = sm3(this.genSalt(s))
|
|
20
|
+
} else {
|
|
21
|
+
result = sm3(s)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return result
|
|
25
|
+
},
|
|
26
|
+
genSalt(s) {
|
|
27
|
+
return s + '{1#2$3%4(5)6@7!poeeww$3%4(5)djjkkldss}'
|
|
28
|
+
},
|
|
29
|
+
}
|