save.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. import { createForm, onFieldValueChange } from '@formily/core';
  2. import type { Field } from '@formily/core';
  3. import { createSchemaField } from '@formily/react';
  4. import {
  5. Checkbox,
  6. Form,
  7. FormGrid,
  8. FormItem,
  9. Input,
  10. NumberPicker,
  11. Password,
  12. Radio,
  13. Select,
  14. Switch,
  15. TreeSelect,
  16. } from '@formily/antd';
  17. import { message, Modal } from 'antd';
  18. import React, { useCallback, useEffect, useMemo, useState } from 'react';
  19. import * as ICONS from '@ant-design/icons';
  20. import { PlusOutlined } from '@ant-design/icons';
  21. import type { ISchema } from '@formily/json-schema';
  22. import { PermissionButton } from '@/components';
  23. import usePermissions from '@/hooks/permission';
  24. import { action } from '@formily/reactive';
  25. import type { Response } from '@/utils/typings';
  26. import { service } from '@/pages/system/Platforms/index';
  27. import { randomString } from '@/utils/util';
  28. interface SaveProps {
  29. visible: boolean;
  30. type: 'save' | 'edit';
  31. data?: any;
  32. onReload?: () => void;
  33. onCancel?: () => void;
  34. }
  35. export default (props: SaveProps) => {
  36. const [loading, setLoading] = useState(false);
  37. const { permission: deptPermission } = usePermissions('system/Department');
  38. const SchemaField = createSchemaField({
  39. components: {
  40. Checkbox,
  41. Form,
  42. FormGrid,
  43. FormItem,
  44. Input,
  45. NumberPicker,
  46. Select,
  47. Switch,
  48. TreeSelect,
  49. Password,
  50. Radio,
  51. },
  52. scope: {
  53. icon(name: any) {
  54. return React.createElement(ICONS[name]);
  55. },
  56. },
  57. });
  58. const getRole = () => service.queryRoleList();
  59. const useAsyncDataSource = (api: any) => (field: Field) => {
  60. field.loading = true;
  61. api(field).then(
  62. action.bound!((resp: Response<any>) => {
  63. field.dataSource = resp.result?.map((item: Record<string, unknown>) => ({
  64. ...item,
  65. label: item.name,
  66. value: item.id,
  67. }));
  68. field.loading = false;
  69. }),
  70. );
  71. };
  72. const form = useMemo(
  73. () =>
  74. createForm({
  75. validateFirst: false,
  76. effects() {
  77. onFieldValueChange('enableOAuth2', (field) => {
  78. form.setFieldState('redirectUrl', (state) => {
  79. state.display = field.value ? 'visible' : 'none';
  80. });
  81. });
  82. },
  83. }),
  84. [props.data],
  85. );
  86. const getDetail = async (id: string) => {
  87. const resp = await service.getDetail(id);
  88. if (resp.status === 200) {
  89. form.setValues({
  90. ...resp.result,
  91. confirm_password: resp.result.password,
  92. });
  93. }
  94. };
  95. useEffect(() => {
  96. if (props.visible) {
  97. if (props.type === 'edit') {
  98. getDetail(props.data.id);
  99. } else {
  100. form.setValues({
  101. enableOAuth2: false,
  102. id: randomString(16),
  103. secureKey: randomString(),
  104. });
  105. }
  106. } else {
  107. if (form) {
  108. form.reset();
  109. }
  110. }
  111. }, [props.type, props.visible]);
  112. const schema: ISchema = {
  113. type: 'object',
  114. properties: {
  115. grid: {
  116. type: 'void',
  117. 'x-component': 'FormGrid',
  118. 'x-component-props': {
  119. maxColumns: 2,
  120. minColumns: 1,
  121. columnGap: 12,
  122. },
  123. properties: {
  124. name: {
  125. type: 'string',
  126. title: '名称',
  127. 'x-decorator': 'FormItem',
  128. 'x-component': 'Input',
  129. 'x-component-props': {
  130. placeholder: '请输入名称',
  131. },
  132. 'x-decorator-props': {
  133. gridSpan: 2,
  134. },
  135. required: true,
  136. 'x-validator': [
  137. {
  138. max: 64,
  139. message: '最多可输入64个字符',
  140. },
  141. {
  142. required: true,
  143. message: '请输入名称',
  144. },
  145. ],
  146. },
  147. id: {
  148. type: 'string',
  149. title: 'clientId',
  150. 'x-decorator': 'FormItem',
  151. 'x-component': 'Input',
  152. 'x-component-props': {
  153. disabled: true,
  154. },
  155. 'x-decorator-props': {
  156. gridSpan: 1,
  157. },
  158. required: true,
  159. },
  160. secureKey: {
  161. type: 'string',
  162. title: 'secureKey',
  163. 'x-decorator': 'FormItem',
  164. 'x-component': 'Input',
  165. 'x-component-props': {
  166. placeholder: '请输入secureKey',
  167. },
  168. 'x-decorator-props': {
  169. gridSpan: 1,
  170. },
  171. required: true,
  172. 'x-validator': [
  173. {
  174. max: 64,
  175. message: '最多可输入64个字符',
  176. },
  177. {
  178. required: true,
  179. message: '请输入secureKey',
  180. },
  181. ],
  182. },
  183. username: {
  184. type: 'string',
  185. title: '用户名',
  186. 'x-decorator': 'FormItem',
  187. 'x-component': 'Input',
  188. 'x-component-props': {
  189. placeholder: '请输入用户名',
  190. },
  191. 'x-decorator-props': {
  192. gridSpan: 2,
  193. },
  194. required: true,
  195. 'x-validator': [
  196. {
  197. max: 64,
  198. message: '最多可输入64个字符',
  199. },
  200. {
  201. required: true,
  202. message: '请输入用户名',
  203. },
  204. ],
  205. },
  206. password: {
  207. type: 'string',
  208. title: '密码',
  209. required: true,
  210. 'x-decorator': 'FormItem',
  211. 'x-component': 'Password',
  212. 'x-component-props': {
  213. placeholder: '请输入密码',
  214. checkStrength: true,
  215. },
  216. 'x-visible': !props.data,
  217. 'x-decorator-props': {
  218. gridSpan: 1,
  219. },
  220. 'x-reactions': [
  221. {
  222. dependencies: ['.confirm_password'],
  223. fulfill: {
  224. state: {
  225. selfErrors:
  226. '{{$deps[0] && $self.value && $self.value !== $deps[0] ? "确认密码不匹配" : ""}}',
  227. },
  228. },
  229. },
  230. ],
  231. 'x-validator': [
  232. {
  233. triggerType: 'onBlur',
  234. validator: (value: string) => {
  235. return new Promise((resolve) => {
  236. service
  237. .validateField('password', value)
  238. .then((resp) => {
  239. if (resp.status === 200) {
  240. if (resp.result.passed) {
  241. resolve('');
  242. } else {
  243. resolve(resp.result.reason);
  244. }
  245. }
  246. resolve('');
  247. })
  248. .catch(() => {
  249. return '验证失败!';
  250. });
  251. });
  252. },
  253. },
  254. {
  255. required: true,
  256. message: '请输入密码',
  257. },
  258. ],
  259. },
  260. confirm_password: {
  261. type: 'string',
  262. title: '确认密码',
  263. required: true,
  264. 'x-decorator': 'FormItem',
  265. 'x-component': 'Password',
  266. 'x-component-props': {
  267. placeholder: '请再次输入密码',
  268. checkStrength: true,
  269. },
  270. 'x-visible': !props.data,
  271. 'x-decorator-props': {
  272. gridSpan: 1,
  273. },
  274. 'x-reactions': [
  275. {
  276. dependencies: ['.password'],
  277. fulfill: {
  278. state: {
  279. selfErrors:
  280. '{{$deps[0] && $self.value && $self.value !== $deps[0] ? "确认密码不匹配" : ""}}',
  281. },
  282. },
  283. },
  284. ],
  285. 'x-validator': [
  286. {
  287. max: 64,
  288. message: '最多可输入64个字符',
  289. },
  290. {
  291. required: true,
  292. message: '请输入确认密码',
  293. },
  294. ],
  295. },
  296. roleIdList: {
  297. type: 'string',
  298. title: '角色',
  299. required: true,
  300. 'x-decorator': 'FormItem',
  301. 'x-component': 'Select',
  302. 'x-component-props': {
  303. mode: 'multiple',
  304. showArrow: true,
  305. placeholder: '请选择角色',
  306. filterOption: (input: string, option: any) =>
  307. option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0,
  308. },
  309. 'x-decorator-props': {
  310. gridSpan: 2,
  311. addonAfter: (
  312. <PermissionButton
  313. type="link"
  314. style={{ padding: 0 }}
  315. isPermission={deptPermission.add}
  316. onClick={() => {
  317. const tab: any = window.open(`${origin}/#/system/role?save=true`);
  318. tab!.onTabSaveSuccess = (value: any) => {
  319. form.setFieldState('roleIdList', async (state) => {
  320. state.dataSource = await getRole().then((resp: any) =>
  321. resp.result?.map((item: Record<string, unknown>) => ({
  322. ...item,
  323. label: item.name,
  324. value: item.id,
  325. })),
  326. );
  327. state.value = [...(state.value || []), value.id];
  328. });
  329. };
  330. }}
  331. >
  332. <PlusOutlined />
  333. </PermissionButton>
  334. ),
  335. },
  336. 'x-reactions': ['{{useAsyncDataSource(getRole)}}'],
  337. 'x-validator': [
  338. {
  339. required: true,
  340. message: '请选择角色',
  341. },
  342. ],
  343. },
  344. enableOAuth2: {
  345. type: 'boolean',
  346. title: '开启OAth2',
  347. required: true,
  348. 'x-decorator': 'FormItem',
  349. 'x-component': 'Radio.Group',
  350. 'x-component-props': {
  351. optionType: 'button',
  352. buttonStyle: 'solid',
  353. },
  354. 'x-decorator-props': {
  355. gridSpan: 2,
  356. tooltip: '免密授权',
  357. },
  358. enum: [
  359. { label: '启用', value: true },
  360. { label: '关闭', value: false },
  361. ],
  362. },
  363. redirectUrl: {
  364. type: 'string',
  365. title: 'redirectUrl',
  366. required: true,
  367. 'x-decorator': 'FormItem',
  368. 'x-component': 'Input',
  369. 'x-component-props': {
  370. placeholder: '请输入redirectUrl',
  371. },
  372. 'x-hidden': true,
  373. 'x-decorator-props': {
  374. gridSpan: 2,
  375. tooltip: '授权后自动跳转的页面地址',
  376. },
  377. 'x-validator': [
  378. {
  379. max: 64,
  380. message: '最多可输入64个字符',
  381. },
  382. {
  383. required: true,
  384. message: '请输入redirectUrl',
  385. },
  386. ],
  387. },
  388. ipWhiteList: {
  389. type: 'string',
  390. title: 'IP白名单',
  391. 'x-decorator': 'FormItem',
  392. 'x-component': 'Input.TextArea',
  393. 'x-decorator-props': {
  394. gridSpan: 2,
  395. },
  396. 'x-component-props': {
  397. placeholder: '请输入IP白名单,多个地址回车分隔,不填默认均可访问',
  398. rows: 3,
  399. },
  400. },
  401. description: {
  402. type: 'string',
  403. title: '说明',
  404. 'x-decorator': 'FormItem',
  405. 'x-component': 'Input.TextArea',
  406. 'x-decorator-props': {
  407. gridSpan: 2,
  408. },
  409. 'x-component-props': {
  410. rows: 3,
  411. placeholder: '请输入说明',
  412. showCount: true,
  413. maxLength: 200,
  414. },
  415. },
  416. },
  417. },
  418. },
  419. };
  420. /**
  421. * 关闭Modal
  422. * @param type 是否需要刷新外部table数据
  423. * @param id 传递上级部门id,用于table展开父节点
  424. */
  425. const modalClose = () => {
  426. if (props.onCancel) {
  427. props.onCancel();
  428. }
  429. };
  430. const saveData = useCallback(async () => {
  431. // setLoading(true)
  432. const data: any = await form.submit();
  433. if (data) {
  434. setLoading(true);
  435. const resp: any = props.type === 'edit' ? await service.edit(data) : await service.save(data);
  436. setLoading(false);
  437. if (resp.status === 200) {
  438. if (props.onReload) {
  439. props.onReload();
  440. }
  441. modalClose();
  442. message.success('操作成功');
  443. }
  444. }
  445. }, [props.type]);
  446. return (
  447. <Modal
  448. maskClosable={false}
  449. visible={props.visible}
  450. destroyOnClose={true}
  451. confirmLoading={loading}
  452. onOk={saveData}
  453. onCancel={modalClose}
  454. width={880}
  455. title={props.data && props.data.id ? '编辑' : '新增'}
  456. >
  457. <Form form={form} layout={'vertical'}>
  458. <SchemaField schema={schema} scope={{ useAsyncDataSource, getRole }} />
  459. </Form>
  460. </Modal>
  461. );
  462. };