Files
gofly-platform-services/zlt-web/react-web-next/src/pages/system/Role/index.tsx
T

151 lines
5.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 }>({});
const [createOpen, setCreateOpen] = useState(false);
const [editOpen, setEditOpen] = useState(false);
const [assignOpen, setAssignOpen] = useState(false);
const [current, setCurrent] = useState<RoleItem | undefined>(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) => (
<Space>
<a onClick={() => { setCurrent(record); setEditOpen(true); }}>编辑</a>
<a onClick={() => { setCurrent(record); setAssignOpen(true); }}>授权</a>
<Popconfirm title={`确认删除角色[${record.name}]?`} onConfirm={async () => {
const hide = message.loading('删除中...');
try {
await roleApi.delete(record.id!);
message.success('删除成功');
} catch (e) {
message.error('删除失败');
} finally { hide(); }
}}>
<a>删除</a>
</Popconfirm>
</Space>
)
}
];
// 页面初次进入时,自动获取当前租户并触发表格请求
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 (
<PageContainer header={{ title: '角色管理' }}>
<SearchBarPlus
defaultCollapsed={false}
split
span={6}
onTenantChanged={(tenantId, includeChildren) => {
setParams((prev) => ({ ...(prev || {}), tenantId, includeChildren }));
}}
onFinish={async (values) => setParams(values)}
>
<ProFormSelect name="searchKey" label="搜索" valueEnum={{ name: '角色名称' }} />
<ProFormText name="searchValue" placeholder="请输入关键字" />
</SearchBarPlus>
<ActionBar
left={
<GradientButton size="middle" key="add" type="primary" onClick={() => setCreateOpen(true)}>
<PlusOutlined /> 新增
</GradientButton>
}
/>
<GlassCard>
<ProTable<RoleItem>
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}
/>
</GlassCard>
<RoleForm
title="新增角色"
open={createOpen}
onOpenChange={setCreateOpen}
onFinish={async (values) => {
const hide = message.loading('保存中...');
try {
await roleApi.saveOrUpdate(values);
message.success('新增成功');
return true;
} catch (e) {
message.error('保存失败');
return false;
} finally { hide(); }
}}
/>
<RoleForm
title="修改角色"
open={editOpen}
initialValues={current}
onOpenChange={setEditOpen}
onFinish={async (values) => {
const hide = message.loading('保存中...');
try {
await roleApi.saveOrUpdate({ ...current, ...values });
message.success('修改成功');
return true;
} catch (e) {
message.error('修改失败');
return false;
} finally { hide(); }
}}
/>
<AssignAuth
open={assignOpen}
roleId={current?.id}
tenantId={params.tenantId}
onClose={() => setAssignOpen(false)}
/>
</PageContainer>
);
};
export default RolePage;