From 00a4c752ed34919916c707a33078584b0c4fae3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A5=94=E8=B7=91=E7=9A=84=E9=9D=A2=E6=9D=A1?= <1262327911@qq.com> Date: Sat, 4 Mar 2023 14:18:17 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=20JSONParse=20?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E5=87=BD=E6=95=B0=E5=80=BC=E8=A1=A8=E8=BE=BE?= =?UTF-8?q?=E5=BC=8F=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/enums/eventEnum.ts | 13 +++++++++---- src/utils/utils.ts | 15 +++++++++------ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/enums/eventEnum.ts b/src/enums/eventEnum.ts index a8110a71..9f05aa57 100644 --- a/src/enums/eventEnum.ts +++ b/src/enums/eventEnum.ts @@ -7,15 +7,15 @@ export enum BaseEvent { // 移入 ON_MOUSE_ENTER = 'mouseenter', // 移出 - ON_MOUSE_LEAVE = 'mouseleave', + ON_MOUSE_LEAVE = 'mouseleave' } // vue3 生命周期事件 -export enum EventLife { +export enum EventLife { // 渲染之后 VNODE_MOUNTED = 'vnodeMounted', // 渲染之前 - VNODE_BEFORE_MOUNT = 'vnodeBeforeMount', + VNODE_BEFORE_MOUNT = 'vnodeBeforeMount' } // 内置字符串函数对象列表 @@ -28,4 +28,9 @@ export const excludeParseEventKeyList = [ BaseEvent.ON_MOUSE_LEAVE, //过滤器 'filter' -] \ No newline at end of file +] +// 内置字符串函数键值列表 +export const excludeParseEventValueList = [ + // 请求里的函数语句 + 'javascript:' +] diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 2ab2adf1..9d0302ba 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -10,7 +10,7 @@ import cloneDeep from 'lodash/cloneDeep' import { WinKeyboard } from '@/enums/editPageEnum' import { RequestHttpIntervalEnum, RequestParamsObjType } from '@/enums/httpEnum' 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 = (data: T) => { */ export const JSONParse = (data: string) => { return JSON.parse(data, (k, v) => { + // 过滤函数字符串 if (excludeParseEventKeyList.includes(k)) return v - if(typeof v === 'string' && v.indexOf('javascript:') > -1){ - //动态请求json中'javascript:'内容会影响模板content解析,直接返回 - 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 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('(') if (baseLeftIndex > -1) { const newFn = `function ${v.substring(baseLeftIndex)}` @@ -344,4 +347,4 @@ export const JSONParse = (data: string) => { */ export const setTitle = (title?: string) => { title && (document.title = title) -} \ No newline at end of file +}