index.jsx 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import React, { useRef } from 'react'
  2. import { history } from 'umi';
  3. import { PageContainer } from '@ant-design/pro-layout';
  4. import ProTable from '@ant-design/pro-table';
  5. import { PlusOutlined } from '@ant-design/icons';
  6. import { Button, Popconfirm, notification } from 'antd';
  7. import request, { queryTable } from '@/utils/request';
  8. export default () => {
  9. const tableRef = useRef()
  10. const handleDelete = (user) => {
  11. request(`/user/${user.userId}`, { method: 'delete' } ).then(() => {
  12. notification.success({ message: '操作成功' })
  13. tableRef.current.reload()
  14. }).catch((e) => {
  15. notification.error({ message: e.message })
  16. })
  17. }
  18. const handleEdit = (user) => {
  19. const url = user && user.userId ? `/system/user/edit?id=${user.userId}` : '/system/user/edit'
  20. history.push(url)
  21. }
  22. const changeStatus = (user) => {
  23. // 0 变 1, 1 变 0
  24. // eslint-disable-next-line no-bitwise
  25. const status = user.status ^ 1
  26. request(`/user/${user.userId}`, { method: 'put', data: { ...user, status } } ).then(() => {
  27. notification.success({ message: '操作成功' })
  28. tableRef.current.reload()
  29. }).catch((e) => {
  30. notification.error({ message: e.message })
  31. })
  32. }
  33. const handleResetPassword = (user) => {
  34. request(`/reset-password/${user.userId}`, { method: 'put' }).then(() => {
  35. notification.success({ message: '操作成功' })
  36. }).catch((e) => {
  37. notification.error({ message: e.message })
  38. })
  39. }
  40. const columns = [
  41. {
  42. title: '编号',
  43. key: 'userId',
  44. dataIndex: 'userId',
  45. width: 200,
  46. hideInSearch: true,
  47. },
  48. {
  49. title: '姓名',
  50. key: 'userName',
  51. dataIndex: 'userName',
  52. },
  53. {
  54. title: '电话',
  55. key: 'phone',
  56. dataIndex: 'phone',
  57. },
  58. {
  59. title: '登录名',
  60. key: 'loginName',
  61. dataIndex: 'loginName',
  62. hideInSearch: true,
  63. },
  64. {
  65. title: '角色',
  66. key: 'roleName',
  67. dataIndex: 'roleName',
  68. hideInSearch: true,
  69. },
  70. {
  71. title: '创建时间',
  72. key: 'createDate',
  73. dataIndex: 'createDate',
  74. valueType: 'date',
  75. hideInSearch: true,
  76. },
  77. {
  78. title: '操作',
  79. key: 'option',
  80. valueType: 'option',
  81. // 不能操作超级管理员
  82. render: (_, user) => user.userId === '1' ? [] : [
  83. <a key="edit" onClick={() => handleEdit(user)}>修改</a>,
  84. <a key="disable" onClick={() => changeStatus(user)}>{user.status === 1 ? '禁用' : '启用'}</a>,
  85. <Popconfirm key="delete" title="确定删除当前人员?" onConfirm={() => handleDelete(user)}>
  86. <a href="#">删除</a>
  87. </Popconfirm>,
  88. <Popconfirm key="reset" title="确定重置用户密码?" onConfirm={() => handleResetPassword(user)}>
  89. <a href="#">重置密码</a>
  90. </Popconfirm>,
  91. ].filter(Boolean)
  92. },
  93. ]
  94. const actions = [
  95. <Button key="button" icon={<PlusOutlined />} type="primary" onClick={handleEdit}>
  96. 新建
  97. </Button>,
  98. ];
  99. return (
  100. <PageContainer>
  101. <ProTable
  102. actionRef={tableRef}
  103. columns={columns}
  104. request={queryTable('/user')}
  105. rowKey="userId"
  106. headerTitle="用户列表"
  107. search={{
  108. labelWidth: '4em',
  109. }}
  110. toolBarRender={() => actions}
  111. />
  112. </PageContainer>
  113. )
  114. }