AddData.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. import { useEffect, useState, useContext } from 'react';
  2. import { Form, Input, Button, Steps, Tabs, Space, Switch, Breadcrumb, Radio, TreeSelect, Tree, Tag, Card, message } from 'antd';
  3. import './index.less';
  4. import DataContext from './data-context';
  5. import MenuTree from './MenuTree';
  6. import { useSelector } from 'react-redux'
  7. import apiObj from '@api/index';
  8. import utils from '@utils/index'
  9. import backIcon from "@images/back.png";
  10. import DoctorList from "./doctorList"
  11. import Item from 'antd/lib/list/Item';
  12. const { post, api, xPost } = apiObj;
  13. const { SHOW_PARENT } = TreeSelect;
  14. const { organizationData } = utils;
  15. const { Step } = Steps;
  16. const { TabPane } = Tabs;
  17. function AddData(props) {
  18. useEffect(() => {
  19. getCreateRoles()
  20. getHospitalTree()
  21. getOrgList()
  22. if (type == 3) {
  23. setValue(val)
  24. setTags(tag)
  25. } else {
  26. orgList.forEach((it, index) => {
  27. tags[index] = []
  28. setTags([...tags])
  29. })
  30. }
  31. }, []);
  32. const { back, } = props;
  33. const [form] = Form.useForm();
  34. const [value, setValue] = useState([]);
  35. const [key, setKey] = useState();
  36. const [index, setIndex] = useState('0');
  37. const [treeRloe, setTreeRloe] = useState([]);
  38. const [tags, setTags] = useState([[]]);
  39. const [treeData, setTreeData] = useState([]);
  40. const [selectedKeys, setSelectedKeys] = useState([]);
  41. const [orgList, setOrgList] = useState([]);
  42. const [selectedRowKeys, setSelectedRowKeys] = useState([]);
  43. const [autoExpandParent, setAutoExpandParent] = useState(true);
  44. const { save, formData, type, val, tag } = useContext(DataContext);
  45. const staticInfo = useSelector(state => {
  46. return state.staticInfo;
  47. });
  48. const { dataList } = staticInfo;
  49. const initialValues = formData
  50. const validateMessages = {
  51. required: '${label}不能为空',
  52. };
  53. //获取当前用于所属角色
  54. function getCreateRoles() {
  55. const params = {
  56. softwareId: ''
  57. }
  58. xPost(api.getCreateRoles, params).then((res) => {
  59. if (res.data.code === 200) {
  60. const data = res.data.data;
  61. let arr = JSON.parse(JSON.stringify(data).replaceAll(/name/g, 'title').replaceAll(/id/g, 'value'))
  62. setTreeRloe(arr)
  63. }
  64. })
  65. }
  66. //获取当前用户组织
  67. function getHospitalTree() {
  68. post(api.getHospitalTree).then((res) => {
  69. if (res.data.code === 200) {
  70. const data = res.data.data
  71. let arr = structureTreeData(data)
  72. setTreeData(arr)
  73. }
  74. })
  75. }
  76. //数据转换为树形结构所需字段
  77. function structureTreeData(arr) {
  78. arr.forEach(item => {
  79. item.key = item.hospitalId + '-' + item.hospitalName
  80. item.title = item.hospitalName
  81. if (!item.children && item.depts) {
  82. item.depts.forEach(it => {
  83. it.key = item.hospitalId + '-' + it.deptId + '-' + it.deptName
  84. })
  85. item.children = JSON.parse(JSON.stringify(item.depts).replaceAll(/deptName/g, 'title').replaceAll(/deptId/g, 'key'))
  86. return
  87. }
  88. if (item.children) {
  89. structureTreeData(item.children)
  90. }
  91. })
  92. return arr
  93. // let list = []
  94. // arr.forEach(it => {
  95. // it.key = it.hospitalId
  96. // it.title = it.hospitalName
  97. // list.push({
  98. // children: it.depts,
  99. // title: it.hospitalName,
  100. // key: it.parentId + '-' + it.hospitalId + '-' + it.hospitalName
  101. // })
  102. // it.depts.forEach(its => {
  103. // its.key = it.parentId + '-' + it.hospitalId + '-' + its.deptId + '-' + its.deptName
  104. // })
  105. // })
  106. // arr = JSON.parse(JSON.stringify(list).replaceAll(/deptName/g, 'title').replaceAll(/deptId/g, 'key'))
  107. // return arr
  108. }
  109. //获取组织列表
  110. function getOrgList() {
  111. xPost(api.getUserHospitals).then((res) => {
  112. if (res.data.code === 200) {
  113. const data = res.data.data;
  114. const { software } = data;
  115. setOrgList(software);
  116. setKey(software[0].id)
  117. } else {
  118. message.warning(res.data.msg || '获取医院列表失败');
  119. }
  120. }).catch(() => {
  121. message.error("接口出错");
  122. });
  123. }
  124. const onChange = (e, i) => {
  125. let val = value
  126. const formData = form.getFieldsValue();
  127. let arr = formData.softwareVOS;
  128. arr[index].id = key
  129. arr[index].softwareMenuIds = []
  130. arr[index].dataAuthDetails = []
  131. arr[index].selectedRowKeys = []
  132. if (e.target.value != 7) {
  133. arr[index].dataAuthDetails = [{
  134. dataType: e.target.value
  135. }]
  136. }
  137. tags[index] = []
  138. val[i] = e.target.value
  139. selectedRowKeys[index] = []
  140. form.setFieldsValue({
  141. softwareVOS: arr
  142. });
  143. setTags([...tags])
  144. setValue([...val])
  145. setSelectedRowKeys([...selectedRowKeys])
  146. };
  147. // 删除选择标签
  148. function delTag(type, id, i) {
  149. const formData = form.getFieldsValue();
  150. if (type == 3) {
  151. formData.softwareVOS[index].selectedRowKeys.forEach((item, inx) => {
  152. if (item.split('-')[0] == id) {
  153. formData.softwareVOS[index].selectedRowKeys.splice(inx)
  154. }
  155. })
  156. } else if (type == 1) {
  157. formData.softwareVOS[index].softwareMenuIds.forEach((item, inx) => {
  158. if (item.split('-')[0] == id) {
  159. formData.softwareVOS[index].softwareMenuIds.splice(inx)
  160. }
  161. })
  162. } else if (type == 2) {
  163. formData.softwareVOS[index].softwareMenuIds.forEach((item, inx) => {
  164. if (item.split('-')[1] == id) {
  165. formData.softwareVOS[index].softwareMenuIds.splice(inx)
  166. }
  167. })
  168. }
  169. form.setFieldsValue({
  170. softwareVOS: formData.softwareVOS
  171. });
  172. tags[index].splice(i)
  173. setTags([...tags])
  174. console.log(form.getFieldsValue())
  175. }
  176. function callback(key) {
  177. setKey(key.split('-')[0])
  178. setIndex(key.split('-')[1])
  179. }
  180. function treeChange(value) {
  181. const formData = form.getFieldsValue();
  182. let arr = formData.softwareVOS;
  183. arr[index].roles = value
  184. form.setFieldsValue({
  185. softwareVOS: arr
  186. });
  187. console.log(form.getFieldsValue())
  188. }
  189. // 去重
  190. function unique(arr) {
  191. let result = {};
  192. let finalResult = [];
  193. for (let i = 0; i < arr.length; i++) {
  194. result[arr[i].detailId] = arr[i];
  195. }
  196. for (let item in result) {
  197. finalResult.push(result[item]);
  198. }
  199. return finalResult;
  200. }
  201. //医生选中
  202. function checkDoctEvent(selectedRowKeys) {
  203. const formData = form.getFieldsValue();
  204. let arr = formData.softwareVOS[index].dataAuthDetails ? formData.softwareVOS[index].dataAuthDetails : []
  205. selectedRowKeys.forEach(it => {
  206. arr.push({
  207. dataType: 7,
  208. detailId: it.split('-')[0],
  209. detailType: 3
  210. })
  211. });
  212. formData.softwareVOS[index].dataAuthDetails = unique(arr)
  213. formData.softwareVOS[index].selectedRowKeys = selectedRowKeys
  214. form.setFieldsValue({
  215. softwareVOS: formData.softwareVOS
  216. });
  217. getTags()
  218. console.log(form.getFieldsValue())
  219. }
  220. //树形结构选中事件
  221. function checkTreeEvent(idsArr) {
  222. const formData = form.getFieldsValue();
  223. let arr = formData.softwareVOS[index].dataAuthDetails ? formData.softwareVOS[index].dataAuthDetails : []
  224. let result = idsArr.find(ele => ele.split('-').length == 2)
  225. if (result) {
  226. arr.push({
  227. dataType: 7,
  228. detailId: result.split('-')[0],
  229. detailType: 1
  230. })
  231. } else {
  232. idsArr.forEach(it => {
  233. console.log(it)
  234. arr.push({
  235. dataType: 7,
  236. detailId: it.split('-')[1],
  237. detailType: 2
  238. })
  239. })
  240. }
  241. formData.softwareVOS[index].dataAuthDetails = unique(arr)
  242. formData.softwareVOS[index].softwareMenuIds = idsArr
  243. form.setFieldsValue({
  244. softwareVOS: formData.softwareVOS
  245. });
  246. getTags()
  247. }
  248. //获取已选中标签
  249. function getTags() {
  250. const formData = form.getFieldsValue();
  251. let tag = []
  252. let softwareMenuIds = formData.softwareVOS[index].softwareMenuIds
  253. let selectedRowKeys = formData.softwareVOS[index].selectedRowKeys
  254. selectedRowKeys && selectedRowKeys.forEach(it => {
  255. tag.push({
  256. id: it.split('-')[0],
  257. name: it.split('-')[1],
  258. type: 3
  259. })
  260. });
  261. if (softwareMenuIds) {
  262. let result = softwareMenuIds.find(ele => ele.split('-').length == 2)
  263. if (result) {
  264. tag.push({
  265. id: result.split('-')[0],
  266. name: result.split('-')[1],
  267. type: 1
  268. })
  269. } else {
  270. softwareMenuIds.forEach(it => {
  271. tag.push({
  272. id: it.split('-')[1],
  273. name: it.split('-')[2],
  274. type: 2
  275. })
  276. })
  277. }
  278. }
  279. tags[index] = tag
  280. setTags([...tags])
  281. }
  282. const onFinish = value => {
  283. save(form.getFieldsValue())
  284. };
  285. function swichChange(val) {
  286. form.setFieldsValue({ status: val ? 1 : 0 })
  287. }
  288. return (
  289. <>
  290. <Breadcrumb separator="">
  291. <Breadcrumb.Item><img className='back-icon' src={backIcon} onClick={back} alt="返回上一页" /></Breadcrumb.Item>
  292. <Breadcrumb.Item>数据权限</Breadcrumb.Item>
  293. <Breadcrumb.Separator />
  294. <Breadcrumb.Item>{type == 3 ? '修改' : '新增'}数据权限</Breadcrumb.Item>
  295. </Breadcrumb>
  296. <div className='form-center'>
  297. <Form labelCol={{ span: 4 }} wrapperCol={{ span: 18 }} form={form} name="nest-messages" onFinish={onFinish} initialValues={initialValues} validateMessages={validateMessages}>
  298. <Form.Item name="name" label="数据权限名称" rules={[{ required: true }]}>
  299. <Input placeholder="请填写数据权限名称" />
  300. </Form.Item>
  301. <Form.Item label="权限范围" rules={[{ required: true }]} >
  302. <Tabs type="card" onChange={callback}>
  303. {
  304. orgList.map((it, i) => {
  305. return (
  306. <TabPane tab={it.name} key={it.id + "-" + i}>
  307. <Form.Item
  308. key={i}
  309. name={['softwareVOS', i, 'datatype']} noStyle>
  310. <Radio.Group onChange={e => { onChange(e, i) }} className="radio">
  311. <Space direction="vertical">
  312. {dataList.map((Item) => {
  313. return (
  314. <Radio key={i} value={Item.name}>{Item.val}</Radio>
  315. )
  316. })
  317. }
  318. </Space>
  319. </Radio.Group>
  320. </Form.Item>
  321. <Form.Item key={i + "a"} hidden={true} name={['softwareVOS', i, 'id']} noStyle>
  322. <Input />
  323. </Form.Item>
  324. <Form.Item key={i + "b"} hidden={true} name={['softwareVOS', i, 'dataAuthDetails']} noStyle>
  325. <Input />
  326. </Form.Item>
  327. <Form.Item key={i + "c"} style={{ display: value[index] == 7 && it.id == key ? 'block' : 'none' }}>
  328. <Card title="已选中" extra={<a href="#">More</a>} >
  329. {tags[index].map((tag, i) => {
  330. return (
  331. <Tag key={i} closable onClose={e => delTag(tag.type, tag.id, i)}>{tag.name}</Tag>
  332. );
  333. })}
  334. </Card>
  335. </Form.Item>
  336. <Form.Item key={i + "d"} style={{ display: value[index] == 7 && it.id == key ? 'block' : 'none' }}>
  337. <Tabs defaultActiveKey="0">
  338. <TabPane tab="可看医生" key="0">
  339. <Form.Item
  340. key={i}
  341. name={['softwareVOS', i, 'selectedRowKeys']}
  342. >
  343. <DoctorList checkeds={form.getFieldValue().softwareVOS ? form.getFieldValue().softwareVOS[i] : []} checkDoct={(selectedRowKeys) => checkDoctEvent(selectedRowKeys)} />
  344. </Form.Item>
  345. </TabPane>
  346. <TabPane tab="可看科室" key="1">
  347. <Form.Item
  348. key={i}
  349. name={['softwareVOS', i, 'softwareMenuIds']}
  350. >
  351. <MenuTree data={treeData} checkeds={form.getFieldValue().softwareVOS ? form.getFieldValue().softwareVOS[i] : []} checkEv={(checkedKeys) => checkTreeEvent(checkedKeys)} />
  352. </Form.Item>
  353. </TabPane>
  354. </Tabs>
  355. </Form.Item>
  356. </TabPane>
  357. )
  358. })
  359. }
  360. </Tabs>
  361. </Form.Item>
  362. <Form.Item label="所属角色" required >
  363. {
  364. orgList.map((it, i) => {
  365. return (
  366. <Form.Item
  367. style={{ display: i == index ? 'block' : 'none' }}
  368. key={i}
  369. >
  370. <Form.Item style={{ display: i == index ? 'block' : 'none' }} key={i + "b"} hidden={true}
  371. name={['softwareVOS', i, 'roles']} rules={[
  372. {
  373. required: true,
  374. message: '请选择所属角色',
  375. },
  376. ]}>
  377. <TreeSelect
  378. showSearch={false}
  379. treeData={treeRloe}
  380. onChange={treeChange}
  381. maxTagCount={1}
  382. treeCheckable
  383. showCheckedStrategy={SHOW_PARENT}
  384. placeholder="请选择角色"
  385. style={{ width: '100%' }}
  386. />
  387. </Form.Item>
  388. <Form.Item key={i + "a"} hidden={true} name={['softwareVOS', i, 'datatype']} noStyle>
  389. <Input />
  390. </Form.Item>
  391. <Form.Item key={i + "c"} hidden={true} name={['softwareVOS', i, 'id']} noStyle>
  392. <Input />
  393. </Form.Item>
  394. <Form.Item key={i + "d"} hidden={true} name={['softwareVOS', i, 'dataAuthDetails']} noStyle>
  395. <Input />
  396. </Form.Item>
  397. <Form.Item key={i + "e"} hidden={true} name={['softwareVOS', i, 'selectedRowKeys']} noStyle>
  398. <Input />
  399. </Form.Item>
  400. <Form.Item key={i + "f"} hidden={true} name={['softwareVOS', i, 'softwareMenuIds']} noStyle>
  401. <Input />
  402. </Form.Item>
  403. </Form.Item>
  404. )
  405. })
  406. }
  407. </Form.Item>
  408. <Form.Item
  409. name="status"
  410. valuePropName="checked"
  411. label="当前状态"
  412. rules={[{ required: true, message: '请选择状态' }]}
  413. >
  414. <Switch onChange={swichChange} />
  415. </Form.Item>
  416. <Form.Item wrapperCol={{ span: 20, offset: 8 }}>
  417. <Button type="primary" htmlType="submit">
  418. Submit
  419. </Button>
  420. </Form.Item>
  421. </Form>
  422. </div>
  423. </>
  424. )
  425. }
  426. export default AddData;