-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved the performance of the backend interface.
- Loading branch information
1 parent
83802db
commit 07d7ac8
Showing
27 changed files
with
570 additions
and
567 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 68 additions & 69 deletions
137
frontend/desktop/src/components/signin/auth/useWechat.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,77 @@ | ||
import {getWechatQR, getWechatResult} from '@/api/platform'; | ||
import { getWechatQR, getWechatResult } from '@/api/platform'; | ||
import useSessionStore from '@/stores/session'; | ||
import {Box, Image, useToast} from '@chakra-ui/react'; | ||
import {useQuery} from '@tanstack/react-query'; | ||
import {useTranslation} from 'next-i18next'; | ||
import {useRouter} from 'next/router'; | ||
import React, {useCallback, useEffect} from 'react'; | ||
import {UserInfo} from "@/api/auth"; | ||
import {jwtDecode} from "jwt-decode"; | ||
import {AccessTokenPayload} from "@/types/token"; | ||
import { Box, Image, useToast } from '@chakra-ui/react'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { useTranslation } from 'next-i18next'; | ||
import { useRouter } from 'next/router'; | ||
import React, { useCallback, useEffect } from 'react'; | ||
import { UserInfo } from '@/api/auth'; | ||
import { jwtDecode } from 'jwt-decode'; | ||
import { AccessTokenPayload } from '@/types/token'; | ||
|
||
export default function useWechat() { | ||
const {t} = useTranslation(); | ||
const router = useRouter(); | ||
const {setSession, setToken} = useSessionStore(); | ||
const { t } = useTranslation(); | ||
const router = useRouter(); | ||
const { setSession, setToken } = useSessionStore(); | ||
|
||
const [wechatInfo, setwechatInfo] = React.useState<{ | ||
code: string; | ||
codeUrl: string; | ||
}>(); | ||
const [wechatInfo, setwechatInfo] = React.useState<{ | ||
code: string; | ||
codeUrl: string; | ||
}>(); | ||
|
||
const login = React.useCallback(() => { | ||
getWechatQR().then((res) => { | ||
console.log(res); | ||
setwechatInfo(res.data); | ||
}); | ||
}, []); | ||
|
||
const {data, isSuccess} = useQuery( | ||
['getWechatResult', wechatInfo?.code], | ||
() => getWechatResult({code: wechatInfo?.code || ''}), | ||
{ | ||
refetchInterval: 3 * 1000, | ||
enabled: !!wechatInfo?.code, | ||
} | ||
); | ||
useEffect(() => { | ||
(async () => { | ||
if (isSuccess && data && data.code === 200 && data.data) { | ||
const regionUserToken = data.data.token; | ||
setToken(regionUserToken); | ||
const infoData = await UserInfo(); | ||
const payload = jwtDecode<AccessTokenPayload>(regionUserToken); | ||
setSession({ | ||
token: regionUserToken, | ||
user: { | ||
k8s_username: payload.userCrName, | ||
name: infoData.data?.info.nickname || '', | ||
avatar: infoData.data?.info.avatarUri || '', | ||
nsid: payload.workspaceId, | ||
ns_uid: payload.workspaceUid, | ||
userCrUid: payload.userCrUid, | ||
userUid: payload.userUid, | ||
userId: payload.userId | ||
}, | ||
// @ts-ignore | ||
kubeconfig: result.data.kubeconfig | ||
}); | ||
return router.replace('/'); | ||
} | ||
})() | ||
}, [data, isSuccess]); | ||
const login = React.useCallback(() => { | ||
getWechatQR().then((res) => { | ||
console.log(res); | ||
setwechatInfo(res.data); | ||
}); | ||
}, []); | ||
|
||
const { data, isSuccess } = useQuery( | ||
['getWechatResult', wechatInfo?.code], | ||
() => getWechatResult({ code: wechatInfo?.code || '' }), | ||
{ | ||
refetchInterval: 3 * 1000, | ||
enabled: !!wechatInfo?.code | ||
} | ||
); | ||
useEffect(() => { | ||
(async () => { | ||
if (isSuccess && data && data.code === 200 && data.data) { | ||
const regionUserToken = data.data.token; | ||
setToken(regionUserToken); | ||
const infoData = await UserInfo(); | ||
const payload = jwtDecode<AccessTokenPayload>(regionUserToken); | ||
setSession({ | ||
token: regionUserToken, | ||
user: { | ||
k8s_username: payload.userCrName, | ||
name: infoData.data?.info.nickname || '', | ||
avatar: infoData.data?.info.avatarUri || '', | ||
nsid: payload.workspaceId, | ||
ns_uid: payload.workspaceUid, | ||
userCrUid: payload.userCrUid, | ||
userUid: payload.userUid, | ||
userId: payload.userId | ||
}, | ||
// @ts-ignore | ||
kubeconfig: result.data.kubeconfig | ||
}); | ||
return router.replace('/'); | ||
} | ||
})(); | ||
}, [data, isSuccess]); | ||
|
||
const WechatComponent = useCallback( | ||
() => ( | ||
<Box p={5}> | ||
{wechatInfo?.codeUrl && <Image w="200px" src={wechatInfo?.codeUrl} alt="qrcode"></Image>} | ||
</Box> | ||
), | ||
[wechatInfo?.codeUrl] | ||
); | ||
const WechatComponent = useCallback( | ||
() => ( | ||
<Box p={5}> | ||
{wechatInfo?.codeUrl && <Image w="200px" src={wechatInfo?.codeUrl} alt="qrcode"></Image>} | ||
</Box> | ||
), | ||
[wechatInfo?.codeUrl] | ||
); | ||
|
||
return { | ||
login, | ||
WechatComponent | ||
}; | ||
return { | ||
login, | ||
WechatComponent | ||
}; | ||
} |
Oops, something went wrong.