Browse Source

fix: 修改表单校验,编辑回填

liaojiaxing 1 month ago
parent
commit
576885a8f8

+ 1 - 1
src/layouts/index.tsx

@@ -34,7 +34,7 @@ export default function Layout() {
           </ul>
 
           <div className="right w-200px text-right">
-            <Avatar size={32} icon={<UserOutlined />} />
+            {/* <Avatar size={32} icon={<UserOutlined />} /> */}
           </div>
         </div>
 

+ 4 - 7
src/pages/management/AddAppDrawer.tsx

@@ -34,7 +34,7 @@ export default ({
   }, [editData]);
 
   useEffect(() => {
-    if(!drawerVisit) {
+    if (!drawerVisit) {
       onClose();
     }
   }, [drawerVisit]);
@@ -83,7 +83,7 @@ export default ({
             ...values,
             id: editData?.id,
             version: editData?.version,
-            icon: values.icon[0]?.response?.id || values.icon[0]?.uid || "",
+            icon: values?.icon?.[0]?.response.id || values?.icon?.[0].uid || "",
             tags: values.tags?.replaceAll(",", ","),
             detail: html,
             isFree: !values?.price,
@@ -117,16 +117,13 @@ export default ({
               { required: true, message: "请输入名称" },
               { max: 30, message: "名称不能超过30个字符" },
             ]}
-            required
           />
           <ProFormText
             width="md"
             name="trialUrl"
             label="试用地址"
             placeholder="请输入名称"
-            rules={[
-              { max: 100, message: "名称不能超过100个字符" },
-            ]}
+            rules={[{ max: 100, message: "名称不能超过100个字符" }]}
           />
           <ProFormUploadButton
             fieldProps={{
@@ -139,7 +136,7 @@ export default ({
             max={1}
             name="icon"
             label="图标"
-            required
+            rules={[{ required: true, message: "请上传应用图标" }]}
           />
         </ProForm.Group>
         <ProFormTextArea

+ 37 - 7
src/pages/management/AddTemplateDrawer.tsx

@@ -9,16 +9,20 @@ import {
   ProFormMoney,
 } from "@ant-design/pro-components";
 import { Button, Space, message } from "antd";
-import { useEffect, useState } from "react";
+import { useEffect, useState, useMemo } from "react";
 import Editor from "@/components/Editor";
 import { customUploadRequest } from "@/utils";
 import { SaveOrUpdateTemplate } from "@/api/templateStore";
-import { APPLICATION_SCENARIOS_OPTIONS, INDUSTRIE_OPTIONS, MODULE_TEMPLATE_TYPE } from "@/constants";
+import {
+  APPLICATION_SCENARIOS_OPTIONS,
+  INDUSTRIE_OPTIONS,
+  MODULE_TEMPLATE_TYPE,
+} from "@/constants";
 
 export default ({
   onSuccess,
   editData,
-  onClose
+  onClose,
 }: {
   onSuccess: () => void;
   onClose: () => void;
@@ -32,11 +36,32 @@ export default ({
   }, [editData]);
 
   useEffect(() => {
-    if(!drawerVisit) {
+    if (!drawerVisit) {
       onClose();
     }
   }, [drawerVisit]);
 
+  const initialValues = useMemo(() => {
+    if (editData) {
+      return {
+        ...editData,
+        industries: JSON.parse(editData.industries),
+        applicationScenarios: JSON.parse(editData.applicationScenarios),
+        icon: editData.icon
+          ? [
+              {
+                uid: editData.icon,
+                name: editData.icon,
+                status: "done",
+                url: `/api/File/Download?fileId=${editData.icon}`,
+              },
+            ]
+          : [],
+      };
+    }
+    return {};
+  }, [editData]);
+
   return (
     <>
       <Space>
@@ -55,13 +80,14 @@ export default ({
         onOpenChange={setDrawerVisit}
         title="新增模版"
         open={drawerVisit}
+        initialValues={initialValues}
         onFinish={async (values: any) => {
           await SaveOrUpdateTemplate({
             ...values,
             id: editData?.id,
             version: editData?.version,
-            icon: values.icon[0]?.response?.id || '',
-            tags: values.tags?.replaceAll(',', ','),
+            icon: values?.icon?.[0]?.response.id || values?.icon?.[0].uid || "",
+            tags: values.tags?.replaceAll(",", ","),
             detail: html,
             isFree: !editData?.price,
           });
@@ -103,7 +129,10 @@ export default ({
             label="模版名称"
             tooltip="最长为 30 位"
             placeholder="请输入名称"
-            rules={[{ required: true , message: '请输入名称'}, { max: 30, message: '名称不能超过30个字符' }]}
+            rules={[
+              { required: true, message: "请输入名称" },
+              { max: 30, message: "名称不能超过30个字符" },
+            ]}
           />
 
           <ProFormUploadButton
@@ -117,6 +146,7 @@ export default ({
             }}
             name="icon"
             label="图标"
+            rules={[{ required: true, message: "请上传应用图标" }]}
           />
         </ProForm.Group>
         <ProFormTextArea