modal.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import React from 'react';
  2. import KCModal from '@/components/KCModal';
  3. import KCProSelect from '@/components/KCProSelect';
  4. import { roleManageModelState, Dispatch } from 'umi';
  5. import { ProFormText, ProFormTextArea } from '@ant-design/pro-form';
  6. import { getHospList, getShareHospList } from '@/service/hospList';
  7. import { AddUsersDataType, getYoushuUsers } from '@/service/user';
  8. import { TableActType } from '..';
  9. import { Form } from 'antd';
  10. import MenuEditer from '@/pages/platform/components/menuEditer/menu';
  11. import UserEditer from '@/pages/platform/components/usersEditer';
  12. interface ActModalProps extends roleManageModelState {
  13. dispatch: Dispatch | undefined;
  14. }
  15. const ActModal: React.FC<ActModalProps> = ({ dispatch, isShowModal, tableAct, currentEdit, hospId }) => {
  16. const setInitialValues = () => {
  17. if (tableAct === TableActType.EDIT) {
  18. return { ...currentEdit };
  19. }
  20. if (tableAct === TableActType.ADD) {
  21. return { hospId: hospId };
  22. }
  23. if (tableAct === TableActType.EDITMENU || tableAct === TableActType.EDITRELAUSER) {
  24. return { ...currentEdit };
  25. }
  26. };
  27. const initialValues = setInitialValues(); // 在表单渲染之前设置初始值
  28. const onVisibleChangeHandle = (bool: boolean) => {
  29. if (!bool) {
  30. dispatch &&
  31. dispatch({
  32. type: 'roleManageModel/cancelTableAct',
  33. });
  34. }
  35. };
  36. const onFinishHandle = (data: any & AddUsersDataType) => {
  37. console.log({initialValues});
  38. if (tableAct === TableActType.ADD) {
  39. dispatch &&
  40. dispatch({
  41. type: 'roleManageModel/postAddData',
  42. payload: data, // 使用合并后的数据
  43. });
  44. }
  45. if (tableAct === TableActType.EDIT || tableAct === TableActType.EDITMENU || tableAct === TableActType.EDITRELAUSER) {
  46. dispatch &&
  47. dispatch({
  48. type: 'roleManageModel/postEditData',
  49. payload: data, // 使用合并后的数据
  50. });
  51. }
  52. return true;
  53. };
  54. const setModalTitle = () => {
  55. if (tableAct === TableActType.EDIT) {
  56. return '编辑角色';
  57. }
  58. if (tableAct === TableActType.ADD) {
  59. return '新增角色';
  60. }
  61. if (tableAct === TableActType.EDITMENU) {
  62. return `绑定菜单(${currentEdit?.roleName})`;
  63. }
  64. if (tableAct === TableActType.EDITRELAUSER) {
  65. return `绑定人员(${currentEdit?.roleName})`;
  66. }
  67. };
  68. return (
  69. <KCModal
  70. visible={isShowModal}
  71. onVisibleChange={onVisibleChangeHandle}
  72. layout="horizontal"
  73. width={500}
  74. initialValues={initialValues}
  75. title={setModalTitle()}
  76. labelCol={{
  77. span: 5,
  78. }}
  79. onFinish={async (data) => onFinishHandle({...data})}
  80. >
  81. {tableAct === TableActType.EDITMENU && (
  82. <Form.Item name="bindMenuIds">
  83. <MenuEditer noAction={true} hospId={currentEdit?.hospId} />
  84. </Form.Item>
  85. )}
  86. {tableAct === TableActType.EDITRELAUSER && (
  87. <Form.Item name="bindUserIds">
  88. <UserEditer total={currentEdit ? currentEdit.allUsers : []} noAction={true} />
  89. </Form.Item>
  90. )}
  91. {(tableAct === TableActType.ADD || tableAct === TableActType.EDIT) && (
  92. <>
  93. <KCProSelect
  94. label="选择院区"
  95. width="md"
  96. name="hospId"
  97. disabled
  98. fieldProps={{size:'small'}}
  99. request={async () => {
  100. const hospList = await getShareHospList();
  101. if (hospList) {
  102. return hospList.map((t) => ({
  103. label: t.name,
  104. value: t.id,
  105. }));
  106. }
  107. return [];
  108. }}
  109. />
  110. <ProFormText
  111. width="md"
  112. name="roleName"
  113. label="角色名称"
  114. placeholder="请输入"
  115. rules={[
  116. {
  117. required: true,
  118. message: '请输入角色名称!',
  119. },
  120. ]}
  121. />
  122. <ProFormText
  123. width="md"
  124. name="roleCode"
  125. label="角色编码"
  126. placeholder="请输入"
  127. rules={[
  128. {
  129. required: true,
  130. message: '请输入角色编码!',
  131. },
  132. ]}
  133. />
  134. <KCProSelect
  135. label="关联有数账号"
  136. width="md"
  137. name="youshuUserId"
  138. request={async () => {
  139. const resp = await getYoushuUsers();
  140. if (resp) {
  141. return resp.map((t) => ({
  142. label: t.name,
  143. value: t.id,
  144. }));
  145. }
  146. return [];
  147. }}
  148. />
  149. <ProFormTextArea name="remark" label="备注" placeholder="请输入名称" width="md" fieldProps={{}} />
  150. </>
  151. )}
  152. </KCModal>
  153. );
  154. };
  155. export default ActModal;