Commit 554ed39b130f512f480e38e9f91f5896dd49a1b2
1 parent
6e78affd
fix(public request): DEFECT-1239 修复JSONParse解析方法方法解析bug
Showing
2 changed files
with
40 additions
and
4 deletions
src/utils/external/utils.ts
0 → 100644
| 1 | +import { excludeParseEventKeyList, excludeParseEventValueList } from "@/enums/eventEnum" | |
| 2 | + | |
| 3 | +const tryRunFunction = (v: string) => { | |
| 4 | + try { | |
| 5 | + return eval(`(function(){return ${v}})()`) | |
| 6 | + } catch (error) { | |
| 7 | + return v | |
| 8 | + } | |
| 9 | +} | |
| 10 | + | |
| 11 | +export const JSONParse = (data: string) => { | |
| 12 | + return JSON.parse(data, (k, v) => { | |
| 13 | + // 过滤函数字符串 | |
| 14 | + if (excludeParseEventKeyList.includes(k)) return v | |
| 15 | + // 过滤函数值表达式 | |
| 16 | + if (typeof v === 'string') { | |
| 17 | + const someValue = excludeParseEventValueList.some(excludeValue => v.indexOf(excludeValue) > -1) | |
| 18 | + if (someValue) return v | |
| 19 | + } | |
| 20 | + // 还原函数值 | |
| 21 | + if (typeof v === 'string' && v.indexOf && (v.indexOf('function') > -1 || v.indexOf('=>') > -1)) { | |
| 22 | + return tryRunFunction(v) | |
| 23 | + } else if (typeof v === 'string' && v.indexOf && v.indexOf('return ') > -1) { | |
| 24 | + const baseLeftIndex = v.indexOf('(') | |
| 25 | + if (baseLeftIndex > -1) { | |
| 26 | + const newFn = `function ${v.substring(baseLeftIndex)}` | |
| 27 | + return tryRunFunction(newFn) | |
| 28 | + } | |
| 29 | + } | |
| 30 | + return v | |
| 31 | + }) | |
| 32 | +} | ... | ... |
| ... | ... | @@ -12,6 +12,9 @@ import { RequestHttpIntervalEnum, RequestParamsObjType } from '@/enums/httpEnum' |
| 12 | 12 | import { CreateComponentType, CreateComponentGroupType } from '@/packages/index.d' |
| 13 | 13 | import { excludeParseEventKeyList, excludeParseEventValueList } from '@/enums/eventEnum' |
| 14 | 14 | |
| 15 | +/// THINGS_KIT 替换JSONParse解析 | |
| 16 | +export { JSONParse } from './external/utils' | |
| 17 | + | |
| 15 | 18 | /** |
| 16 | 19 | * * 判断是否是开发环境 |
| 17 | 20 | * @return { Boolean } |
| ... | ... | @@ -127,11 +130,11 @@ export const fileToUrl = (file: File): string => { |
| 127 | 130 | * * file转base64 |
| 128 | 131 | */ |
| 129 | 132 | export const fileTobase64 = (file: File, callback: Function) => { |
| 130 | - let reader = new FileReader() | |
| 133 | + const reader = new FileReader() | |
| 131 | 134 | reader.readAsDataURL(file) |
| 132 | 135 | reader.onload = function (e: ProgressEvent<FileReader>) { |
| 133 | 136 | if (e.target) { |
| 134 | - let base64 = e.target.result | |
| 137 | + const base64 = e.target.result | |
| 135 | 138 | callback(base64) |
| 136 | 139 | } |
| 137 | 140 | } |
| ... | ... | @@ -318,7 +321,8 @@ export const JSONStringify = <T>(data: T) => { |
| 318 | 321 | * * JSON反序列化,支持函数和 undefined |
| 319 | 322 | * @param data |
| 320 | 323 | */ |
| 321 | -export const JSONParse = (data: string) => { | |
| 324 | +/// THINGS_KIT 重命名 JSONParse 为 JSONParseOriginal | |
| 325 | +export const JSONParseOriginal = (data: string) => { | |
| 322 | 326 | return JSON.parse(data, (k, v) => { |
| 323 | 327 | // 过滤函数字符串 |
| 324 | 328 | if (excludeParseEventKeyList.includes(k)) return v |
| ... | ... | @@ -347,4 +351,4 @@ export const JSONParse = (data: string) => { |
| 347 | 351 | */ |
| 348 | 352 | export const setTitle = (title?: string) => { |
| 349 | 353 | title && (document.title = title) |
| 350 | -} | |
| \ No newline at end of file | ||
| 354 | +} | ... | ... |