fix: jscode, refactor (#1379)

Reviewed-on: daoyoucloud/tachybase#1379
Reviewed-by: sealday <zhanglin@daoyoucloud.com>
Co-authored-by: bai.zixv <bai.zixv@foxmail.com>
Co-committed-by: bai.zixv <bai.zixv@foxmail.com>
This commit is contained in:
bai.zixv 2024-07-25 11:15:05 +08:00 committed by sealday
parent ac65c94730
commit ce1e07a56f

View File

@ -3,6 +3,7 @@ import { Input } from '@tachybase/client';
import { useField, useFieldSchema, useForm } from '@tachybase/schema'; import { useField, useFieldSchema, useForm } from '@tachybase/schema';
import { Descriptions } from 'antd'; import { Descriptions } from 'antd';
import dayjs from 'dayjs';
import _ from 'lodash'; import _ from 'lodash';
import { CodeFieldProps } from './Code.interface'; import { CodeFieldProps } from './Code.interface';
@ -47,7 +48,7 @@ function useAction(props: CodeFieldProps): string | React.ReactNode {
dynamicCode({ jsCode, form, path, recordData, result }, { setResult, formatFunc }); dynamicCode({ jsCode, form, path, recordData, result }, { setResult, formatFunc });
}, []); }, []);
const showItems = result.items.map((item) => { const showItems = result?.items?.map((item) => {
return { return {
label: item.label, label: item.label,
children: <p>{item.children}</p>, children: <p>{item.children}</p>,
@ -83,20 +84,21 @@ async function dynamicCode({ jsCode, form, path, recordData, result }, { setResu
// ], // ],
// }); // });
// }`; // }`;
/** 动态导入开始, 与 jsCode 配置相关的包 */
const dayjs = (await import('dayjs')).default;
const localeSetting = { invalidDate: '-' };
dayjs.updateLocale('en', localeSetting);
/** 动态导入结束 */
evalSimulate(jsCode, { const ctx = {
scopes: { form, path, recordData, result }, data: { form, path, recordData, result, setResult, formatFunc },
handlers: { body: {},
setResult, };
formatFunc,
await evalSimulate(jsCode, {
ctx,
lib: {
log: console.log,
JSON,
dayjs,
}, },
modules: { dayjs },
}); });
setResult(ctx.body);
} catch (error) { } catch (error) {
setResult({ setResult({
childrenType: '', childrenType: '',
@ -112,9 +114,10 @@ async function dynamicCode({ jsCode, form, path, recordData, result }, { setResu
} }
// 模拟 eval 实现, 相对更安全的实现和更好的性能, 以及限制作用域范围, // 模拟 eval 实现, 相对更安全的实现和更好的性能, 以及限制作用域范围,
function evalSimulate(jsCode, { scopes, handlers, modules }) { async function evalSimulate(jsCode, { ctx, lib }) {
const AsyncFunction: any = async function () {}.constructor;
try { try {
return new Function('$root', `with($root) { ${jsCode}; }`)({ scopes, handlers, modules }); return await new AsyncFunction('$root', `with($root) { ${jsCode}; }`)({ ctx, lib });
} catch (err) { } catch (err) {
console.log('err', err); console.log('err', err);
} }