utils.ts
1.23 KB
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
import { excludeParseEventKeyList, excludeParseEventValueList } from '@/enums/eventEnum'
const tryRunFunction = (v: string) => {
try {
return eval(`(function(){return ${v}})()`)
} catch (error) {
return v
}
}
export const JSONParse = (data: string) => {
return JSON.parse(data, (k, v) => {
// 过滤函数字符串
if (excludeParseEventKeyList.includes(k)) return v
// 过滤函数值表达式
if (typeof v === 'string') {
const someValue = excludeParseEventValueList.some(excludeValue => v.indexOf(excludeValue) > -1)
if (someValue) return v
}
// 还原函数值
if (typeof v === 'string' && v.indexOf && (v.indexOf('function') > -1 || v.indexOf('=>') > -1)) {
return tryRunFunction(v)
} else if (typeof v === 'string' && v.indexOf && v.indexOf('return ') > -1) {
const baseLeftIndex = v.indexOf('(')
if (baseLeftIndex > -1) {
const newFn = `function ${v.substring(baseLeftIndex)}`
return tryRunFunction(newFn)
}
}
return v
})
}
//解析web url 参数
export const parseWebUrl = (str = window.location.search) => {
const reg = /([^?&=]+)=([^&]+)/g
const params = {} as any
str.replace(reg, (_, k, v) => (params[k] = v))
return params
}