You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

authUser.tsx 8.8 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import DictTag from '@/components/DictTag';
  2. import { HttpResult } from '@/enums/httpEnum';
  3. import { getDictValueEnum } from '@/services/system/dict';
  4. import {
  5. allocatedUserList,
  6. authUserCancel,
  7. authUserCancelAll,
  8. authUserSelectAll,
  9. unallocatedUserList,
  10. } from '@/services/system/role';
  11. import {
  12. DeleteOutlined,
  13. ExclamationCircleOutlined,
  14. PlusOutlined,
  15. RollbackOutlined,
  16. } from '@ant-design/icons';
  17. import { ActionType, PageContainer, ProColumns, ProTable } from '@ant-design/pro-components';
  18. import { FormattedMessage, history, useAccess, useIntl, useParams } from '@umijs/max';
  19. import { Button, Modal, message } from 'antd';
  20. import React, { useEffect, useRef, useState } from 'react';
  21. import UserSelectorModal from './components/UserSelectorModal';
  22. /**
  23. * 删除节点
  24. *
  25. * @param selectedRows
  26. */
  27. const cancelAuthUserAll = async (roleId: string, selectedRows: API.System.User[]) => {
  28. const hide = message.loading('正在取消授权');
  29. if (!selectedRows) return true;
  30. try {
  31. const userIds = selectedRows.map((row) => row.userId).join(',');
  32. const resp = await authUserCancelAll({ roleId, userIds });
  33. hide();
  34. if (resp.code === 200) {
  35. message.success('取消授权成功,即将刷新');
  36. } else {
  37. message.error(resp.msg);
  38. }
  39. return true;
  40. } catch (error) {
  41. hide();
  42. message.error('取消授权失败,请重试');
  43. return false;
  44. }
  45. };
  46. const cancelAuthUser = async (roleId: string, userId: number) => {
  47. const hide = message.loading('正在取消授权');
  48. try {
  49. const resp = await authUserCancel({ userId, roleId });
  50. hide();
  51. if (resp.code === 200) {
  52. message.success('取消授权成功,即将刷新');
  53. } else {
  54. message.error(resp.msg);
  55. }
  56. return true;
  57. } catch (error) {
  58. hide();
  59. message.error('取消授权失败,请重试');
  60. return false;
  61. }
  62. };
  63. const AuthUserTableList: React.FC = () => {
  64. const [modalVisible, setModalVisible] = useState<boolean>(false);
  65. const actionRef = useRef<ActionType>();
  66. const [selectedRows, setSelectedRows] = useState<API.System.User[]>([]);
  67. const [statusOptions, setStatusOptions] = useState<any>([]);
  68. const access = useAccess();
  69. /** 国际化配置 */
  70. const intl = useIntl();
  71. const params = useParams();
  72. if (params.id === undefined) {
  73. history.back();
  74. }
  75. const roleId = params.id || '0';
  76. useEffect(() => {
  77. getDictValueEnum('sys_normal_disable').then((data) => {
  78. setStatusOptions(data);
  79. });
  80. }, []);
  81. const columns: ProColumns<API.System.User>[] = [
  82. {
  83. title: <FormattedMessage id="system.user.user_id" defaultMessage="用户编号" />,
  84. dataIndex: 'deptId',
  85. valueType: 'text',
  86. },
  87. {
  88. title: <FormattedMessage id="system.user.user_name" defaultMessage="用户账号" />,
  89. dataIndex: 'userName',
  90. valueType: 'text',
  91. },
  92. {
  93. title: <FormattedMessage id="system.user.nick_name" defaultMessage="用户昵称" />,
  94. dataIndex: 'nickName',
  95. valueType: 'text',
  96. },
  97. {
  98. title: <FormattedMessage id="system.user.phonenumber" defaultMessage="手机号码" />,
  99. dataIndex: 'phonenumber',
  100. valueType: 'text',
  101. },
  102. {
  103. title: <FormattedMessage id="system.role.create_time" defaultMessage="创建时间" />,
  104. dataIndex: 'createTime',
  105. valueType: 'dateRange',
  106. render: (_, record) => {
  107. return <span>{record.createTime?.toString()} </span>;
  108. },
  109. hideInSearch: true,
  110. },
  111. {
  112. title: <FormattedMessage id="system.user.status" defaultMessage="帐号状态" />,
  113. dataIndex: 'status',
  114. valueType: 'select',
  115. valueEnum: statusOptions,
  116. render: (_, record) => {
  117. return <DictTag enums={statusOptions} value={record.status} />;
  118. },
  119. },
  120. {
  121. title: <FormattedMessage id="pages.searchTable.titleOption" defaultMessage="操作" />,
  122. dataIndex: 'option',
  123. width: '160px',
  124. valueType: 'option',
  125. render: (_, record) => [
  126. <Button
  127. type="link"
  128. size="small"
  129. danger
  130. icon={<DeleteOutlined />}
  131. key="remove"
  132. hidden={!access.hasPerms('system:role:remove')}
  133. onClick={async () => {
  134. Modal.confirm({
  135. title: '删除',
  136. content: '确认要取消该用户' + record.userName + '"角色授权吗?',
  137. okText: '确认',
  138. cancelText: '取消',
  139. onOk: async () => {
  140. const success = await cancelAuthUser(roleId, record.userId);
  141. if (success) {
  142. if (actionRef.current) {
  143. actionRef.current.reload();
  144. }
  145. }
  146. },
  147. });
  148. }}
  149. >
  150. 取消授权
  151. </Button>,
  152. ],
  153. },
  154. ];
  155. return (
  156. <PageContainer header={{ breadcrumb: {}, title: '' }} className="system-menu">
  157. <div style={{ width: '100%', height: '100%' }}>
  158. <ProTable<API.System.User>
  159. headerTitle={intl.formatMessage({
  160. id: 'pages.searchTable.title',
  161. defaultMessage: '信息',
  162. })}
  163. actionRef={actionRef}
  164. rowKey="userId"
  165. key="userList"
  166. search={{
  167. labelWidth: 120,
  168. }}
  169. scroll={{ y: 'calc(100% - 55px)' }}
  170. tableAlertRender={false}
  171. tableAlertOptionRender={false}
  172. toolBarRender={() => [
  173. <Button
  174. type="primary"
  175. key="add"
  176. hidden={!access.hasPerms('system:role:add')}
  177. onClick={async () => {
  178. setModalVisible(true);
  179. }}
  180. >
  181. <PlusOutlined />{' '}
  182. <FormattedMessage id="system.role.auth.addUser" defaultMessage="添加用户" />
  183. </Button>,
  184. <Button
  185. type="primary"
  186. key="remove"
  187. hidden={selectedRows?.length === 0 || !access.hasPerms('system:role:remove')}
  188. onClick={async () => {
  189. Modal.confirm({
  190. title: '是否确认删除所选数据项?',
  191. icon: <ExclamationCircleOutlined />,
  192. content: '请谨慎操作',
  193. async onOk() {
  194. const success = await cancelAuthUserAll(roleId, selectedRows);
  195. if (success) {
  196. setSelectedRows([]);
  197. actionRef.current?.reloadAndRest?.();
  198. }
  199. },
  200. onCancel() {},
  201. });
  202. }}
  203. >
  204. <DeleteOutlined />{' '}
  205. <FormattedMessage id="system.role.auth.cancelAll" defaultMessage="批量取消授权" />
  206. </Button>,
  207. <Button
  208. type="primary"
  209. key="back"
  210. onClick={async () => {
  211. history.back();
  212. }}
  213. >
  214. <RollbackOutlined /> <FormattedMessage id="pages.goback" defaultMessage="返回" />
  215. </Button>,
  216. ]}
  217. request={(params) =>
  218. allocatedUserList({ ...params, roleId } as API.System.RoleListParams).then((res) => {
  219. const result = {
  220. data: res.rows,
  221. total: res.total,
  222. success: true,
  223. };
  224. return result;
  225. })
  226. }
  227. columns={columns}
  228. rowSelection={{
  229. onChange: (_, selectedRows) => {
  230. setSelectedRows(selectedRows);
  231. },
  232. }}
  233. />
  234. </div>
  235. <UserSelectorModal
  236. open={modalVisible}
  237. onSubmit={(values: React.Key[]) => {
  238. const userIds = values.join(',');
  239. if (userIds === '') {
  240. message.warning('请选择要分配的用户');
  241. return;
  242. }
  243. authUserSelectAll({ roleId: roleId, userIds: userIds }).then((resp) => {
  244. if (resp.code === HttpResult.SUCCESS) {
  245. message.success('更新成功!');
  246. if (actionRef.current) {
  247. actionRef.current.reload();
  248. }
  249. } else {
  250. message.warning(resp.msg);
  251. }
  252. });
  253. setModalVisible(false);
  254. }}
  255. onCancel={() => {
  256. setModalVisible(false);
  257. }}
  258. params={{ roleId }}
  259. request={(params) =>
  260. unallocatedUserList({ ...params } as API.System.RoleListParams).then((res) => {
  261. const result = {
  262. data: res.rows,
  263. total: res.rows.length,
  264. success: true,
  265. };
  266. return result;
  267. })
  268. }
  269. />
  270. </PageContainer>
  271. );
  272. };
  273. export default AuthUserTableList;