Selaa lähdekoodia

fix 未登录时提示语修改

jax 5 kuukautta sitten
vanhempi
commit
e0db9621c6

+ 1 - 1
src/app/middleware/authMiddleware.js

@@ -15,7 +15,7 @@ export function withAuth(handler) {
       if (!token) {
         return setCORSHeaders(
           NextResponse.json(
-            { success: false, error: "缺少token" },
+            { success: false, error: "请先登录" },
             { status: 401 }
           )
         );

+ 11 - 2
src/app/ui/BasketballMatch.jsx

@@ -42,8 +42,9 @@ const BasketballMatch = ({ selectedDayMatches, currentUser }) => {
 
   // 处理预测变更
   const handlePredictionChange = async (matchId, type, value) => {
-    console.log(222, matchId, type, value);
-    console.log(222, predictions);
+    if (checkUserLogin()) {
+      return;
+    }
     const match = selectedDayMatches.find((m) => m._id === matchId);
     if (match.status === "进行中") {
       setAlert({ type: "error", message: "比赛已开始,无法提交预测" });
@@ -151,6 +152,14 @@ const BasketballMatch = ({ selectedDayMatches, currentUser }) => {
     }
   };
 
+  const checkUserLogin = () => {
+    if (!currentUser) {
+      setAlert({ type: "error", message: "请先登录" });
+      return true;
+    }
+    return false;
+  };
+
   return (
     <div className="bg-blue-600 text-white p-4 max-w-md mx-auto min-h-screen">
       {selectedDayMatches.map((match) => {

+ 33 - 17
src/app/ui/FootballMatch.jsx

@@ -32,30 +32,30 @@ const FootballMatch = ({ selectedDayMatches, currentUser }) => {
 
   const [selectedFirstTeamToScore, setSelectedFirstTeamToScore] = useState(""); // ["home", "away", "no_goal"]
 
-  console.log("selectedDayMatches", selectedDayMatches);
-  console.log("selectedFirstTeamToScore", selectedFirstTeamToScore);
+  // console.log("selectedDayMatches", selectedDayMatches);
+  // console.log("selectedFirstTeamToScore", selectedFirstTeamToScore);
 
-  console.log(
-    "selectedFirstTeamToScoreOfMatch",
-    selectedFirstTeamToScoreOfMatch
-  );
-  console.log("selectedMatchId", selectedMatchId);
-  console.log("selectedMatch", selectedMatch);
+  // console.log(
+  //   "selectedFirstTeamToScoreOfMatch",
+  //   selectedFirstTeamToScoreOfMatch
+  // );
+  // console.log("selectedMatchId", selectedMatchId);
+  // console.log("selectedMatch", selectedMatch);
 
-  console.log("selectedWinTeams", selectedWinTeams);
-  console.log("totalGoalCountOfMatch", totalGoalCountOfMatch);
+  // console.log("selectedWinTeams", selectedWinTeams);
+  // console.log("totalGoalCountOfMatch", totalGoalCountOfMatch);
 
   const selectedWinTeam = selectedWinTeams[selectedMatchId] || "";
 
-  console.log("selectedWinTeam", selectedWinTeam);
+  // console.log("selectedWinTeam", selectedWinTeam);
 
-  console.log("predictions", predictions);
+  // console.log("predictions", predictions);
 
   const predictionMap = predictions.reduce((map, prediction) => {
     map[prediction.matchId] = prediction;
     return map;
   }, {});
-  console.log("predictionMap", predictionMap);
+  // console.log("predictionMap", predictionMap);
 
   useEffect(() => {
     console.log("currentUser", currentUser);
@@ -90,6 +90,9 @@ const FootballMatch = ({ selectedDayMatches, currentUser }) => {
   ]);
 
   const incrementGoal = (matchId, whoWillWin, totalGoals, matchStatus) => {
+    if (checkUserLogin()) {
+      return;
+    }
     if (matchStatus === "进行中") {
       setAlert({ type: "error", message: "比赛已开始,无法提交预测" });
       return;
@@ -117,6 +120,9 @@ const FootballMatch = ({ selectedDayMatches, currentUser }) => {
   };
 
   const decrementGoal = (matchId, whoWillWin, totalGoals, matchStatus) => {
+    if (checkUserLogin()) {
+      return;
+    }
     if (matchStatus === "进行中") {
       setAlert({ type: "error", message: "比赛已开始,无法提交预测" });
       return;
@@ -144,6 +150,9 @@ const FootballMatch = ({ selectedDayMatches, currentUser }) => {
 
   // 选择who wins
   const handleWinTeamSelect = (matchId, selectedTeam, matchStatus) => {
+    if (checkUserLogin()) {
+      return;
+    }
     if (matchStatus === "进行中") {
       setAlert({ type: "error", message: "比赛已开始,无法提交预测" });
       return;
@@ -170,15 +179,14 @@ const FootballMatch = ({ selectedDayMatches, currentUser }) => {
 
   // 打开 FirstScoreTeam 选择框
   const openSelectFirstTeamModal = (match, whoWillWin, firstTeamToScore) => {
-    console.log("match", match);
+    if (checkUserLogin()) {
+      return;
+    }
     if (match.status === "进行中") {
       setAlert({ type: "error", message: "比赛已开始,无法提交预测" });
       return;
     }
     const matchId = match._id;
-    console.log("whoWillWin", whoWillWin);
-    console.log("matchId", matchId);
-    console.log("firstTeamToScore", firstTeamToScore);
     setSelectedMatchId(matchId);
     firstTeamToScore
       ? setSelectedFirstTeamToScore(firstTeamToScore)
@@ -264,6 +272,14 @@ const FootballMatch = ({ selectedDayMatches, currentUser }) => {
     }
   };
 
+  const checkUserLogin = () => {
+    if (!currentUser) {
+      setAlert({ type: "error", message: "请先登录" });
+      return true;
+    }
+    return false;
+  };
+
   return (
     <div className="bg-blue-600 text-white p-4 max-w-md mx-auto  min-h-screen">
       <div>

+ 4 - 4
src/app/ui/MatchDays.jsx

@@ -16,7 +16,7 @@ const MatchDays = ({ onSelectMatch, type }) => {
   }, [type]);
 
   useEffect(() => {
-    console.log("matchDays", matchDays);
+    // console.log("matchDays", matchDays);
     if (matchDays.length > 0) {
       getMatchesByDate(matchDays[0].date);
     }
@@ -33,7 +33,7 @@ const MatchDays = ({ onSelectMatch, type }) => {
         throw new Error("Network response was not ok");
       }
       const res = await response.json();
-      console.log("MatchDays", res.data);
+      // console.log("MatchDays", res.data);
       setMatchDays(res.data);
     } catch (error) {
       console.error("Error fetching match days:", error);
@@ -43,7 +43,7 @@ const MatchDays = ({ onSelectMatch, type }) => {
 
   // 根据日期获取比赛
   async function getMatchesByDate(date) {
-    console.log("date", date);
+    // console.log("date", date);
     try {
       let url = `/api/match?action=getMatchesByDate&date=${date}`;
       if (type) {
@@ -56,7 +56,7 @@ const MatchDays = ({ onSelectMatch, type }) => {
         throw new Error("Network response was not ok");
       }
       const res = await response.json();
-      console.log("Matches", res.data, type);
+      // console.log("Matches", res.data, type);
       onSelectMatch(res.data);
     } catch (error) {
       console.error("Error fetching matches by date:", error);