Skip to content

Commit 303b683

Browse files
committed
fix: fix sonarqube
1 parent 653698e commit 303b683

File tree

13 files changed

+330
-188
lines changed

13 files changed

+330
-188
lines changed

src/mock/permission.ts

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -128,24 +128,20 @@ export const mockRoles: Role[] = [
128128
/**
129129
* 固定的测试账号(四个角色对应四个账号)
130130
*/
131-
export const testAccounts: Record<string, { password: string; role: string; name: string }> = {
131+
export const testAccounts: Record<string, { role: string; name: string }> = {
132132
133-
password: '123456',
134133
role: 'super_admin',
135134
name: '超级管理员',
136135
},
137136
138-
password: '123456',
139137
role: 'admin',
140138
name: '管理员',
141139
},
142140
143-
password: '123456',
144141
role: 'business_user',
145142
name: '业务员',
146143
},
147144
148-
password: '123456',
149145
role: 'user',
150146
name: '普通用户',
151147
},
@@ -304,47 +300,45 @@ const safeJsonParse = <T>(text: string | null): T | null => {
304300
}
305301
}
306302

303+
const safeGetStorageItem = (key: string): string | null => {
304+
try {
305+
return globalThis?.localStorage?.getItem(key) ?? null
306+
} catch {
307+
return null
308+
}
309+
}
310+
307311
const tryGetManualRolePermission = (): UserPermission | null => {
308-
const storedRoleCode = localStorage.getItem('user_role')
309-
console.log('📝 手动设置的角色:', storedRoleCode)
312+
const storedRoleCode = safeGetStorageItem('user_role')
310313
if (storedRoleCode && mockUserPermissions[storedRoleCode]) {
311-
console.log('✅ 使用手动设置的角色:', storedRoleCode)
312314
return { ...mockUserPermissions[storedRoleCode] }
313315
}
314316
return null
315317
}
316318

317319
const tryGetGithubUserPermission = (): UserPermission | null => {
318-
const githubUser = localStorage.getItem('github_user')
320+
const githubUser = safeGetStorageItem('github_user')
319321
const user = safeJsonParse<{ email?: string }>(githubUser)
320322
if (!user) return null
321323

322-
console.log('🔍 检测到 GitHub 用户:', user)
323-
if (user.email === '[email protected]' || user.email) {
324-
console.log('✅ GitHub 用户登录,授予超级管理员权限')
324+
if (user.email) {
325325
return { ...mockUserPermissions['super_admin'] }
326326
}
327327
return null
328328
}
329329

330330
const tryGetTokenRolePermission = (): UserPermission | null => {
331-
const tokenData = localStorage.getItem('token')
332-
console.log('🎫 Token 数据:', tokenData)
331+
const tokenData = safeGetStorageItem('token')
333332
if (!tokenData) return null
334333

335334
const tokenObj = safeJsonParse<{ token?: string }>(tokenData)
336335
const email = tokenObj?.token ?? tokenData
337-
console.log('📧 解析出的邮箱:', email)
338-
console.log('🔎 查找账号:', email, '在', Object.keys(testAccounts))
339336

340337
const account = testAccounts[email]
341338
if (!account) {
342-
console.log('❌ 未找到匹配的测试账号')
343339
return null
344340
}
345341

346-
console.log('✅ 找到账号,角色:', account.role)
347-
console.log('📋 返回权限数据:', mockUserPermissions[account.role])
348342
return { ...mockUserPermissions[account.role] }
349343
}
350344

@@ -354,8 +348,6 @@ const tryGetTokenRolePermission = (): UserPermission | null => {
354348
export const mockGetUserPermissions = async (_userId?: string, _roleCode?: string): Promise<UserPermission> => {
355349
await new Promise((resolve) => setTimeout(resolve, 100))
356350

357-
console.log('🔍 开始获取用户权限...')
358-
359351
// 1. 优先使用手动设置的角色(用于测试切换)
360352
const manual = tryGetManualRolePermission()
361353
if (manual) return manual
@@ -369,7 +361,6 @@ export const mockGetUserPermissions = async (_userId?: string, _roleCode?: strin
369361
if (token) return token
370362

371363
// 4. 默认返回普通用户权限
372-
console.log('⚠️ 使用默认权限(普通用户)')
373364
return { ...mockUserPermissions['user'] }
374365
}
375366

src/pages/demo/index.jsx

Lines changed: 16 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import React, { useState, useEffect } from 'react'
2-
import { Table } from 'antd'
1+
import React from 'react'
32
import { Smartphone, Monitor, Rocket } from 'lucide-react'
43
import PageContainer from '@stateless/PageContainer'
54
import FixTabPanel from '@stateless/FixTabPanel'
@@ -28,7 +27,6 @@ import HePng from '@assets/images/he.png'
2827
import SongPng from '@assets/images/song.png'
2928
import XuePng from '@assets/images/xue.png'
3029
import OneTimePasscode from '@stateless/OneTimePasscode'
31-
import styles from './index.module.less'
3230

3331
const companies = [SpringPng, HePng, SongPng, XuePng]
3432

@@ -85,68 +83,20 @@ const fixTabsData = [
8583
},
8684
]
8785

88-
const tabs = [
89-
{
90-
title: 'Product',
91-
value: 'product',
92-
content: (
93-
<div className="relative h-full w-full overflow-hidden rounded-2xl bg-linear-to-br from-purple-700 to-violet-900 p-10 text-xl font-bold text-white md:text-4xl">
94-
<p>Product Tab</p>
95-
</div>
96-
),
97-
},
98-
{
99-
title: 'Services',
100-
value: 'services',
101-
content: (
102-
<div className="relative h-full w-full overflow-hidden rounded-2xl bg-linear-to-br from-purple-700 to-violet-900 p-10 text-xl font-bold text-white md:text-4xl">
103-
<p>Services tab</p>
104-
</div>
105-
),
106-
},
107-
{
108-
title: 'Playground',
109-
value: 'playground',
110-
content: (
111-
<div className="relative h-full w-full overflow-hidden rounded-2xl bg-linear-to-br from-purple-700 to-violet-900 p-10 text-xl font-bold text-white md:text-4xl">
112-
<p>Playground tab</p>
113-
</div>
114-
),
115-
},
116-
{
117-
title: 'Content',
118-
value: 'content',
119-
content: (
120-
<div className="relative h-full w-full overflow-hidden rounded-2xl bg-linear-to-br from-purple-700 to-violet-900 p-10 text-xl font-bold text-white md:text-4xl">
121-
<p>Content tab</p>
122-
</div>
123-
),
124-
},
125-
{
126-
title: 'Random',
127-
value: 'random',
128-
content: (
129-
<div className="relative h-full w-full overflow-hidden rounded-2xl bg-linear-to-br from-purple-700 to-violet-900 p-10 text-xl font-bold text-white md:text-4xl">
130-
<p>Random tab</p>
131-
</div>
132-
),
133-
},
134-
]
135-
13686
const customCommandMap = {
13787
npm: 'npm run shadcn add button',
13888
yarn: 'yarn shadcn add button',
13989
pnpm: 'pnpm dlx shadcn@latest add button',
14090
bun: 'bun x shadcn@latest add button',
14191
}
14292

143-
const dateDifference = (date1, date2) => {
144-
const d1 = new Date(date1)
145-
const d2 = new Date(date2)
146-
const diffTime = Math.abs(d2 - d1)
147-
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24))
148-
return diffDays
149-
}
93+
const animatedListItems = Array.from({ length: 10 }, (_, index) => ({
94+
id: `animated-${index + 1}`,
95+
}))
96+
97+
const stickyCardItems = Array.from({ length: 4 }, (_, index) => ({
98+
id: `sticky-${index + 1}`,
99+
}))
150100

151101
const tagCardList = [
152102
{
@@ -315,7 +265,7 @@ const ProDemo = () => {
315265
return (
316266
<FixTabPanel>
317267
<PageContainer>
318-
<OneTimePasscode length={6} variant="compact" onComplete={(code) => console.log('OTP Code:', code)} />
268+
<OneTimePasscode length={6} variant="compact" onComplete={() => {}} />
319269
<MusicPlayer />
320270
<TransferHistory />
321271
<FixCarousel />
@@ -338,17 +288,13 @@ const ProDemo = () => {
338288
<LogoSlider companies={companies} />
339289
<section style={{ height: 240, overflow: 'hidden', margin: 20 }}>
340290
<AnimatedList>
341-
{Array.from({ length: 10 }, () => ({
342-
id: Math.random(),
343-
}))
344-
.flat()
345-
.map((item, index) => (
346-
<div key={item?.id} className="flex flex-col items-center justify-center gap-4">
347-
<div className="flex items-center justify-center gap-4">
348-
<div className="h-16 w-100 rounded-full bg-gradient-to-br from-purple-500 to-blue-500" />
349-
</div>
291+
{animatedListItems.map((item) => (
292+
<div key={item.id} className="flex flex-col items-center justify-center gap-4">
293+
<div className="flex items-center justify-center gap-4">
294+
<div className="h-16 w-100 rounded-full bg-gradient-to-br from-purple-500 to-blue-500" />
350295
</div>
351-
))}
296+
</div>
297+
))}
352298
</AnimatedList>
353299
</section>
354300

@@ -374,7 +320,7 @@ const ProDemo = () => {
374320
]}
375321
className="my-4 rounded-xl"
376322
/>
377-
<StickyCard cards={[...Array.from({ length: 4 }, () => ({ id: Math.random() }))]} />
323+
<StickyCard cards={stickyCardItems} />
378324
<section style={{ marginBottom: '15px' }}>
379325
<OrgChart />
380326
</section>

src/pages/layout/primaryNav/index.jsx

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react'
2+
import PropTypes from 'prop-types'
23
import { Button, Space } from 'antd'
34
import { DashboardOutlined, ProjectOutlined, HomeOutlined } from '@ant-design/icons'
45
import useSafeNavigate from '@app-hooks/useSafeNavigate'
@@ -8,15 +9,46 @@ export const usePrimaryNavItems = () => {
89
const { redirectTo } = useSafeNavigate()
910
const { routes } = usePermission()
1011

12+
const matchWildcard = (text, pattern) => {
13+
if (pattern === '*') return true
14+
if (!pattern.includes('*')) return text === pattern
15+
16+
const parts = pattern.split('*').filter((p) => p.length > 0)
17+
if (parts.length === 0) return true
18+
19+
let pos = 0
20+
const startsWithWildcard = pattern.startsWith('*')
21+
const endsWithWildcard = pattern.endsWith('*')
22+
23+
if (!startsWithWildcard) {
24+
const first = parts[0]
25+
if (!text.startsWith(first)) return false
26+
pos = first.length
27+
}
28+
29+
for (let i = startsWithWildcard ? 0 : 1; i < parts.length; i += 1) {
30+
const part = parts[i]
31+
const idx = text.indexOf(part, pos)
32+
if (idx === -1) return false
33+
pos = idx + part.length
34+
}
35+
36+
if (!endsWithWildcard) {
37+
const last = parts[parts.length - 1]
38+
return text.endsWith(last)
39+
}
40+
41+
return true
42+
}
43+
1144
const hasAccess = (path) => {
1245
if (!routes || routes.length === 0) return false
1346
if (path === '/') return true
1447
return routes.some((route) => {
1548
if (route === path) return true
1649
if (path.startsWith(route + '/')) return true
1750
if (route.includes('*')) {
18-
const pattern = route.replace('*', '.*')
19-
return new RegExp(`^${pattern}$`).test(path)
51+
return matchWildcard(path, route)
2052
}
2153
return false
2254
})
@@ -63,4 +95,8 @@ const PrimaryNav = ({ layout = '' }) => {
6395
)
6496
}
6597

98+
PrimaryNav.propTypes = {
99+
layout: PropTypes.string,
100+
}
101+
66102
export default PrimaryNav

src/pages/layout/proContent/breadcrumb/util.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ export const findRouteChain = (routes, pathname) => {
4646
if (route.key === pathToCheck || route.path === pathToCheck) return true
4747

4848
// 动态路由匹配
49-
if (route.key && route.key.includes(':')) {
50-
const pattern = route.key.replace(/:[^/]+/g, '[^/]+')
49+
if (route?.key?.includes(':')) {
50+
const pattern = route?.key.replace(/:[^/]+/g, '[^/]+')
5151
const regex = new RegExp(`^${pattern}$`)
5252
return regex.test(pathToCheck)
5353
}

src/pages/layout/proContent/index.jsx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import React, { useState, useEffect, useRef } from 'react'
2-
import { Layout, FloatButton, theme, Space } from 'antd'
1+
import React, { useState, useEffect } from 'react'
2+
import { Layout, theme, Space } from 'antd'
33
import { useLocation } from 'react-router-dom'
4-
import { VerticalAlignTopOutlined } from '@ant-design/icons'
54
import { getKeyName } from '@utils/publicFn'
6-
import { useProTabContext } from '@app-hooks/proTabsContext'
7-
import ClockFace from '@stateless/ClockFace'
85
import ProBreadcrumb from './breadcrumb'
96
import ProTabs from '../proTabs'
107
import styles from './index.module.less'
@@ -13,7 +10,6 @@ const { Content, Header, Footer } = Layout
1310

1411
const ProContent = () => {
1512
const [tabActiveKey, setTabActiveKey] = useState('home')
16-
const { activeKey, panes } = useProTabContext()
1713
const [panesItem, setPanesItem] = useState({
1814
title: '',
1915
content: null,
@@ -22,8 +18,6 @@ const ProContent = () => {
2218
path: '',
2319
i18nKey: '',
2420
})
25-
26-
const pathRef = useRef('')
2721
const { pathname, search } = useLocation()
2822
const {
2923
token: { colorBgContainer, colorBgLayout },
@@ -32,19 +26,16 @@ const ProContent = () => {
3226
// pass full path (including search) so getKeyName can consider query params
3327
const full = search ? pathname + search : pathname
3428
const { tabKey, title, element, i18nKey } = getKeyName(full)
35-
const newPath = full
36-
pathRef.current = newPath
3729

3830
setPanesItem({
3931
title,
4032
content: element,
4133
key: tabKey,
4234
closable: tabKey !== '/',
43-
path: newPath,
35+
path: full,
4436
i18nKey,
4537
})
4638
setTabActiveKey(tabKey)
47-
// eslint-disable-next-line react-hooks/exhaustive-deps
4839
}, [pathname, search])
4940

5041
return (

0 commit comments

Comments
 (0)