index.jsx 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import React, { useState, useEffect, useRef } from 'react';
  2. import {
  3. Button,
  4. Popconfirm,
  5. Modal,
  6. Form,
  7. message,
  8. Row,
  9. Col,
  10. } from 'antd';
  11. import { PlusOutlined } from '@ant-design/icons';
  12. import {
  13. getUserList,
  14. deleteUser,
  15. updateUser,
  16. getUserDetail,
  17. } from '@/services/user';
  18. import { getUserRoleList } from '@/services/userRole';
  19. import { PageHeaderWrapper } from '@ant-design/pro-layout';
  20. import PageTable from '@/components/PageTable';
  21. import moment from 'moment';
  22. import './index.less';
  23. import OrgTree from './components/OrgTree';
  24. import UserModal from './components/UserModal';
  25. import RoleModel from './components/RoleModel';
  26. const formatterTime = (val) => {
  27. return val && val !== '-' ? moment(val).format('YYYY-MM-DD') : '-';
  28. };
  29. export default (props) => {
  30. //编辑弹窗
  31. const [form] = Form.useForm();
  32. const [editModal, setEditModal] = useState(false);
  33. const [userId, setuserId] = useState();
  34. const [cooperativeList, setCooperativeList] = useState([]);
  35. const [editRoleModal, setEditRoleModal] = useState(false);
  36. const [rUserId, setRUserId] = useState();
  37. const [currentCheckbox, setCurrentCheckbox] = useState();
  38. //列表数据
  39. const actionRef = useRef();
  40. const [orgId, setOrgId] = useState();
  41. //表单点击取消按钮
  42. const onCancel = () => {
  43. setuserId();
  44. form.resetFields();
  45. setEditModal(false);
  46. };
  47. // 列表点击编辑按钮
  48. const handleEdit = (val) => {
  49. setuserId(val.userId);
  50. if (!userId && orgId) {
  51. form.setFieldsValue({ orgId: orgId })
  52. }
  53. setEditModal(true);
  54. };
  55. //列表点击删除按钮
  56. const handleDelete = (id) => {
  57. deleteUser(id)
  58. .then(() => {
  59. message.success('删除成功');
  60. actionRef.current.reload();
  61. })
  62. .catch((err) => {
  63. console.log(err.message);
  64. });
  65. };
  66. //列表切换人员状态方法
  67. const handleOK = (record, data) => {
  68. const title = record.status
  69. ? '您确定要将该人员状态变更为禁用吗? 禁用后该人员将不能登录'
  70. : '您确定要将该人员状态变更为启用吗? 启用后该人员可以登录!';
  71. Modal.confirm({
  72. title: title,
  73. okText: '确认',
  74. cancelText: '取消',
  75. onOk() {
  76. updateUser(record.userId, { ...record, status: record.status === 1 ? 0 : 1 })
  77. .then((res) => {
  78. message.success('操作成功');
  79. actionRef.current.reload();
  80. })
  81. .catch((err) => {
  82. console.log(err.message);
  83. });
  84. },
  85. });
  86. };
  87. //点击授权角色方法
  88. const handleRole = (val) => {
  89. setRUserId(val.userId);
  90. getUserRoleList({ user_id: val.userId })
  91. .then((res) => {
  92. if (res.length !== 0) {
  93. setCurrentCheckbox(res.map((item) => item.roleId));
  94. }
  95. })
  96. .catch((err) => {
  97. console.log(err.message);
  98. });
  99. setEditRoleModal(true);
  100. };
  101. //监控
  102. useEffect(() => {
  103. if (userId) {
  104. getUserDetail(userId)
  105. .then((res) => {
  106. if (res.orgId === '-1') {
  107. form.setFieldsValue({ ...res, orgId: cooperativeList[0].orgId });
  108. } else {
  109. form.setFieldsValue(res);
  110. }
  111. })
  112. .catch((err) => {
  113. console.log(err.message);
  114. });
  115. } else {
  116. if (orgId) {
  117. form.setFieldsValue({ orgId: orgId })
  118. } else {
  119. form.resetFields();
  120. }
  121. }
  122. }, [orgId, userId]);
  123. const actions = () => [
  124. <Button key="add" type="primary" icon={<PlusOutlined />} onClick={handleEdit}>
  125. 新增
  126. </Button>,
  127. ];
  128. const columns = [
  129. {
  130. title: '姓名',
  131. dataIndex: 'userName',
  132. key: 'userName',
  133. },
  134. {
  135. title: '昵称',
  136. dataIndex: 'nickName',
  137. key: 'nickName',
  138. search: false,
  139. },
  140. {
  141. title: '性别',
  142. dataIndex: 'sex',
  143. key: 'sex',
  144. width: 80,
  145. render: (_, record) => {
  146. return record.sex === 1 ? '男' : record.sex === 0 ? '女' : '未知';
  147. },
  148. search: false,
  149. },
  150. {
  151. title: '手机号',
  152. dataIndex: 'phone',
  153. key: 'phone',
  154. width: 120,
  155. },
  156. {
  157. title: '注册时间',
  158. dataIndex: 'createDate',
  159. key: 'createDate',
  160. render: formatterTime,
  161. search: false,
  162. width: 80,
  163. },
  164. {
  165. title: '状态',
  166. dataIndex: 'status',
  167. key: 'status',
  168. width: 80,
  169. search: false,
  170. render: (_, record) => {
  171. return record.status === 1 ? '启用' : '未启用';
  172. },
  173. },
  174. {
  175. title: '管理员',
  176. dataIndex: 'isOrgManager',
  177. key: 'isOrgManager',
  178. width: 80,
  179. search: false,
  180. render: (_, record) => {
  181. return record.isOrgManager && record.isOrgManager == 1 ? '管理员' : '非管理员';
  182. },
  183. },
  184. {
  185. title: '操作',
  186. valueType: 'option',
  187. width: 160,
  188. render: (_, record) => [
  189. <Button style={{ padding: 0 }} type="link" key={1} onClick={() => handleOK(record)}>
  190. {record.status === 0 ? '启用' : '禁用'}
  191. </Button>,
  192. <Button style={{ padding: 0 }} type="link" key={2} onClick={() => handleEdit(record)}>
  193. 编辑
  194. </Button>,
  195. <Button style={{ padding: 0 }} type="link" key={3} onClick={() => handleRole(record)}>
  196. 授权角色
  197. </Button>,
  198. <Popconfirm
  199. key={4}
  200. title="您是否确认删除 ?"
  201. onConfirm={() => handleDelete(record.userId)}
  202. okText="确定"
  203. cancelText="取消"
  204. >
  205. <Button style={{ padding: 0 }} type="link">
  206. 删除
  207. </Button>
  208. </Popconfirm>,
  209. ],
  210. },
  211. ];
  212. return (
  213. <PageHeaderWrapper>
  214. <Row gutter={16}>
  215. <Col span={6}>
  216. <OrgTree setOrgId={setOrgId} setCooperativeList={setCooperativeList} />
  217. </Col>
  218. <Col span={18}>
  219. <PageTable
  220. request={getUserList}
  221. params={{ org_id: orgId }}
  222. // expfunc={exportPersonList}
  223. columns={columns}
  224. actionRef={actionRef}
  225. rowKey="userId"
  226. options={false}
  227. toolBarRender={actions}
  228. scroll={{ x: 1000 }}
  229. />
  230. </Col>
  231. </Row>
  232. <UserModal editModal={editModal} onCancel={onCancel} actionRef={actionRef} form={form} orgId={orgId} userId={userId} />
  233. <RoleModel rUserId={rUserId} setRUserId={setRUserId} currentCheckbox={currentCheckbox} setCurrentCheckbox={setCurrentCheckbox} editRoleModal={editRoleModal} setEditRoleModal={setEditRoleModal} />
  234. </PageHeaderWrapper>
  235. );
  236. };