1- import React from 'react'
2- import { Card , Button , Space , Tag , message , Divider , Row , Col , Grid , Switch , Select } from 'antd'
1+ import React , { useState , useEffect } from 'react'
2+ import { Card , Button , Space , Tag , message , Divider , Row , Col , Grid , Switch , Select , Alert } from 'antd'
33import { usePermission } from '@app-hooks/usePermission'
44import PermissionGuard from '@/components/auth/PermissionGuard'
55import AuthButton from '@/components/auth/AuthButton'
@@ -24,12 +24,34 @@ const PermissionExample = () => {
2424
2525 // 角色切换(开发测试用)
2626 const switchRole = async ( roleCode : string ) => {
27+ // 如果已登录且未强制启用示例切换,则阻止切换,以免与真实登录权限冲突
28+ const token =
29+ localStorage . getItem ( 'token' ) || localStorage . getItem ( 'github_token' ) || localStorage . getItem ( 'github_user' )
30+ const force = localStorage . getItem ( 'force_demo_switch' ) === '1'
31+ if ( token && ! force ) {
32+ message . warning ( '当前为已登录状态,示例内角色切换已被禁用。如需测试切换,请先登出或在本页面启用开发强制开关。' )
33+ return
34+ }
35+
2736 localStorage . setItem ( 'user_role' , roleCode )
2837 await permissionService . syncPermissions ( )
2938 await refreshPermissions ( )
3039 message . success ( `已切换到 ${ roleCode } 角色` )
3140 }
3241
42+ // 本地控制:开发时可强制启用示例切换(仅页面级别)
43+ const [ forceDemoSwitch , setForceDemoSwitch ] = useState < boolean > ( false )
44+ useEffect ( ( ) => {
45+ setForceDemoSwitch ( localStorage . getItem ( 'force_demo_switch' ) === '1' )
46+ } , [ ] )
47+
48+ const toggleForceDemoSwitch = ( checked : boolean ) => {
49+ setForceDemoSwitch ( checked )
50+ if ( checked ) localStorage . setItem ( 'force_demo_switch' , '1' )
51+ else localStorage . removeItem ( 'force_demo_switch' )
52+ message . info ( `开发强制切换已${ checked ? '启用' : '禁用' } ` )
53+ }
54+
3355 // 主题设置切换
3456 const handleThemeModeChange = ( checked : boolean ) => {
3557 updateSettings ( { themeMode : checked ? 'dark' : 'light' } )
@@ -93,6 +115,32 @@ const PermissionExample = () => {
93115
94116 < Divider />
95117
118+ { /* 登录时提示:已使用登录账户权限,示例切换已禁用 */ }
119+ { ( localStorage . getItem ( 'token' ) ||
120+ localStorage . getItem ( 'github_token' ) ||
121+ localStorage . getItem ( 'github_user' ) ) &&
122+ ! forceDemoSwitch && (
123+ < Alert
124+ title = "已使用登录账户权限,示例切换已禁用"
125+ description = {
126+ < div >
127+ 如需临时启用示例切换(仅用于开发),可在下方启用“开发强制开关”。启用后示例切换会覆盖当前页面权限视图,但不会修改后端用户数据。
128+ </ div >
129+ }
130+ type = "warning"
131+ showIcon
132+ style = { { marginBottom : 16 } }
133+ />
134+ ) }
135+
136+ { /* 开发强制开关,仅在非生产或 localhost 显示 */ }
137+ { ( process . env . NODE_ENV !== 'production' || window . location . hostname . includes ( 'localhost' ) ) && (
138+ < div style = { { marginBottom : 16 , display : 'flex' , alignItems : 'center' , gap : 12 } } >
139+ < Switch checked = { forceDemoSwitch } onChange = { toggleForceDemoSwitch } />
140+ < span style = { { color : 'rgba(0,0,0,0.65)' } } > 开发:强制启用示例切换(仅页面级别)</ span >
141+ </ div >
142+ ) }
143+
96144 { /* 权限说明 */ }
97145 < Card title = "权限说明" style = { { marginBottom : 16 } } size = { screens . xs ? 'small' : 'default' } >
98146 < Row gutter = { [ 16 , 8 ] } >
@@ -193,10 +241,18 @@ const PermissionExample = () => {
193241 </ Tag >
194242 ) }
195243 </ div >
244+ { /* 如果当前为已登录状态,禁用示例内切换按钮以避免覆盖真实用户权限 */ }
196245 < Button
197246 type = { roles . includes ( role . code ) ? 'primary' : 'default' }
198247 onClick = { ( ) => switchRole ( role . code ) }
199248 size = { screens . xs ? 'small' : 'middle' }
249+ disabled = {
250+ ! ! (
251+ localStorage . getItem ( 'token' ) ||
252+ localStorage . getItem ( 'github_token' ) ||
253+ localStorage . getItem ( 'github_user' )
254+ )
255+ }
200256 >
201257 切换
202258 </ Button >
0 commit comments