AddFunc.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import React, {
  2. useState, useEffect, useContext
  3. } from 'react';
  4. import { Modal, Form, Input, Select, Button, Switch, TreeSelect, message, Space } from 'antd';
  5. import apiObj from '@api/index';
  6. import utils from '@utils/index'
  7. import FuncContext from './func-context';
  8. import { useSelector } from 'react-redux'
  9. import Item from 'antd/lib/list/Item';
  10. const { post, api, xPost } = apiObj;
  11. const { Option, OptGroup } = Select;
  12. const { organizationData } = utils;
  13. const { SHOW_PARENT } = TreeSelect;
  14. const { TextArea } = Input;
  15. function AddUser(props) {
  16. useEffect(() => {
  17. getHospitalTree();
  18. }, []);
  19. const [form] = Form.useForm();
  20. const { type,id,detail } = useContext(FuncContext);
  21. const [treeData, setTreeData] = useState([]);
  22. const [treeFunc, setTreeFunc] = useState([]);
  23. const staticInfo = useSelector(state => {
  24. return state.staticInfo;
  25. });
  26. const { titleList } = staticInfo;
  27. const initialValues = detail
  28. //所属系统
  29. function getHospitalTree() {
  30. xPost(api.getUserHospitals).then((res) => {
  31. if (res.data.code === 200) {
  32. const data = res.data.data.software
  33. const treeFunc = organizationData(data)
  34. setTreeFunc(treeFunc)
  35. } else {
  36. message.warning(res.data.msg || '获取医院列表失败');
  37. }
  38. })
  39. }
  40. function organizationData(arr) {
  41. arr.forEach(item => {
  42. item.value = item.id
  43. item.title = item.name
  44. })
  45. return arr
  46. }
  47. function addAttr(data) {
  48. for (var j = 0; j < data.length; j++) {
  49. data[j].title = data[j].name //添加title属性
  50. data[j].key = data[j].code //添加key属性
  51. if (data[j].children.length > 0) {
  52. addAttr(data[j].children)
  53. }
  54. }
  55. return data
  56. }
  57. function swichChange(val) {
  58. form.setFieldsValue({ status: val ? 1 : 0 })
  59. }
  60. const onChangeRloe = value => {
  61. form.setFieldsValue({ roles: value })
  62. };
  63. const onFinish = values => {
  64. const formData = form.getFieldValue()
  65. let params = values
  66. params.parentId = formData.parentId
  67. if (type == 2) {
  68. params.id = id
  69. updateMenu(params)
  70. } else {
  71. addMenu(params)
  72. }
  73. };
  74. function addMenu(param) {
  75. post(api.addMenu, param).then((res) => {
  76. if (res.data.code === 200) {
  77. props.userChange()
  78. message.success(res.data.message);
  79. form.resetFields();
  80. } else {
  81. message.error(res.data.message);
  82. }
  83. })
  84. }
  85. function updateMenu(param) {
  86. post(api.updateMenu, param).then((res) => {
  87. if (res.data.code === 200) {
  88. props.userChange()
  89. message.success(res.data.message);
  90. form.resetFields();
  91. } else {
  92. message.error(res.data.message);
  93. }
  94. })
  95. }
  96. function cancel() {
  97. props.userChange()
  98. }
  99. return (
  100. <>
  101. <Form
  102. labelCol={{ span: 6 }}
  103. wrapperCol={{ span: 16 }}
  104. form={form}
  105. name="register"
  106. onFinish={onFinish}
  107. initialValues={initialValues}
  108. >
  109. <Form.Item
  110. name="softwares"
  111. label="所属系统"
  112. rules={[
  113. {
  114. required: true,
  115. message: '请选择所属系统',
  116. },
  117. ]}
  118. >
  119. <TreeSelect
  120. showSearch={false}
  121. treeData={treeFunc}
  122. treeCheckable
  123. onChange={onChangeRloe}
  124. showCheckedStrategy={SHOW_PARENT}
  125. placeholder="请选择所属系统"
  126. style={{ width: '100%' }}
  127. />
  128. </Form.Item>
  129. <Form.Item
  130. name="parentName"
  131. label="上级菜单"
  132. >
  133. <Input disabled />
  134. </Form.Item>
  135. <Form.Item
  136. name="name"
  137. label="功能名称"
  138. rules={[
  139. {
  140. required: true,
  141. message: '请填写功能名称',
  142. },
  143. ]}
  144. >
  145. <Input placeholder="请填写功能名称" />
  146. </Form.Item>
  147. <Form.Item
  148. name="type"
  149. label="类型"
  150. rules={[
  151. {
  152. required: true,
  153. message: '请选择类型',
  154. }
  155. ]}
  156. >
  157. <Select
  158. allowClear
  159. >
  160. <Option value="0">目录</Option>
  161. <Option value="1">菜单</Option>
  162. <Option value="2">按钮</Option>
  163. <Option value="3">功能</Option>
  164. </Select>
  165. </Form.Item>
  166. <Form.Item
  167. name="permissions"
  168. label="链接地址"
  169. >
  170. <Input placeholder="请填写链接地址" />
  171. </Form.Item>
  172. <Form.Item
  173. name="code"
  174. label="权限标识"
  175. rules={[
  176. {
  177. required: true,
  178. message: '请填写权限标识',
  179. }
  180. ]}
  181. >
  182. <Input placeholder="请填写权限标识" />
  183. </Form.Item>
  184. <Form.Item
  185. name="orderNo"
  186. label="排序"
  187. >
  188. <Input placeholder="大于0的整数" />
  189. </Form.Item>
  190. <Form.Item
  191. name="menuDescribe"
  192. label="功能描述"
  193. >
  194. <TextArea rows={4} />
  195. </Form.Item>
  196. <Form.Item
  197. name="remark"
  198. label="备注"
  199. >
  200. <TextArea rows={4} />
  201. </Form.Item>
  202. <Form.Item
  203. name="status"
  204. valuePropName="checked"
  205. label="当前状态"
  206. rules={[{ required: true, message: '请选择状态' }]}
  207. >
  208. <Switch onChange={swichChange} />
  209. </Form.Item>
  210. <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
  211. <Space size="middle">
  212. <Button htmlType="button" onClick={e => cancel()}>
  213. 取消
  214. </Button>
  215. <Button type="primary" htmlType="submit">
  216. 保存
  217. </Button>
  218. </Space>
  219. </Form.Item>
  220. </Form>
  221. </>
  222. );
  223. }
  224. export default AddUser;