index.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { useIntl } from 'umi';
  2. import { createForm } from '@formily/core';
  3. import { createSchemaField } from '@formily/react';
  4. import React from 'react';
  5. import * as ICONS from '@ant-design/icons';
  6. import { Form, FormItem, Input } from '@formily/antd';
  7. import type { ISchema } from '@formily/json-schema';
  8. import { service } from '@/pages/system/Role';
  9. import { Modal } from '@/components';
  10. import { onlyMessage } from '@/utils/util';
  11. import { getMenuPathByParams, MENUS_CODE } from '@/utils/menu';
  12. import useHistory from '@/hooks/route/useHistory';
  13. interface Props {
  14. model: 'add' | 'edit' | 'query';
  15. close: () => void;
  16. }
  17. const Save = (props: Props) => {
  18. const { model } = props;
  19. const intl = useIntl();
  20. const history = useHistory();
  21. const form = createForm({
  22. validateFirst: true,
  23. });
  24. const SchemaField = createSchemaField({
  25. components: {
  26. FormItem,
  27. Input,
  28. },
  29. scope: {
  30. icon(name: any) {
  31. return React.createElement(ICONS[name]);
  32. },
  33. },
  34. });
  35. const schema: ISchema = {
  36. type: 'object',
  37. properties: {
  38. name: {
  39. title: intl.formatMessage({
  40. id: 'pages.table.name',
  41. defaultMessage: '角色名称',
  42. }),
  43. type: 'string',
  44. 'x-decorator': 'FormItem',
  45. 'x-component': 'Input',
  46. 'x-decorator-props': {},
  47. name: 'name',
  48. required: true,
  49. 'x-component-props': {
  50. placeholder: '请输入角色名称',
  51. },
  52. 'x-validator': [
  53. {
  54. max: 64,
  55. message: '最多可输入64个字符',
  56. },
  57. {
  58. required: true,
  59. message: '请输入名称',
  60. },
  61. ],
  62. },
  63. description: {
  64. type: 'string',
  65. title: intl.formatMessage({
  66. id: 'pages.table.describe',
  67. defaultMessage: '描述',
  68. }),
  69. 'x-decorator': 'FormItem',
  70. 'x-component': 'Input.TextArea',
  71. 'x-component-props': {
  72. checkStrength: true,
  73. placeholder: '请输入说明',
  74. },
  75. 'x-decorator-props': {},
  76. name: 'password',
  77. required: false,
  78. 'x-validator': [
  79. {
  80. max: 200,
  81. message: '最多可输入200个字符',
  82. },
  83. ],
  84. },
  85. },
  86. };
  87. const save = async () => {
  88. const value = await form.submit<RoleItem>();
  89. const response: any = await service.save(value);
  90. if (response.status === 200) {
  91. onlyMessage(
  92. intl.formatMessage({
  93. id: 'pages.data.option.success',
  94. defaultMessage: '操作成功',
  95. }),
  96. );
  97. if ((window as any).onTabSaveSuccess) {
  98. (window as any).onTabSaveSuccess(response.result);
  99. setTimeout(() => window.close(), 300);
  100. } else {
  101. history.push(
  102. `${getMenuPathByParams(MENUS_CODE['system/Role/Detail'], response.result.id)}`,
  103. );
  104. }
  105. props.close();
  106. } else {
  107. onlyMessage('操作失败!', 'error');
  108. }
  109. };
  110. return (
  111. <Modal
  112. title={intl.formatMessage({
  113. id: `pages.data.option.${model}`,
  114. defaultMessage: '编辑',
  115. })}
  116. maskClosable={false}
  117. visible={model !== 'query'}
  118. onCancel={props.close}
  119. onOk={save}
  120. width="35vw"
  121. permissionCode={'system/Role'}
  122. permission={['add']}
  123. >
  124. <Form form={form} layout="vertical">
  125. <SchemaField schema={schema} />
  126. </Form>
  127. </Modal>
  128. );
  129. };
  130. export default Save;