From b361b79b441fff98d46a9a4cc208574ac03481af Mon Sep 17 00:00:00 2001 From: chenos Date: Fri, 23 Aug 2024 13:40:37 +0800 Subject: [PATCH] fix: improve code --- src/client/PublicSharedForm.tsx | 4 ++-- src/client/useCreateActionProps.ts | 21 +++++---------------- src/server/plugin.ts | 8 +++++++- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/client/PublicSharedForm.tsx b/src/client/PublicSharedForm.tsx index a040430..db55161 100644 --- a/src/client/PublicSharedForm.tsx +++ b/src/client/PublicSharedForm.tsx @@ -50,7 +50,7 @@ function PublicAPIClientProvider({ children }) { const apiClient = new APIClient(app.getOptions().apiClient as any); apiClient.app = app; apiClient.axios.interceptors.request.use((config) => { - config.headers['X-Form-Token'] = apiClient.storage.getItem('NOCOBASE_FORM_TOKEN'); + config.headers['X-Form-Token'] = apiClient.storage.getItem('NOCOBASE_FORM_TOKEN') || ''; return config; }); return apiClient; @@ -68,7 +68,7 @@ function InternalSharedForm() { { onSuccess(data) { apiClient.axios.interceptors.request.use((config) => { - config.headers['X-Form-Token'] = data?.data?.token; + config.headers['X-Form-Token'] = data?.data?.token || ''; return config; }); }, diff --git a/src/client/useCreateActionProps.ts b/src/client/useCreateActionProps.ts index 867e75b..60b63a4 100644 --- a/src/client/useCreateActionProps.ts +++ b/src/client/useCreateActionProps.ts @@ -1,32 +1,21 @@ import { useForm } from '@formily/react'; -import { useActionContext, useCollection, useDataBlockRequest, useDataBlockResource } from '@nocobase/client'; +import { useDataBlockResource } from '@nocobase/client'; import { App as AntdApp } from 'antd'; export const useCreateActionProps = () => { - const { setVisible } = useActionContext(); const { message } = AntdApp.useApp(); const form = useForm(); const resource = useDataBlockResource(); - const { runAsync } = useDataBlockRequest(); - const collection = useCollection(); return { type: 'primary', async onClick() { await form.submit(); const values = form.values; - if (values[collection.filterTargetKey]) { - await resource.update({ - values, - filterByTk: values[collection.filterTargetKey], - }); - } else { - await resource.publicSubmit({ - values, - }); - } - await runAsync(); + await resource.publicSubmit({ + values, + }); + await form.reset(); message.success('Saved successfully!'); - setVisible(false); }, }; }; diff --git a/src/server/plugin.ts b/src/server/plugin.ts index 0697a73..309c8ee 100644 --- a/src/server/plugin.ts +++ b/src/server/plugin.ts @@ -174,11 +174,17 @@ export class PluginSharedFormsServer extends Plugin { if (!ctx.action) { return next(); } + const { actionName, resourceName, params } = ctx.action; + // 有密码时,跳过 token + if (resourceName === 'sharedForms' && actionName === 'getMeta' && params.password) { + return next(); + } const jwt = this.app.authManager.jwt; const token = ctx.get('X-Form-Token'); if (token) { try { - ctx.sharedForm = await jwt.decode(token); + // TODO:decode token + ctx.sharedForm = {}; // 将 publicSubmit 转为 create(用于触发工作流的 Action 事件) const actionName = ctx.action.actionName; if (actionName === 'publicSubmit') {