Skip to content

Commit 79e9aa7

Browse files
committed
feat: 添加切换权限页面
1 parent b5cacbd commit 79e9aa7

File tree

7 files changed

+154
-1
lines changed

7 files changed

+154
-1
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import { useLoading } from '@sa/hooks';
2+
import type { DescriptionsProps } from 'antd';
3+
4+
import { useAuth } from '@/features/auth';
5+
import { selectUserInfo } from '@/features/auth/authStore';
6+
import { useInitAuth } from '@/features/auth/initAuth';
7+
import { useRouter } from '@/features/router';
8+
import { initTab, useUpdateTabs } from '@/features/tab/tabHooks';
9+
import { useThemeSettings } from '@/features/theme';
10+
11+
type AccountKey = 'admin' | 'super' | 'user';
12+
13+
interface Account {
14+
key: AccountKey;
15+
label: string;
16+
password: string;
17+
userName: string;
18+
}
19+
20+
const ToggleAuth = () => {
21+
const { t } = useTranslation();
22+
23+
const userInfo = useAppSelector(selectUserInfo);
24+
const updateTabs = useUpdateTabs();
25+
const { hasAuth } = useAuth();
26+
27+
const { toLogin } = useInitAuth();
28+
29+
const { loading, startLoading } = useLoading();
30+
const themeSettings = useThemeSettings();
31+
const { reload, resetRoutes } = useRouter();
32+
33+
const [loginAccount, setLoginAccount] = useState<AccountKey>('super');
34+
35+
const accounts: Account[] = [
36+
{
37+
key: 'super',
38+
label: t('page.login.pwdLogin.superAdmin'),
39+
password: '123456',
40+
userName: 'Super'
41+
},
42+
{
43+
key: 'admin',
44+
label: t('page.login.pwdLogin.admin'),
45+
password: '123456',
46+
userName: 'Admin'
47+
},
48+
{
49+
key: 'user',
50+
label: t('page.login.pwdLogin.user'),
51+
password: '123456',
52+
userName: 'User'
53+
}
54+
];
55+
56+
const roles: DescriptionsProps['items'] = [
57+
{
58+
children: (
59+
<ASpace>
60+
{userInfo.roles.map(role => (
61+
<ATag key={role}>{role}</ATag>
62+
))}
63+
</ASpace>
64+
),
65+
key: '1',
66+
label: t('page.manage.user.userRole')
67+
},
68+
{
69+
children: (
70+
<ASpace>
71+
{accounts.map(account => (
72+
<AButton
73+
disabled={loading && loginAccount !== account.key}
74+
key={account.key}
75+
loading={loading && loginAccount === account.key}
76+
onClick={() => handleToggleAccount(account)}
77+
>
78+
{account.label}
79+
</AButton>
80+
))}
81+
</ASpace>
82+
),
83+
key: '2',
84+
label: t('page.function.toggleAuth.toggleAccount')
85+
}
86+
];
87+
88+
async function handleToggleAccount(account: Account) {
89+
setLoginAccount(account.key);
90+
91+
startLoading();
92+
93+
resetRoutes();
94+
95+
await toLogin({ password: account.password, userName: account.userName }, false);
96+
97+
initTab(themeSettings.tab.cache, updateTabs);
98+
99+
reload();
100+
}
101+
return (
102+
<ASpace
103+
className="w-full"
104+
direction="vertical"
105+
size={16}
106+
>
107+
<ACard
108+
className="card-wrapper"
109+
size="small"
110+
title={t('request.logout')}
111+
>
112+
<ADescriptions
113+
bordered
114+
column={1}
115+
items={roles}
116+
layout="vertical"
117+
size="small"
118+
/>
119+
120+
<ACard
121+
className="card-wrapper"
122+
size="small"
123+
title={t('page.function.toggleAuth.authHook')}
124+
variant="borderless"
125+
>
126+
<ASpace>
127+
{hasAuth('B_CODE1') && <AButton>{t('page.function.toggleAuth.superAdminVisible')}</AButton>}
128+
{hasAuth('B_CODE2') && <AButton>{t('page.function.toggleAuth.adminVisible')}</AButton>}
129+
{hasAuth('B_CODE3') && <AButton>{t('page.function.toggleAuth.adminOrUserVisible')}</AButton>}
130+
</ASpace>
131+
</ACard>
132+
</ACard>
133+
</ASpace>
134+
);
135+
};
136+
137+
export const handle = {
138+
i18nKey: 'route.function_toggle-auth',
139+
icon: 'ic:round-construction',
140+
order: 4,
141+
title: 'function_toggle-auth'
142+
};
143+
144+
export default ToggleAuth;

src/pages/(base)/home/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ const Home = () => {
4949
};
5050

5151
export const handle = {
52-
constant: true,
5352
i18nKey: 'route.home',
5453
icon: 'mdi:monitor-dashboard',
5554
order: 1,

src/router/elegant/imports.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const pages: Record<string, () => Promise<any>> = {
2828
"(base)_function_request": () => import("@/pages/(base)/function/request/index.tsx"),
2929
"(base)_function_super-page": () => import("@/pages/(base)/function/super-page/index.tsx"),
3030
"(base)_function_tab": () => import("@/pages/(base)/function/tab/index.tsx"),
31+
"(base)_function_toggle-auth": () => import("@/pages/(base)/function/toggle-auth/index.tsx"),
3132
"(base)_home": () => import("@/pages/(base)/home/index.tsx"),
3233
"(base)_manage": () => import("@/pages/(base)/manage/index.tsx"),
3334
"(base)_manage_menu": () => import("@/pages/(base)/manage/menu/index.tsx"),

src/router/elegant/routeMap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export const routeMap: RouteMap = {
3939
"(base)_function_request": "/function/request",
4040
"(base)_function_super-page": "/function/super-page",
4141
"(base)_function_tab": "/function/tab",
42+
"(base)_function_toggle-auth": "/function/toggle-auth",
4243
"(base)_home": "/home",
4344
"(base)_manage": "/manage",
4445
"(base)_manage_menu": "/manage/menu",

src/router/elegant/routes.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ export const generatedRoutes = [
7373
matchedFiles: [null, '/src/pages/(base)/function/tab/index.tsx', null, null],
7474
name: '(base)_function_tab',
7575
path: '/function/tab'
76+
},
77+
{
78+
matchedFiles: [null, '/src/pages/(base)/function/toggle-auth/index.tsx', null, null],
79+
name: '(base)_function_toggle-auth',
80+
path: '/function/toggle-auth'
7681
}
7782
]
7883
},

src/types/auto-imports.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ declare global {
6464
const SoybeanAvatar: typeof import('../components/SoybeanAvatar')['default']
6565
const SvgIcon: typeof import('../components/SvgIcon')['default']
6666
const SystemLogo: typeof import('../components/SystemLogo')['default']
67+
const TypingAnimation: typeof import('../components/TypingAnimation')['TypingAnimation']
6768
const WaveBg: typeof import('../components/WaveBg')['default']
6869
const createRef: typeof import('react')['createRef']
6970
const forwardRef: typeof import('react')['forwardRef']

src/types/elegant-router.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ declare module "@soybean-react/vite-plugin-react-router" {
4040
"(base)_function_request": "/function/request";
4141
"(base)_function_super-page": "/function/super-page";
4242
"(base)_function_tab": "/function/tab";
43+
"(base)_function_toggle-auth": "/function/toggle-auth";
4344
"(base)_home": "/home";
4445
"(base)_manage": "/manage";
4546
"(base)_manage_menu": "/manage/menu";
@@ -143,6 +144,7 @@ declare module "@soybean-react/vite-plugin-react-router" {
143144
| "(base)_function_request"
144145
| "(base)_function_super-page"
145146
| "(base)_function_tab"
147+
| "(base)_function_toggle-auth"
146148
| "(base)_home"
147149
| "(base)"
148150
| "(base)_manage"

0 commit comments

Comments
 (0)