perf: 优化 JSONParse 过滤函数值表达式写法

This commit is contained in:
奔跑的面条 2023-03-04 14:18:17 +08:00
parent 134b44944e
commit 00a4c752ed
2 changed files with 18 additions and 10 deletions

View File

@ -7,7 +7,7 @@ export enum BaseEvent {
// 移入 // 移入
ON_MOUSE_ENTER = 'mouseenter', ON_MOUSE_ENTER = 'mouseenter',
// 移出 // 移出
ON_MOUSE_LEAVE = 'mouseleave', ON_MOUSE_LEAVE = 'mouseleave'
} }
// vue3 生命周期事件 // vue3 生命周期事件
@ -15,7 +15,7 @@ export enum EventLife {
// 渲染之后 // 渲染之后
VNODE_MOUNTED = 'vnodeMounted', VNODE_MOUNTED = 'vnodeMounted',
// 渲染之前 // 渲染之前
VNODE_BEFORE_MOUNT = 'vnodeBeforeMount', VNODE_BEFORE_MOUNT = 'vnodeBeforeMount'
} }
// 内置字符串函数对象列表 // 内置字符串函数对象列表
@ -29,3 +29,8 @@ export const excludeParseEventKeyList = [
//过滤器 //过滤器
'filter' 'filter'
] ]
// 内置字符串函数键值列表
export const excludeParseEventValueList = [
// 请求里的函数语句
'javascript:'
]

View File

@ -10,7 +10,7 @@ import cloneDeep from 'lodash/cloneDeep'
import { WinKeyboard } from '@/enums/editPageEnum' import { WinKeyboard } from '@/enums/editPageEnum'
import { RequestHttpIntervalEnum, RequestParamsObjType } from '@/enums/httpEnum' import { RequestHttpIntervalEnum, RequestParamsObjType } from '@/enums/httpEnum'
import { CreateComponentType, CreateComponentGroupType } from '@/packages/index.d' import { CreateComponentType, CreateComponentGroupType } from '@/packages/index.d'
import { excludeParseEventKeyList } from '@/enums/eventEnum' import { excludeParseEventKeyList, excludeParseEventValueList } from '@/enums/eventEnum'
/** /**
* * * *
@ -320,14 +320,17 @@ export const JSONStringify = <T>(data: T) => {
*/ */
export const JSONParse = (data: string) => { export const JSONParse = (data: string) => {
return JSON.parse(data, (k, v) => { return JSON.parse(data, (k, v) => {
// 过滤函数字符串
if (excludeParseEventKeyList.includes(k)) return v if (excludeParseEventKeyList.includes(k)) return v
if(typeof v === 'string' && v.indexOf('javascript:') > -1){ // 过滤函数值表达式
//动态请求json中'javascript:'内容会影响模板content解析直接返回 if (typeof v === 'string') {
return v 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)) { if (typeof v === 'string' && v.indexOf && (v.indexOf('function') > -1 || v.indexOf('=>') > -1)) {
return eval(`(function(){return ${v}})()`) return eval(`(function(){return ${v}})()`)
} else if (typeof v === 'string' && v.indexOf && (v.indexOf('return ') > -1)) { } else if (typeof v === 'string' && v.indexOf && v.indexOf('return ') > -1) {
const baseLeftIndex = v.indexOf('(') const baseLeftIndex = v.indexOf('(')
if (baseLeftIndex > -1) { if (baseLeftIndex > -1) {
const newFn = `function ${v.substring(baseLeftIndex)}` const newFn = `function ${v.substring(baseLeftIndex)}`