index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. import React, { useState, useEffect} from 'react';
  2. import { Form, Input, Button, Table, Select, Space, Menu, Dropdown, Modal, message, Row, Col } from 'antd';
  3. import { DownOutlined, PlusOutlined } from '@ant-design/icons';
  4. import AddUser from './addUser'
  5. import '@common/common.less';
  6. import { useSelector } from 'react-redux'
  7. import apiObj from '@api/index';
  8. import UserContext from './user-context';
  9. import { getValueFromEvent } from '@utils/index'
  10. const { post, api, xPost } = apiObj;
  11. const { Option } = Select;
  12. function UserManager() {
  13. useEffect(() => {
  14. getUserPage();
  15. }, []);
  16. const [userList, setUserList] = useState([]);
  17. const [title, setTitle] = useState("");
  18. const [visible, setVisible] = useState(false);
  19. const [userId, setUserId] = useState("");
  20. const [msvisible, setMsvisible] = useState(false);
  21. const [modalType, setModalType] = useState("");
  22. const [type, setType] = useState("");
  23. const [formData, setFormData] = useState(null);
  24. const [roleList, setRoleList] = useState([]);
  25. const [size, setSize] = useState(15);
  26. const [total, setTotal] = useState(0);
  27. const [current, setCurrent] = useState(1);
  28. const [params, setParams] = useState({
  29. pages: 1,
  30. current: 1,
  31. size: 15
  32. });
  33. const [form] = Form.useForm();
  34. const tipText = {
  35. 1: '确定要删除该用户?',
  36. 2: '禁用后该用户将无法登录,确定要禁用该用户?',
  37. 3: '确定要重置该用户密码?',
  38. };
  39. const staticInfo = useSelector(state => {
  40. return state.staticInfo;
  41. });
  42. let list = []
  43. let data = {
  44. pages: 1,
  45. current: 1,
  46. size: size
  47. }
  48. const { statusList } = staticInfo;
  49. //新增弹窗
  50. const showModal = (name, type, flag, userId) => {
  51. setVisible(type);
  52. setTitle(name);
  53. setType(flag)
  54. setUserId(userId)
  55. if (flag == 1) {
  56. setFormData({
  57. status: '1'
  58. })
  59. }
  60. if (flag == 3 || flag == 2) {
  61. getUserById(userId)
  62. }
  63. }
  64. //表格数据
  65. function getUserPage(param) {
  66. post(api.getUserPage, param || params).then((res) => {
  67. if (res.data.code === 200) {
  68. const data = res.data.data;
  69. setUserList(data.records);
  70. setTotal(data.total)
  71. }
  72. })
  73. }
  74. //查看用户
  75. function getUserById(userId) {
  76. xPost(api.getUserById, { userId: userId }).then((res) => {
  77. if (res.data.code === 200) {
  78. const data = res.data.data;
  79. let roles = []
  80. data.roles.forEach(item => {
  81. roles.push(item.roleId)
  82. })
  83. const arr = {
  84. username: data.username,
  85. mobilePhone: data.mobilePhone,
  86. password: data.password,
  87. againpassword: data.password,
  88. name: data.name,
  89. idcard: data.idcard,
  90. addHospitalTreeVO: getHospitals(data.hospitals),
  91. titleId: data.titleId,
  92. jobNo: data.jobNo,
  93. roles: roles,
  94. orderNo: data.orderNo,
  95. status: data.status,
  96. }
  97. setRoleList(data.roles)
  98. setFormData(arr)
  99. }
  100. })
  101. }
  102. // 处理组织结构数据回显
  103. function getHospitals(arr) {
  104. arr.forEach((item, i, array) => {
  105. item.value = item.hospitalId
  106. item.title = item.hospitalName
  107. if (!item.children && item.depts) {
  108. item.depts.forEach(it => {
  109. it.value = item.parentId + '-' + it.deptId
  110. if (it.relation == 1) {
  111. list.push(it.value)
  112. }
  113. })
  114. }
  115. if (item.type != 0 && item.relation == 1) {
  116. list.push(item.value)
  117. console.log(list)
  118. }
  119. if (item.children) {
  120. getHospitals(item.children)
  121. }
  122. })
  123. return list
  124. }
  125. // 禁用/启用接口
  126. function disableUser(userId, status) {
  127. const param = { userId: userId, status: status };
  128. xPost(api.disableUser, param).then((res) => {
  129. if (res.data.code === 200) {
  130. getUserPage();
  131. setMsvisible(false);
  132. message.success((status ? '启用' : '禁用') + "成功");
  133. } else {
  134. message.warning(res.data.message || '操作失败');
  135. }
  136. }).catch(() => {
  137. message.error("接口出错");
  138. });
  139. }
  140. //重置密码
  141. function onResetPsd(userId) {
  142. const param = { userId: userId };
  143. xPost(api.resetPasswordUser, param).then((res) => {
  144. if (res.data.code === 200) {
  145. getUserPage();
  146. message.success("重置成功");
  147. } else {
  148. message.warning(res.data.message || '操作失败');
  149. }
  150. }).catch(() => {
  151. message.error("接口出错");
  152. });
  153. }
  154. //删除
  155. function deleteUser() {
  156. const param = { userId: userId };
  157. xPost(api.deleteUser, param).then((res) => {
  158. if (res.data.code === 200) {
  159. getUserPage();
  160. setMsvisible(false);
  161. message.success("删除成功");
  162. } else {
  163. message.warning(res.data.message || '操作失败');
  164. }
  165. }).catch(() => {
  166. message.error("接口出错");
  167. });
  168. }
  169. function onSizeChange(current, pageSize) {
  170. params.current = current
  171. params.size = pageSize
  172. setSize(pageSize)
  173. setCurrent(current)
  174. setParams(params)
  175. getUserPage()
  176. }
  177. function changePage(page, pageSize) {
  178. params.current = page
  179. params.size = pageSize
  180. setCurrent(page)
  181. setParams(params)
  182. getUserPage()
  183. }
  184. const onFinish = (value) => {
  185. const param = {
  186. ...data,
  187. ...value
  188. }
  189. setCurrent(1)
  190. setParams(param)
  191. getUserPage(param);
  192. };
  193. const onReset = () => {
  194. setCurrent(1)
  195. setParams(data)
  196. form.resetFields();
  197. getUserPage(data);
  198. };
  199. const messageBox = (type, id) => {
  200. setMsvisible(true)
  201. setUserId(id)
  202. setModalType(type)
  203. }
  204. //提示框确认事件
  205. function handleOk() {
  206. if (modalType == 1) {
  207. deleteUser(userId)
  208. } else if (modalType == 2) {
  209. disableUser(userId, 0)
  210. } else if (modalType == 3) {
  211. onResetPsd(userId);
  212. }
  213. }
  214. //提示框取消
  215. function handleCancel() {
  216. setMsvisible(false);
  217. }
  218. function cancel() {
  219. setVisible(false)
  220. setFormData(null)
  221. }
  222. function userChange() {
  223. setVisible(false)
  224. getUserPage();
  225. }
  226. const columns = [
  227. { title: '用户名', dataIndex: 'username', key: 'index' },
  228. { title: '姓名', dataIndex: 'name', key: 'index' },
  229. { title: '所属组织', dataIndex: 'hospitalName', key: 'index' },
  230. { title: '工号', dataIndex: 'jobNo', key: 'index' },
  231. {
  232. title: '状态', dataIndex: 'status', key: 'status', render: (text, record) => (
  233. <Space size="middle">
  234. {record.status == 1 ?
  235. <span className="Enable">启用</span>
  236. :
  237. <span className="Disable">禁用</span>
  238. }
  239. </Space>
  240. )
  241. },
  242. {
  243. title: '操作', dataIndex: 'key', render: (text, record) => (
  244. <Space size="middle">
  245. <a onClick={e => showModal('查看用户', true, 3, record.userId)}>查看</a>
  246. <a onClick={e => showModal('修改用户', true, 2, record.userId)} >修改</a>
  247. <Dropdown overlay={menu.bind(this, record)} record={record}>
  248. <a className="ant-dropdown-link" onClick={e => e.preventDefault()}>
  249. 更多 <DownOutlined />
  250. </a>
  251. </Dropdown>
  252. </Space>
  253. )
  254. }
  255. ]
  256. const menu = (record) => {
  257. return (
  258. <Menu>
  259. <Menu.Item key="0" className="menuItem" >
  260. <a onClick={() => messageBox(3, record.userId)}>
  261. 重置密码
  262. </a>
  263. </Menu.Item>
  264. <Menu.Item key="1" className="menuItem" >
  265. {record.status === '1' ? (<a onClick={() => messageBox(2, record.userId)}>禁用</a>) : (<a onClick={() => disableUser(record.userId, 1)}>启用</a>)}
  266. </Menu.Item>
  267. <Menu.Item key="2" className="menuItem" >
  268. <a onClick={() => messageBox(1, record.userId)}>
  269. 删除
  270. </a>
  271. </Menu.Item>
  272. </Menu>
  273. )
  274. }
  275. return (
  276. <div className="wrapper">
  277. <div className="filter-box">
  278. <Form
  279. form={form}
  280. name="normal_login"
  281. onFinish={onFinish}
  282. initialValues={{ status: '' }}
  283. >
  284. <Row gutter={24}>
  285. <Col span={5} key={0}>
  286. <Form.Item label="用户名" name="username" getValueFromEvent={getValueFromEvent}>
  287. <Input placeholder="用户名" autoComplete='off'/>
  288. </Form.Item>
  289. </Col>
  290. <Col span={5} key={1}>
  291. <Form.Item label="姓名" name="name" getValueFromEvent={getValueFromEvent}>
  292. <Input placeholder="姓名" autoComplete='off'/>
  293. </Form.Item>
  294. </Col>
  295. <Col span={5} key={2}>
  296. <Form.Item name="status" label="当前状态">
  297. <Select
  298. allowClear
  299. >
  300. {statusList.map((item) => {
  301. return (
  302. <Option value={item.name} key={item.name}>{item.val}</Option>
  303. )
  304. })}
  305. </Select>
  306. </Form.Item>
  307. </Col>
  308. <Col span={6} key={3}>
  309. <Form.Item>
  310. <Button type="primary" htmlType="submit">
  311. 查询
  312. </Button>
  313. <Button onClick={onReset}>
  314. 重置
  315. </Button>
  316. </Form.Item>
  317. </Col>
  318. </Row>
  319. </Form>
  320. </div>
  321. <div className="table">
  322. <div className="table-header">
  323. <h2 className="table-title">用户管理</h2>
  324. <Button type="primary" icon={<PlusOutlined />} onClick={e => showModal('新增用户', true, 1)}>新增用户</Button>
  325. </div>
  326. <Table
  327. columns={columns}
  328. scroll={{ y: 'calc(100vh - 360px)' }}
  329. dataSource={userList}
  330. rowKey={record => record.userId + record.hospitalName}
  331. pagination={{
  332. current: current,
  333. pageSize: size,
  334. size: 'small',
  335. showSizeChanger: true,
  336. pageSizeOptions: ['15', '30', '60', '120'],
  337. showTotal: (total, range) => `第${range[0]}-${range[1]} 条/共 ${total} 条数据`,
  338. onShowSizeChange: (current, pageSize) => onSizeChange(current, pageSize), // 改变每页数量时更新显示
  339. onChange: (page, pageSize) => changePage(page, pageSize),//点击页码事件
  340. total: total
  341. }} />
  342. </div>
  343. {visible && formData ?
  344. <Modal
  345. title={title}
  346. okText='确定'
  347. cancelText='取消'
  348. width={'45%'}
  349. visible={visible}
  350. onCancel={cancel}
  351. footer={null}
  352. forceRender={true}
  353. >
  354. <UserContext.Provider value={{ userId, type, formData, roleList }}>
  355. <AddUser userChange={userChange} />
  356. </UserContext.Provider>
  357. </Modal>
  358. : ''}
  359. <Modal
  360. title="提示"
  361. okText='确定'
  362. cancelText='取消'
  363. width={400}
  364. visible={msvisible}
  365. onOk={handleOk}
  366. onCancel={handleCancel}
  367. >
  368. <p>{tipText[modalType]}</p>
  369. </Modal>
  370. </div >
  371. )
  372. }
  373. export default UserManager;