diff --git a/src/hooks/useAutoLogin.ts b/src/hooks/useAutoLogin.ts new file mode 100644 index 0000000..eb1fc39 --- /dev/null +++ b/src/hooks/useAutoLogin.ts @@ -0,0 +1,15 @@ +import { useCallback } from "react" +import { useNavigate } from "react-router"; + +const useAutoLogin = () => { + const navigate = useNavigate(); + const login = useCallback((redirect?: string) => { + if (redirect) { + sessionStorage.setItem('redirect', redirect); + } + navigate('/user-center?autoSignIn=true'); + }, []); + return { login }; +} + +export default useAutoLogin; diff --git a/src/page/FavPlayersPage.tsx b/src/page/FavPlayersPage.tsx index 8938eef..9299b09 100644 --- a/src/page/FavPlayersPage.tsx +++ b/src/page/FavPlayersPage.tsx @@ -2,10 +2,11 @@ import { Avatar, Button, Card, Divider, Flex, Image, Popconfirm, Radio, Segmente import { useFavPlayerStore, type FavPlayer } from "../store/useFavPlayerStore"; import { useNavigate } from "react-router"; import { useCallback, useEffect, useMemo, useState } from "react"; -import { useLogto } from "@logto/react"; -import { DeleteOutlined, LoginOutlined, UploadOutlined } from "@ant-design/icons"; +import { useLogto, type IdTokenClaims } from "@logto/react"; +import { DeleteOutlined, LoginOutlined, SyncOutlined, UploadOutlined } from "@ant-design/icons"; import { useRequest } from "ahooks"; import type { XCXProfile } from "../types"; +import useAutoLogin from "../hooks/useAutoLogin"; enum SortType { DEFAULT = '注册时间', @@ -21,6 +22,8 @@ export function FavePlayersPage() { const { favMap, unFav } = useFavPlayerStore(state => state); const [sortType, setSortType] = useState(SortType.DEFAULT); const [showType, setShowType] = useState(ShowType.LOCAL); + const [claims, setClaims] = useState(); + const { login } = useAutoLogin(); const localList = Object.values(favMap); const favListRequest = useRequest(async (aud: string) => { const res = await fetch(`/api/fav/${aud}`); @@ -47,6 +50,7 @@ export function FavePlayersPage() { if (!isAuthenticated) return; const id = setTimeout(async () => { const claims = await getIdTokenClaims(); + setClaims(claims); favListRequest.runAsync(claims?.aud!); }, 300); return () => clearTimeout(id); @@ -64,14 +68,11 @@ export function FavePlayersPage() { const handleClearLocal = useCallback(() => { list.forEach(e => unFav(e.uid)); }, []); - useEffect(() => { - setShowType(isAuthenticated ? ShowType.ACCOUNT : ShowType.LOCAL); - }, [isAuthenticated]); return (
收藏的球员 -
+ -
+ ) } diff --git a/src/services/xcxApi.ts b/src/services/xcxApi.ts index 0fedf11..f452371 100644 --- a/src/services/xcxApi.ts +++ b/src/services/xcxApi.ts @@ -1,5 +1,6 @@ import type { GamesData, XCXFindUserResp, XCXMember, XCXProfile, XCXTag } from "../types"; import { BASE_URL } from "../utils/common"; +import { redis } from "../utils/server"; const XCX_BASE_URL = `${BASE_URL}/xcx/public/index.php`; @@ -33,8 +34,14 @@ export class XCXAPI { } async getAdvProfile(uid: string) { - const url = `/api/User/adv_profile?uid=${uid}`; - return this.#fetch(url); + const cacheProfile = await redis.get(`my-kaiqiuwang-profile:${uid}`); + if (!cacheProfile) { + const url = `/api/User/adv_profile?uid=${uid}`; + const profile = await this.#fetch(url); + await redis.setex(`my-kaiqiuwang-profile:${uid}`, 60 * 10, JSON.stringify(profile)); + return profile; + } + return JSON.parse(cacheProfile); } async getPlayerTags(uid: string) {