modalApp.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { useAjax } from "@/Hook/useAjax";
  2. import { addAppApi, updateAppApi } from "@/services/iaaSystem/application";
  3. import { MinusCircleOutlined, PlusOutlined } from "@ant-design/icons";
  4. import { Button, Form, message, Modal, Select, Space, Switch } from "antd";
  5. import { DefaultOptionType } from "antd/lib/select";
  6. import React from "react"
  7. interface Props {
  8. appAllList: DefaultOptionType[]
  9. userList: DefaultOptionType[]
  10. apptypeEnum: { [x: string]: string }
  11. onChange?: () => void
  12. visible?: boolean
  13. onClose?: () => void
  14. initialValues?: any
  15. }
  16. /**
  17. * 新增修改应用权限
  18. * @returns
  19. */
  20. const ModalApp: React.FC<Props> = ({ appAllList, userList, apptypeEnum, onChange, visible, onClose, initialValues }) => {
  21. /*******************************/
  22. const [form] = Form.useForm();
  23. const authMemberAppList = Form.useWatch('authMemberAppList', form)
  24. const addApp = useAjax((params) => addAppApi(params))
  25. const updateApp = useAjax((params) => updateAppApi(params))
  26. /*******************************/
  27. const handleOk = async () => {
  28. form.submit()
  29. let data = await form.validateFields()
  30. if (initialValues?.id) {
  31. data.id = initialValues?.id
  32. updateApp.run(data).then(res => {
  33. if (res?.data) {
  34. message.success('修改成功')
  35. onChange && onChange()
  36. }
  37. })
  38. } else {
  39. addApp.run(data).then(res => {
  40. if (res?.data) {
  41. message.success('新增成功')
  42. onChange && onChange()
  43. }
  44. })
  45. }
  46. }
  47. return <Modal
  48. title={<strong>{initialValues?.id ? `修改 ${initialValues?.userName} 应用权限` : '新增应用权限'}</strong>}
  49. open={visible}
  50. onCancel={onClose}
  51. onOk={handleOk}
  52. confirmLoading={addApp.loading || updateApp.loading}
  53. width={600}
  54. >
  55. <Form
  56. name="basicApp"
  57. layout="vertical"
  58. form={form}
  59. autoComplete="off"
  60. colon={false}
  61. initialValues={initialValues ? { ...initialValues } : { authMemberAppList: [{}] }}
  62. >
  63. {initialValues?.id ? <>
  64. <Form.Item
  65. label={<strong>应用</strong>}
  66. name={'iaaAppId'}
  67. rules={[{ required: true, message: '请选选择应用' }]}
  68. >
  69. <Select
  70. showSearch
  71. allowClear
  72. placeholder="选择应用"
  73. filterOption={(input, option) =>
  74. (option?.label as any)?.toLowerCase().indexOf(input.toLowerCase()) >= 0
  75. }
  76. options={appAllList}
  77. />
  78. </Form.Item>
  79. <Form.Item
  80. label={<strong>是否包含自然量</strong>}
  81. name={'defaultAgent'}
  82. valuePropName="checked"
  83. initialValue={false}
  84. >
  85. <Switch checkedChildren="是" unCheckedChildren="否" />
  86. </Form.Item>
  87. </> : <>
  88. <Form.Item label={<strong>用户</strong>} name="userIdList" rules={[{ required: true, message: '请选择用户!' }]}>
  89. <Select
  90. mode="multiple"
  91. allowClear
  92. placeholder="请选择用户"
  93. options={userList}
  94. filterOption={(input, option) =>
  95. (option?.label as any)?.toLowerCase().indexOf(input.toLowerCase()) >= 0
  96. }
  97. />
  98. </Form.Item>
  99. <Form.List name="authMemberAppList">
  100. {(fields, { add, remove }) => {
  101. return <>
  102. {fields.map(({ key, name, ...restField }) => (
  103. <Space key={key} style={{ display: 'flex', marginBottom: 8, width: '100%' }} align="start">
  104. <Form.Item
  105. {...restField}
  106. label={<strong>应用类型</strong>}
  107. name={[name, 'appType']}
  108. rules={[{ required: true, message: '请选选择应用' }]}
  109. >
  110. <Select
  111. style={{ width: 130 }}
  112. showSearch
  113. allowClear
  114. placeholder="选择应用类型"
  115. filterOption={(input, option) =>
  116. (option?.label as any)?.toLowerCase().indexOf(input.toLowerCase()) >= 0
  117. }
  118. options={Object.keys(apptypeEnum).map(key => ({ label: apptypeEnum[key], value: key }))}
  119. />
  120. </Form.Item>
  121. <Form.Item
  122. {...restField}
  123. label={<strong>应用</strong>}
  124. name={[name, 'iaaAppIdList']}
  125. rules={[{ required: true, message: '请选选择应用' }]}
  126. >
  127. <Select
  128. style={{ width: 280 }}
  129. showSearch
  130. allowClear
  131. placeholder="选择应用"
  132. mode="multiple"
  133. filterOption={(input, option) =>
  134. (option?.label as any)?.toLowerCase().indexOf(input.toLowerCase()) >= 0
  135. }
  136. options={appAllList?.filter(item => authMemberAppList?.[key]?.appType ? item.appType === authMemberAppList[key]?.appType : true)}
  137. />
  138. </Form.Item>
  139. <Form.Item
  140. {...restField}
  141. label={<strong>是否包含自然量</strong>}
  142. name={[name, 'defaultAgent']}
  143. valuePropName="checked"
  144. initialValue={false}
  145. >
  146. <Switch checkedChildren="是" unCheckedChildren="否" />
  147. </Form.Item>
  148. <MinusCircleOutlined style={{ color: 'red' }} onClick={() => remove(name)} />
  149. </Space>
  150. ))}
  151. <Form.Item>
  152. <Button type="dashed" onClick={() => add()} block icon={<PlusOutlined />}>
  153. 增加
  154. </Button>
  155. </Form.Item>
  156. </>
  157. }}
  158. </Form.List>
  159. </>}
  160. </Form>
  161. </Modal>
  162. }
  163. export default React.memo(ModalApp)