import React, { useEffect, useState } from 'react'; import { Modal, Transfer, message, Spin } from 'antd'; import type { TransferDirection, TransferProps } from 'antd/es/transfer'; import { getRoles } from '../../Role/service'; import { getUserRoleIds, updateUserRoles } from '../service'; import type { RoleDTO } from '../../Role/types'; interface RoleModalProps { userId: number; visible: boolean; onCancel: () => void; onSuccess: () => void; } interface RoleTransferItem { key: string; title: string; description: string; disabled?: boolean; } const RoleModal: React.FC = ({ userId, visible, onCancel, onSuccess }) => { const [roles, setRoles] = useState([]); const [selectedKeys, setSelectedKeys] = useState([]); const [loading, setLoading] = useState(false); const fetchData = async () => { try { setLoading(true); const [allRoles, userRoleIds] = await Promise.all([ getRoles(), getUserRoleIds(userId) ]); setRoles(allRoles); setSelectedKeys(userRoleIds.map(String)); } catch (error) { message.error('获取角色数据失败'); } finally { setLoading(false); } }; useEffect(() => { if (visible) { fetchData(); } }, [visible, userId]); const handleChange: TransferProps['onChange'] = (nextTargetKeys) => { setSelectedKeys(nextTargetKeys as string[]); }; const handleOk = async () => { try { setLoading(true); await updateUserRoles(userId, selectedKeys.map(Number)); message.success('角色分配成功'); onSuccess(); } catch (error) { message.error('角色分配失败'); } finally { setLoading(false); } }; return ( dataSource={roles.map(role => ({ key: role.id.toString(), title: role.name, description: role.code, disabled: role.code === 'ROLE_ADMIN' }))} titles={['未选择', '已选择']} targetKeys={selectedKeys} onChange={handleChange} render={item => item.title} listStyle={{ width: 250, height: 400, }} showSearch filterOption={(inputValue, item) => (item.title?.toLowerCase().indexOf(inputValue.toLowerCase()) !== -1) || (item.description?.toLowerCase().indexOf(inputValue.toLowerCase()) !== -1) } /> ); }; export default RoleModal;