Skip to content

Commit b0c3791

Browse files
author
刘健
committed
Merge branch 'feature/merge' into 'main'
fix: login page redirect multiple times (#166) See merge request apipark/APIPark!158
2 parents d5af1c8 + a6105cf commit b0c3791

File tree

12 files changed

+241
-268
lines changed

12 files changed

+241
-268
lines changed

frontend/packages/common/src/components/aoplatform/LanguageSetting.tsx

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,66 @@ import { Icon } from '@iconify/react/dist/iconify.js'
44
import { Button, Dropdown } from 'antd'
55
import { memo, useEffect, useMemo } from 'react'
66

7+
const LanguageItems = [
8+
{
9+
key: 'en-US',
10+
label: (
11+
<Button key="en" type="text" className="flex items-center p-0 bg-transparent border-none">
12+
English
13+
</Button>
14+
),
15+
title: 'English'
16+
},
17+
{
18+
key: 'ja-JP',
19+
label: (
20+
<Button key="jp" type="text" className="flex items-center p-0 bg-transparent border-none">
21+
日本語
22+
</Button>
23+
),
24+
title: '日本語'
25+
},
26+
{
27+
key: 'zh-TW',
28+
label: (
29+
<Button key="tw" type="text" className="flex items-center p-0 bg-transparent border-none">
30+
繁體中文
31+
</Button>
32+
),
33+
title: '繁體中文'
34+
},
35+
{
36+
key: 'zh-CN',
37+
label: (
38+
<Button key="cn" type="text" className="flex items-center p-0 bg-transparent border-none">
39+
简体中文
40+
</Button>
41+
),
42+
title: '简体中文'
43+
}
44+
]
745
const LanguageSetting = ({ mode = 'light' }: { mode?: 'dark' | 'light' }) => {
846
const { dispatch, state } = useGlobalContext()
9-
const items = [
10-
{
11-
key: 'en-US',
12-
label: (
13-
<Button key="en" type="text" className="flex items-center p-0 bg-transparent border-none">
14-
English
15-
</Button>
16-
),
17-
title: 'English'
18-
},
19-
{
20-
key: 'ja-JP',
21-
label: (
22-
<Button key="jp" type="text" className="flex items-center p-0 bg-transparent border-none">
23-
日本語
24-
</Button>
25-
),
26-
title: '日本語'
27-
},
28-
{
29-
key: 'zh-TW',
30-
label: (
31-
<Button key="tw" type="text" className="flex items-center p-0 bg-transparent border-none">
32-
繁體中文
33-
</Button>
34-
),
35-
title: '繁體中文'
36-
},
37-
{
38-
key: 'zh-CN',
39-
label: (
40-
<Button key="cn" type="text" className="flex items-center p-0 bg-transparent border-none">
41-
简体中文
42-
</Button>
43-
),
44-
title: '简体中文'
45-
}
46-
]
4747

48-
const langLabel = useMemo(() => items.find((item) => item?.key === state.language)?.title, [state.language])
48+
const langLabel = useMemo(() => LanguageItems.find((item) => item?.key === state.language)?.title, [state.language])
4949

5050
useEffect(() => {
5151
const savedLang = i18n.language || sessionStorage.getItem('i18nextLng')
5252
if (savedLang && state.language !== savedLang) {
5353
dispatch({ type: 'UPDATE_LANGUAGE', language: savedLang })
5454
} else if (!savedLang) {
5555
const browserLang = navigator.language
56-
const supportedLang = items.find((item) => item.key === browserLang) ? browserLang : 'zh-CN'
56+
const supportedLang = LanguageItems.find((item) => item.key === browserLang) ? browserLang : 'zh-CN'
57+
if (state.language === supportedLang) return
5758
dispatch({ type: 'UPDATE_LANGUAGE', language: supportedLang })
5859
i18n.changeLanguage(supportedLang)
5960
}
6061
}, [])
61-
6262
return (
6363
<Dropdown
6464
trigger={['hover']}
6565
menu={{
66-
items,
66+
items: LanguageItems,
6767
style: { minWidth: '80px' },
6868
onClick: (e) => {
6969
const { key } = e

frontend/packages/common/src/components/aoplatform/WithPermission.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { PERMISSION_DEFINITION } from '@common/const/permissions'
2+
import { $t } from '@common/locales'
13
import { Button, Tooltip, Upload } from 'antd'
24
import { ReactElement, cloneElement, useEffect, useMemo, useState } from 'react'
35
import { useGlobalContext } from '../../contexts/GlobalStateContext'
4-
import { PERMISSION_DEFINITION } from '@common/const/permissions'
5-
import { $t } from '@common/locales'
66

77
type WithPermissionProps = {
88
access?: string | string[]

frontend/packages/common/src/contexts/GlobalStateContext.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ export const GlobalProvider: FC<{ children: ReactNode }> = ({ children }) => {
342342
updateDate: '2024-07-01',
343343
powered: 'Powered by https://apipark.com',
344344
mainPage: '/guide/page',
345-
language: 'en-US',
345+
language: sessionStorage.getItem('i18nextLng') || 'en-US',
346346
pluginsLoaded: false
347347
})
348348
const [accessData, setAccessData] = useState<Map<string, string[]>>(new Map())
@@ -510,7 +510,7 @@ export const useGlobalContext = () => {
510510
updateDate: '',
511511
powered: '',
512512
mainPage: '',
513-
language: 'en-US',
513+
language: sessionStorage.getItem('i18nextLng') || 'en-US',
514514
pluginsLoaded: false
515515
},
516516
dispatch: () => {},

frontend/packages/common/src/locales/index.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import i18n from 'i18next'
2-
import { initReactI18next } from 'react-i18next'
2+
import { initReactI18next, useTranslation } from 'react-i18next'
33
// i18next-browser-languagedetector插件 这是一个 i18next 语言检测插件,用于检测浏览器中的用户语言,
44
import crc32 from 'crc/crc32'
55
import LanguageDetector from 'i18next-browser-languagedetector'
@@ -39,23 +39,22 @@ i18n
3939
.init({
4040
// 初始化
4141
resources, // 本地多语言数据
42-
// fallbackLng: config.lang, // 默认当前环境的语言
42+
supportedLngs: ['zh-CN', 'en-US', 'zh-TW', 'ja-JP'],
4343
detection: {
4444
caches: ['localStorage', 'sessionStorage', 'cookie']
4545
}
4646
})
4747

4848
// --------这里是i18next-scanner新增的配置-------------
49+
// 用于非 React 组件中的翻译
4950
export const $t = (key: string, params?: any[]): string => {
50-
const hashKey = `K${crc32(key).toString(16)}` // 将中文转换成crc32格式去匹配对应的json语言包
51+
// 将中文转换成crc32格式去匹配对应的json语言包
52+
const hashKey = `K${crc32(key).toString(16)}`
5153
let words = i18n.t(hashKey)
52-
// const { t } = useTranslation(); // 通过hooks
53-
// let words = t(hashKey);
5454
if (words === hashKey) {
5555
words = key
5656
}
5757

58-
// 配置传递参数的场景, 目前仅支持数组,可在此拓展
5958
if (Array.isArray(params)) {
6059
const reg = /\((\d)\)/g
6160
words = words.replace(reg, (a: string, b: number) => {
@@ -65,4 +64,25 @@ export const $t = (key: string, params?: any[]): string => {
6564
return words
6665
}
6766

67+
// 用于 React 组件中的翻译
68+
export const useI18n = () => {
69+
const { t } = useTranslation()
70+
71+
return (key: string, params?: any[]): string => {
72+
const hashKey = `K${crc32(key).toString(16)}`
73+
let words = t(hashKey)
74+
if (words === hashKey) {
75+
words = key
76+
}
77+
78+
if (Array.isArray(params)) {
79+
const reg = /\((\d)\)/g
80+
words = words.replace(reg, (a: string, b: number) => {
81+
return params[b]
82+
})
83+
}
84+
return words
85+
}
86+
}
87+
6888
export default i18n

frontend/packages/common/src/utils/permission.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ export const checkAccess: (access: AccessDataType, accessData: Map<string, strin
1818
if (accLevel === 'team') {
1919
accessSet = new Set(Array.from(accessSet).concat(accessData?.get('team') || []))
2020
}
21-
return accessSet!.size > 0 ? hasIntersection(neededBackendAccessArr, accessSet) : false
21+
if (!accessSet!.size) {
22+
return false
23+
}
24+
const hasAccess = hasIntersection(neededBackendAccessArr, accessSet)
25+
return hasAccess
2226
}
2327

2428
const hasIntersection = (arr1: string[], set1: Set<string>) => {

frontend/packages/core/src/App.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,15 @@ function App() {
151151
form={{ validateMessages }}
152152
>
153153
<PluginEventHubProvider>
154-
<AppAntd className="h-full" message={{ maxCount: 1 }}>
155-
<PluginSlotHubProvider>
156-
<GlobalProvider>
154+
<GlobalProvider>
155+
<AppAntd className="h-full" message={{ maxCount: 1 }}>
156+
<PluginSlotHubProvider>
157157
<BreadcrumbProvider>
158158
<RenderRoutes />
159159
</BreadcrumbProvider>
160-
</GlobalProvider>
161-
</PluginSlotHubProvider>
162-
</AppAntd>
160+
</PluginSlotHubProvider>
161+
</AppAntd>
162+
</GlobalProvider>
163163
</PluginEventHubProvider>
164164
</ConfigProvider>
165165
</StyleProvider>

frontend/packages/core/src/pages/aiSetting/AiSettingList.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import InsidePage from '@common/components/aoplatform/InsidePage'
2-
import { $t } from '@common/locales'
2+
import { useI18n } from '@common/locales'
33
import { Tabs } from 'antd'
44
import { useEffect, useState } from 'react'
55
import { useSearchParams } from 'react-router-dom'
@@ -10,6 +10,7 @@ import { AiSettingProvider } from './contexts/AiSettingContext'
1010
const CONTENT_STYLE = { height: 'calc(-300px + 100vh)' } as const
1111

1212
const AiSettingContent = () => {
13+
const $t = useI18n()
1314
const [searchParams, setSearchParams] = useSearchParams()
1415
const [activeKey, setActiveKey] = useState(searchParams.get('status') === 'unconfigure' ? 'config' : 'flow')
1516

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
1-
import InsidePage from "@common/components/aoplatform/InsidePage";
2-
import { $t } from "@common/locales";
3-
import ServiceCategory from "./ServiceCategory";
4-
import ApiRequestSetting from "./ApiRequestSetting";
5-
import { Row, Col } from "antd";
1+
import InsidePage from '@common/components/aoplatform/InsidePage'
2+
import { useI18n } from '@common/locales'
3+
import { Col, Row } from 'antd'
4+
import ApiRequestSetting from './ApiRequestSetting'
5+
import ServiceCategory from './ServiceCategory'
66

7-
export default function CommonPage(){
8-
return (
9-
<InsidePage
10-
pageTitle={$t('常规设置')}
11-
showBorder={false}
12-
contentClassName="pr-PAGE_INSIDE_X"
13-
scrollPage={false}
14-
>
15-
<Row className="mb-btnybase" >
16-
<Col >
17-
<span className="font-bold mr-[13px]">
18-
{$t('API 请求设置')}
19-
</span>
20-
</Col>
21-
</Row>
22-
<ApiRequestSetting />
23-
<Row className="mb-btnybase mt-[40px]">
24-
<Col >
25-
<span className="font-bold mr-[13px]">
26-
{$t('服务分类')}
27-
</span>
28-
</Col>
29-
</Row>
30-
<ServiceCategory />
31-
</InsidePage>
32-
)
33-
}
7+
export default function CommonPage() {
8+
const $t = useI18n()
9+
10+
return (
11+
<InsidePage pageTitle={$t('常规设置')} showBorder={false} contentClassName="pr-PAGE_INSIDE_X" scrollPage={false}>
12+
<Row className="mb-btnybase">
13+
<Col>
14+
<span className="font-bold mr-[13px]">{$t('API 请求设置')}</span>
15+
</Col>
16+
</Row>
17+
<ApiRequestSetting />
18+
<Row className="mb-btnybase mt-[40px]">
19+
<Col>
20+
<span className="font-bold mr-[13px]">{$t('服务分类')}</span>
21+
</Col>
22+
</Row>
23+
<ServiceCategory />
24+
</InsidePage>
25+
)
26+
}

0 commit comments

Comments
 (0)