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