index.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import { Button, Modal } from 'antd';
  2. import { createForm, registerValidateRules } from '@formily/core';
  3. import { createSchemaField } from '@formily/react';
  4. import React, { useEffect, useState } from 'react';
  5. import * as ICONS from '@ant-design/icons';
  6. import { Form, FormGrid, FormItem, Input, Select } from '@formily/antd';
  7. import type { ISchema } from '@formily/json-schema';
  8. import { service } from '@/pages/link/Protocol';
  9. import FileUpload from '../FileUpload';
  10. import type { ProtocolItem } from '@/pages/link/Protocol/typings';
  11. import { PermissionButton, RadioCard } from '@/components';
  12. import { onlyMessage } from '@/utils/util';
  13. interface Props {
  14. data: ProtocolItem | undefined;
  15. close: () => void;
  16. reload: () => void;
  17. }
  18. const Save = (props: Props) => {
  19. const [data, setData] = useState<ProtocolItem | undefined>(props.data);
  20. const { permission } = PermissionButton.usePermission('link/Protocol');
  21. // const [count, setCount] = useState<number>(0);
  22. useEffect(() => {
  23. setData(props.data);
  24. // if (props.data?.id) {
  25. // service
  26. // .productCount({
  27. // terms: [
  28. // {
  29. // terms: [
  30. // {
  31. // column: 'message_protocol',
  32. // value: props.data.id,
  33. // },
  34. // ],
  35. // },
  36. // ],
  37. // })
  38. // .then((resp) => {
  39. // if (resp.status === 200) {
  40. // setCount(resp.result);
  41. // }
  42. // });
  43. // }
  44. }, [props.data]);
  45. const form = createForm({
  46. validateFirst: true,
  47. initialValues: data || {},
  48. });
  49. registerValidateRules({
  50. validateId(value) {
  51. if (!value) return '';
  52. const reg = new RegExp('^[0-9a-zA-Z_\\\\-]+$');
  53. return reg.exec(value) ? '' : 'ID只能由数字、26个英文字母或者下划线组成';
  54. },
  55. });
  56. const SchemaField = createSchemaField({
  57. components: {
  58. FormItem,
  59. Input,
  60. Select,
  61. FileUpload,
  62. FormGrid,
  63. RadioCard,
  64. },
  65. scope: {
  66. icon(name: any) {
  67. return React.createElement(ICONS[name]);
  68. },
  69. },
  70. });
  71. const schema: ISchema = {
  72. type: 'object',
  73. properties: {
  74. layout: {
  75. type: 'void',
  76. 'x-component': 'FormGrid',
  77. 'x-component-props': {
  78. maxColumns: 2,
  79. minColumns: 2,
  80. columnGap: 24,
  81. },
  82. properties: {
  83. // id: {
  84. // title: 'ID',
  85. // 'x-component': 'Input',
  86. // 'x-decorator': 'FormItem',
  87. // 'x-disabled': !!props.data?.id,
  88. // 'x-decorator-props': {
  89. // gridSpan: 2,
  90. // },
  91. // 'x-validator': [
  92. // {
  93. // required: true,
  94. // message: '请输入ID',
  95. // },
  96. // {
  97. // max: 64,
  98. // message: '最多可输入64个字符',
  99. // },
  100. // {
  101. // validateId: true,
  102. // message: 'ID只能由数字、26个英文字母或者下划线组成',
  103. // },
  104. // {
  105. // triggerType: 'onBlur',
  106. // validator: (value: string) => {
  107. // if (!value) return;
  108. // return new Promise((resolve) => {
  109. // service
  110. // .validator(value)
  111. // .then((resp) => {
  112. // if (!!resp?.result) {
  113. // resolve('ID已存在');
  114. // } else {
  115. // resolve('');
  116. // }
  117. // })
  118. // .catch(() => '验证失败!');
  119. // });
  120. // },
  121. // },
  122. // ],
  123. // 'x-component-props': {
  124. // placeholder: '请输入ID',
  125. // },
  126. // },
  127. name: {
  128. title: '名称',
  129. 'x-component': 'Input',
  130. 'x-decorator': 'FormItem',
  131. 'x-decorator-props': {
  132. gridSpan: 2,
  133. },
  134. 'x-component-props': {
  135. placeholder: '请输入名称',
  136. },
  137. 'x-validator': [
  138. {
  139. required: true,
  140. message: '请输入名称',
  141. },
  142. {
  143. max: 64,
  144. message: '最多可输入64个字符',
  145. },
  146. ],
  147. },
  148. type: {
  149. title: '类型',
  150. 'x-component': 'RadioCard',
  151. 'x-decorator': 'FormItem',
  152. 'x-disabled': !!props.data?.id,
  153. 'x-decorator-props': {
  154. gridSpan: 2,
  155. },
  156. default: 'jar',
  157. 'x-component-props': {
  158. model: 'singular',
  159. itemStyle: {
  160. width: '50%',
  161. },
  162. options: [
  163. {
  164. label: 'Jar',
  165. value: 'jar',
  166. imgUrl: require('/public/images/jar.png'),
  167. },
  168. {
  169. label: 'Local',
  170. value: 'local',
  171. imgUrl: require('/public/images/local.png'),
  172. },
  173. ],
  174. },
  175. 'x-validator': [
  176. {
  177. required: true,
  178. message: '请选择类型',
  179. },
  180. ],
  181. // enum: [
  182. // { label: 'jar', value: 'jar' },
  183. // { label: 'local', value: 'local' },
  184. // // { label: 'script', value: 'script' },
  185. // ],
  186. },
  187. configuration: {
  188. type: 'object',
  189. properties: {
  190. location: {
  191. title: '文件地址',
  192. 'x-decorator': 'FormItem',
  193. 'x-visible': false,
  194. 'x-decorator-props': {
  195. gridSpan: 2,
  196. },
  197. // 'x-disabled': !!count,
  198. 'x-validator': [
  199. {
  200. required: true,
  201. message: '请输入文件地址',
  202. },
  203. ],
  204. 'x-reactions': {
  205. dependencies: ['..type'],
  206. fulfill: {
  207. state: {
  208. visible: '{{["jar","local"].includes($deps[0])}}',
  209. componentType: '{{$deps[0]==="jar"?"FileUpload":"Input"}}',
  210. componentProps:
  211. '{{$deps[0]==="jar"?{type:"file", accept: ".jar, .zip"}:{placeholder: "请输入文件地址"}}}',
  212. decoratorProps:
  213. '{{$deps[0]!=="jar"?{gridSpan:"2",tooltip:"local:填写本地协议编译目录绝对地址,如:d:/protocol/target/classes"}:{gridSpan:"2",tooltip:"jar:上传协议jar包,文件格式支持.jar或.zip"}}}',
  214. },
  215. },
  216. },
  217. },
  218. },
  219. },
  220. description: {
  221. title: '说明',
  222. 'x-component': 'Input.TextArea',
  223. 'x-decorator': 'FormItem',
  224. 'x-decorator-props': {
  225. gridSpan: 2,
  226. },
  227. 'x-component-props': {
  228. rows: 3,
  229. showCount: true,
  230. maxLength: 200,
  231. placeholder: '请输入说明',
  232. },
  233. },
  234. },
  235. },
  236. },
  237. };
  238. const save = async () => {
  239. const value = await form.submit<ProtocolItem>();
  240. const response: any = await service.savePatch({ ...props.data, ...value });
  241. if (response && response?.status === 200) {
  242. onlyMessage('操作成功');
  243. props.reload();
  244. if ((window as any).onTabSaveSuccess) {
  245. (window as any).onTabSaveSuccess(response);
  246. setTimeout(() => window.close(), 300);
  247. }
  248. }
  249. };
  250. return (
  251. <Modal
  252. title={props?.data?.id ? '编辑' : '新增'}
  253. maskClosable={false}
  254. visible
  255. onCancel={props.close}
  256. width={700}
  257. footer={[
  258. <Button key={1} onClick={props.close}>
  259. 取消
  260. </Button>,
  261. <Button
  262. type="primary"
  263. key={2}
  264. onClick={() => {
  265. save();
  266. }}
  267. disabled={props.data?.id ? !permission.update : !permission.add}
  268. >
  269. 确定
  270. </Button>,
  271. // <Button
  272. // key={3}
  273. // type="primary"
  274. // onClick={() => {
  275. // save(true);
  276. // }}
  277. // disabled={
  278. // props.data?.id
  279. // ? !permission.update && !permission.action
  280. // : !permission.add && !permission.action
  281. // }
  282. // >
  283. // 保存并发布
  284. // </Button>,
  285. ]}
  286. >
  287. <Form form={form} layout="vertical">
  288. <SchemaField schema={schema} />
  289. </Form>
  290. </Modal>
  291. );
  292. };
  293. export default Save;