index.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import { message, Modal } from 'antd';
  2. import { useIntl } from 'umi';
  3. import { createForm, onFormSubmitStart } from '@formily/core';
  4. import { createSchemaField } from '@formily/react';
  5. import React, { useEffect, useState } from 'react';
  6. import * as ICONS from '@ant-design/icons';
  7. import { ArrayTable, Form, FormItem, Input, Editable } from '@formily/antd';
  8. import type { ISchema } from '@formily/json-schema';
  9. import type { PermissionItem } from '@/pages/system/Permission/typings';
  10. import { service } from '@/pages/system/Permission';
  11. interface Props {
  12. model: 'add' | 'edit' | 'query';
  13. data: Partial<PermissionItem>;
  14. close: () => void;
  15. }
  16. const defaultAction = [
  17. { action: 'query', name: '查询', describe: '查询' },
  18. { action: 'save', name: '保存', describe: '保存' },
  19. { action: 'delete', name: '删除', describe: '删除' },
  20. ];
  21. const Save = (props: Props) => {
  22. const { model } = props;
  23. const intl = useIntl();
  24. const [data, setData] = useState<Partial<PermissionItem>>(props.data);
  25. useEffect(() => {
  26. if (model === 'edit') {
  27. setData(props.data);
  28. } else {
  29. setData({});
  30. }
  31. }, [props.data, props.model]);
  32. const form = createForm({
  33. validateFirst: true,
  34. initialValues: data,
  35. effects: () => {
  36. onFormSubmitStart((formEffect) => {
  37. formEffect.values.actions = formEffect.values.actions?.filter(
  38. (item: any) => Object.keys(item).length > 0,
  39. );
  40. });
  41. },
  42. });
  43. const SchemaField = createSchemaField({
  44. components: {
  45. FormItem,
  46. Input,
  47. ArrayTable,
  48. Editable,
  49. },
  50. scope: {
  51. icon(name: any) {
  52. return React.createElement(ICONS[name]);
  53. },
  54. },
  55. });
  56. const schema: ISchema = {
  57. type: 'object',
  58. properties: {
  59. id: {
  60. title: intl.formatMessage({
  61. id: 'pages.system.permission.id',
  62. defaultMessage: '标识(ID)',
  63. }),
  64. type: 'string',
  65. 'x-decorator': 'FormItem',
  66. 'x-component': 'Input',
  67. name: 'id',
  68. 'x-decorator-props': {
  69. tooltip: <div>标识ID需与代码中的标识ID一致</div>,
  70. },
  71. 'x-validator': [
  72. {
  73. max: 64,
  74. message: '最多可输入64个字符',
  75. },
  76. {
  77. required: true,
  78. message: '请输入标识(ID)',
  79. },
  80. ],
  81. },
  82. name: {
  83. title: intl.formatMessage({
  84. id: 'pages.table.name',
  85. defaultMessage: '名称',
  86. }),
  87. type: 'string',
  88. 'x-decorator': 'FormItem',
  89. 'x-component': 'Input',
  90. name: 'name',
  91. 'x-validator': [
  92. {
  93. max: 64,
  94. message: '最多可输入64个字符',
  95. },
  96. {
  97. required: true,
  98. message: '请输入名称',
  99. },
  100. ],
  101. },
  102. actions: {
  103. type: 'array',
  104. 'x-decorator': 'FormItem',
  105. 'x-component': 'ArrayTable',
  106. default: defaultAction,
  107. title: '操作类型',
  108. items: {
  109. type: 'object',
  110. properties: {
  111. column1: {
  112. type: 'void',
  113. 'x-component': 'ArrayTable.Column',
  114. 'x-component-props': { width: 80, title: '-', align: 'center' },
  115. properties: {
  116. index: {
  117. type: 'void',
  118. 'x-component': 'ArrayTable.Index',
  119. },
  120. },
  121. },
  122. column2: {
  123. type: 'void',
  124. 'x-component': 'ArrayTable.Column',
  125. 'x-component-props': {
  126. width: 200,
  127. title: intl.formatMessage({
  128. id: 'pages.system.permission.addConfigurationType',
  129. defaultMessage: '操作类型',
  130. }),
  131. },
  132. properties: {
  133. action: {
  134. type: 'string',
  135. 'x-decorator': 'Editable',
  136. 'x-component': 'Input',
  137. },
  138. },
  139. },
  140. column3: {
  141. type: 'void',
  142. 'x-component': 'ArrayTable.Column',
  143. 'x-component-props': {
  144. width: 200,
  145. title: intl.formatMessage({
  146. id: 'pages.table.name',
  147. defaultMessage: '名称',
  148. }),
  149. },
  150. properties: {
  151. name: {
  152. type: 'string',
  153. 'x-decorator': 'Editable',
  154. 'x-component': 'Input',
  155. },
  156. },
  157. },
  158. column4: {
  159. type: 'void',
  160. 'x-component': 'ArrayTable.Column',
  161. 'x-component-props': {
  162. width: 200,
  163. title: intl.formatMessage({
  164. id: 'pages.table.describe',
  165. defaultMessage: '描述',
  166. }),
  167. },
  168. properties: {
  169. describe: {
  170. type: 'string',
  171. 'x-decorator': 'Editable',
  172. 'x-component': 'Input',
  173. },
  174. },
  175. },
  176. column5: {
  177. type: 'void',
  178. 'x-component': 'ArrayTable.Column',
  179. 'x-component-props': {
  180. title: intl.formatMessage({
  181. id: 'pages.data.option',
  182. defaultMessage: '操作',
  183. }),
  184. dataIndex: 'operations',
  185. width: 200,
  186. fixed: 'right',
  187. },
  188. properties: {
  189. item: {
  190. type: 'void',
  191. 'x-component': 'FormItem',
  192. properties: {
  193. remove: {
  194. type: 'void',
  195. 'x-component': 'ArrayTable.Remove',
  196. },
  197. },
  198. },
  199. },
  200. },
  201. },
  202. },
  203. properties: {
  204. add: {
  205. type: 'void',
  206. 'x-component': 'ArrayTable.Addition',
  207. title: intl.formatMessage({
  208. id: 'pages.system.permission.add',
  209. defaultMessage: '添加条目',
  210. }),
  211. },
  212. },
  213. },
  214. },
  215. };
  216. const save = async () => {
  217. const value = await form.submit<UserItem>();
  218. const response = await service.update(value);
  219. if (response.status === 200) {
  220. message.success(
  221. intl.formatMessage({
  222. id: 'pages.data.option.success',
  223. defaultMessage: '操作成功',
  224. }),
  225. );
  226. props.close();
  227. } else {
  228. message.error('操作失败!');
  229. }
  230. };
  231. return (
  232. <Modal
  233. title={intl.formatMessage({
  234. id: `pages.data.option.${model}`,
  235. defaultMessage: '编辑',
  236. })}
  237. maskClosable={false}
  238. visible={model !== 'query'}
  239. onCancel={props.close}
  240. onOk={save}
  241. width="1000px"
  242. >
  243. <Form form={form} layout="vertical">
  244. <SchemaField schema={schema} />
  245. </Form>
  246. </Modal>
  247. );
  248. };
  249. export default Save;