AddAppDrawer.tsx 5.3 KB

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