import React, { useEffect, useState } from 'react'; import { PageContainer, ProTable, ProFormSelect, ProFormText } from '@ant-design/pro-components'; import { Popconfirm, Space, message } from 'antd'; import { ActionBar, GlassCard, GradientButton, SearchBarPlus } from '@/components/ui'; import { PlusOutlined } from '@ant-design/icons'; import { roleApi, RoleItem } from '@/services/role'; import { tenantApi } from '@/services/tenant'; import RoleForm from './components/RoleForm'; import AssignAuth from './components/AssignAuth'; const RolePage: React.FC = () => { const [params, setParams] = useState<{ tenantId?: string; includeChildren?: boolean; searchKey?: string; searchValue?: string; _timestamp?: number }>({}); const [createOpen, setCreateOpen] = useState(false); const [editOpen, setEditOpen] = useState(false); const [assignOpen, setAssignOpen] = useState(false); const [current, setCurrent] = useState(undefined); // 以租户为主:通过 SearchBarPlus 内置租户选择器 const columns = [ { dataIndex: 'index', valueType: 'indexBorder', width: 48 }, { title: '租户', dataIndex: 'tenantId', width: 160, search: false, render: (_: any, r: RoleItem) => (r as any)?.tenantName || (r as any)?.tenantId || '-' }, { title: 'ID', dataIndex: 'id' }, { title: '角色名称', dataIndex: 'name' }, { title: '编码', dataIndex: 'code' }, { title: '数据权限', dataIndex: 'dataScope', renderText: (v: any) => (v === 'ALL' ? '全部' : '自定义') }, { title: '操作', valueType: 'option', width: 220, render: (_: any, record: RoleItem) => ( { setCurrent(record); setEditOpen(true); }}>编辑 { setCurrent(record); setAssignOpen(true); }}>授权 { const hide = message.loading('删除中...'); try { await roleApi.delete(record.id!); message.success('删除成功'); } catch (e) { message.error('删除失败'); } finally { hide(); } }}> 删除 ) } ]; // 页面初次进入时,自动获取当前租户并触发表格请求 useEffect(() => { (async () => { try { let tenantId: string | undefined; const cur: any = await tenantApi.getCurrentTenant(); tenantId = cur?.data?.tenantId ?? cur?.tenantId; if (!tenantId) { const stats: any = await tenantApi.getScopeStats(); tenantId = stats?.data?.currentTenantId ?? stats?.currentTenantId; } if (tenantId) { setParams((prev) => ({ ...(prev || {}), tenantId, includeChildren: true })); } } catch (e) { // 忽略错误,保持手动查询可用 } })(); }, []); return ( { setParams((prev) => ({ ...(prev || {}), tenantId, includeChildren, _timestamp: Date.now() })); }} onFinish={async (values) => { setParams((prev) => ({ ...(prev || {}), ...values, _timestamp: Date.now() })); }} > setCreateOpen(true)}> 新增 } /> rowKey="id" headerTitle="角色列表" params={params} request={async (p) => roleApi.page({ ...p, ...params })} columns={columns as any} search={false} pagination={{ showSizeChanger: true, showQuickJumper: true }} toolBarRender={false} /> { const hide = message.loading('保存中...'); try { await roleApi.saveOrUpdate(values); message.success('新增成功'); return true; } catch (e) { message.error('保存失败'); return false; } finally { hide(); } }} /> { const hide = message.loading('保存中...'); try { await roleApi.saveOrUpdate({ ...current, ...values }); message.success('修改成功'); return true; } catch (e) { message.error('修改失败'); return false; } finally { hide(); } }} /> setAssignOpen(false)} /> ); }; export default RolePage;