AddTemplateDrawer.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import { PlusOutlined } from "@ant-design/icons";
  2. import {
  3. DrawerForm,
  4. ProForm,
  5. ProFormSelect,
  6. ProFormText,
  7. ProFormUploadButton,
  8. ProFormTextArea,
  9. ProFormMoney,
  10. } from "@ant-design/pro-components";
  11. import { Button, Space, message } from "antd";
  12. import type { FormInstance } from "@ant-design/pro-components";
  13. import { useEffect, useState, useMemo, useRef } from "react";
  14. import Editor from "@/components/Editor";
  15. import { customUploadRequest } from "@/utils";
  16. import { SaveOrUpdateTemplate, GetTemplateDetail } from "@/api/templateStore";
  17. import {
  18. APPLICATION_SCENARIOS_OPTIONS,
  19. INDUSTRIE_OPTIONS,
  20. MODULE_TEMPLATE_TYPE,
  21. } from "@/constants";
  22. import { useRequest } from "umi";
  23. export default ({
  24. onSuccess,
  25. editData,
  26. onClose,
  27. }: {
  28. onSuccess: () => void;
  29. onClose: () => void;
  30. editData?: any;
  31. }) => {
  32. const [drawerVisit, setDrawerVisit] = useState(false);
  33. const [html, setHtml] = useState("");
  34. const formRef = useRef<FormInstance>();
  35. const { run } = useRequest(GetTemplateDetail, {
  36. manual: true,
  37. onSuccess: (res) => {
  38. setHtml(res?.result?.detail || "");
  39. },
  40. });
  41. useEffect(() => {
  42. setDrawerVisit(!!editData);
  43. if(editData?.id) {
  44. run({
  45. id: editData.id,
  46. });
  47. }
  48. }, [editData]);
  49. useEffect(() => {
  50. if (!drawerVisit) {
  51. onClose();
  52. setHtml("");
  53. formRef.current?.resetFields();
  54. }
  55. }, [drawerVisit]);
  56. const initialValues = useMemo(() => {
  57. if (editData) {
  58. return {
  59. ...editData,
  60. industries: editData.industries ? JSON.parse(editData.industries) : [],
  61. applicationScenarios: editData.applicationScenarios ? JSON.parse(editData.applicationScenarios) : [],
  62. icon: editData.icon
  63. ? [
  64. {
  65. uid: editData.icon,
  66. name: editData.icon,
  67. status: "done",
  68. url: `/api/File/Download?fileId=${editData.icon}`,
  69. },
  70. ]
  71. : [],
  72. };
  73. }
  74. return {};
  75. }, [editData]);
  76. return (
  77. <>
  78. <Space>
  79. <Button
  80. type="primary"
  81. onClick={() => {
  82. setDrawerVisit(true);
  83. }}
  84. >
  85. <PlusOutlined />
  86. 新增模版
  87. </Button>
  88. </Space>
  89. <DrawerForm
  90. onOpenChange={setDrawerVisit}
  91. title="新增模版"
  92. open={drawerVisit}
  93. initialValues={initialValues}
  94. onFinish={async (values: any) => {
  95. await SaveOrUpdateTemplate({
  96. ...values,
  97. id: editData?.id,
  98. version: editData?.version,
  99. icon: values?.icon?.[0]?.response.id || values?.icon?.[0].uid || "",
  100. tags: values.tags?.replaceAll(",", ","),
  101. detail: html,
  102. isFree: !editData?.price,
  103. });
  104. onSuccess();
  105. message.success("提交成功");
  106. return true;
  107. }}
  108. drawerProps={{
  109. maskClosable: false,
  110. destroyOnClose: true,
  111. }}
  112. formRef={formRef}
  113. size="small"
  114. >
  115. <ProForm.Group title="基础信息">
  116. <ProFormSelect
  117. width="md"
  118. name="applicationId"
  119. label="所属应用"
  120. placeholder="请选择"
  121. />
  122. <ProFormSelect
  123. width="md"
  124. name="type"
  125. label="模版分类"
  126. placeholder="请选择"
  127. options={MODULE_TEMPLATE_TYPE}
  128. />
  129. <ProFormSelect
  130. width="md"
  131. name="moduleId"
  132. label="选择模版"
  133. placeholder="请选择"
  134. />
  135. <ProFormText
  136. width="md"
  137. name="name"
  138. label="模版名称"
  139. tooltip="最长为 30 位"
  140. placeholder="请输入名称"
  141. rules={[
  142. { required: true, message: "请输入名称" },
  143. { max: 30, message: "名称不能超过30个字符" },
  144. ]}
  145. />
  146. <ProFormUploadButton
  147. max={1}
  148. fieldProps={{
  149. multiple: false,
  150. name: "file",
  151. listType: "picture-card",
  152. accept: "image/*",
  153. customRequest: customUploadRequest,
  154. }}
  155. name="icon"
  156. label="图标"
  157. rules={[{ required: true, message: "请上传应用图标" }]}
  158. />
  159. </ProForm.Group>
  160. <ProFormTextArea
  161. name="desc"
  162. label="模版简介"
  163. placeholder="请输入简介,1000字以内"
  164. />
  165. <ProForm.Group>
  166. <ProFormSelect
  167. width="md"
  168. name="industries"
  169. label="所属行业"
  170. placeholder="请选择"
  171. mode="multiple"
  172. options={INDUSTRIE_OPTIONS}
  173. />
  174. <ProFormSelect
  175. width="md"
  176. name="applicationScenarios"
  177. label="应用场景"
  178. placeholder="请选择"
  179. mode="multiple"
  180. options={APPLICATION_SCENARIOS_OPTIONS}
  181. />
  182. <ProFormText
  183. width="md"
  184. name="tags"
  185. label="标签"
  186. placeholder="多个标签用逗号分隔"
  187. />
  188. <ProFormMoney
  189. width="md"
  190. name="price"
  191. label="定价"
  192. defaultValue={0}
  193. min={0}
  194. placeholder="请输入"
  195. locale="zh-CN"
  196. />
  197. </ProForm.Group>
  198. <ProForm.Group title="详情信息">
  199. <Editor html={html} onChange={setHtml} />
  200. </ProForm.Group>
  201. </DrawerForm>
  202. </>
  203. );
  204. };