Browse Source

fix: 首页初始化发送请求

Before 1 year ago
parent
commit
20071e1af9

+ 2 - 3
src/app/[locale]/(ordinary)/gameList/[gameListFlag]/page.tsx

@@ -1,10 +1,9 @@
 import { FC } from "react";
 
 interface Props {}
-export function generateStaticParams() {
-    return [{ slug: [""] }];
-}
+
 const GameListFlag: FC<Props> = (props) => {
+    console.log(`🎯🎯🎯🎯🎯-> in page.tsx on `, props);
     return (
         <div
             className={

+ 3 - 3
src/app/[locale]/page.tsx

@@ -1,16 +1,16 @@
+"use server";
 import HomeActions from "@/app/[locale]/_home/HomeActions";
 import HomeGames from "@/app/[locale]/_home/HomeGames";
 import Box from "@/components/Box";
 import Layout from "@/components/Layout";
 import { useTranslations } from "next-intl";
-import { FC } from "react";
 import HomeCard from "./_home/HomeCard";
 import HomeSearch from "./_home/HomeSearch";
 import HomeSwiper from "./_home/HomeSwiper";
 
-const App: FC = (props) => {
+const App = (props) => {
     const t = useTranslations("global");
-    console.log(`🎯🎯🎯🎯🎯-> in page.tsx on 13`);
+    console.log(`🎯🎯🎯🎯🎯-> in page.tsx on 13`, props);
     return (
         <Layout>
             <div>

+ 17 - 15
src/components/Header/index.tsx

@@ -1,9 +1,10 @@
 import { getUserMoneyApi } from "@/api/user";
 import { Link } from "@/i18n";
 import { useGlobalStore } from "@/stores";
+import { useRequest } from "ahooks";
 import clsx from "clsx";
 import { useTranslations } from "next-intl";
-import { FC, ReactNode, useEffect, useRef, useState } from "react";
+import { FC, ReactNode, useRef, useState } from "react";
 import HeaderTitle from "./HerderTitle";
 import styles from "./style.module.scss";
 /**
@@ -23,23 +24,24 @@ const DefaultHeader: FC<HeaderProps> = (props) => {
     const t = useTranslations("Header");
     const { token, userInfo } = useGlobalStore();
     const [money, setMoney] = useState(0);
-
     const timer = useRef<NodeJS.Timeout | null>(null);
 
-    useEffect(() => {
+    const getUserMoneyHandler = () => {
         if (token) {
-            timer.current = setInterval(() => {
-                getUserMoneyApi().then((res) => {
-                    setMoney(res.data.Score);
-                });
-            }, 5000);
+            return getUserMoneyApi();
         }
-        return () => {
-            clearTimeout(timer.current!);
-            timer.current = null;
-        };
-    }, [token]);
-
+        return Promise.reject();
+    };
+    useRequest(getUserMoneyHandler, {
+        pollingInterval: 3000,
+        pollingWhenHidden: true,
+        pollingErrorRetryCount: 3,
+        refreshDeps: [token],
+        onError: (error) => {},
+        onSuccess: (res) => {
+            setMoney(res.data.Score);
+        },
+    });
     return (
         <div className={styles.headerMain}>
             <div className={styles.headerLeft} onClick={menuHandler}>
@@ -64,7 +66,7 @@ const DefaultHeader: FC<HeaderProps> = (props) => {
                     ) : (
                         <>
                             <Link href={"/login?redirect=/"}>{t("login")}</Link>
-                            <Link href={"/login?redirect=/"} className={styles.rightActive}>
+                            <Link href={"/register?redirect=/"} className={styles.rightActive}>
                                 {t("register")}
                             </Link>
                         </>

+ 3 - 3
src/utils/server/axios.ts

@@ -50,7 +50,7 @@ export default class Request {
                     if (globalStore) {
                         let userInfo = JSON.parse(globalStore);
                         config.headers["Token"] = userInfo?.state?.token;
-                        // config.headers["lang"] = 111;
+                        config.headers["language"] = userInfo?.state?.lang;
                     }
                     if (config && config?.toast) {
                         Toast.show({
@@ -61,7 +61,7 @@ export default class Request {
                         });
                     }
                 }
-                
+
                 return config;
             },
             (error) => {
@@ -82,7 +82,6 @@ export default class Request {
                 return res;
             },
             (error) => {
-
                 if (responseInterceptorCatch) {
                     responseInterceptorCatch(error);
                 }
@@ -106,6 +105,7 @@ export default class Request {
                     if (res && res.data && res.data.code === 200) {
                         resolve(res?.data);
                     } else {
+                        Toast.show(res.data?.msg || "请求失败");
                         reject(res);
                     }
                 })