Ver código fonte

生日页接口逻辑

zhangzf 6 horas atrás
pai
commit
4ce772d189

+ 36 - 17
src/app/(public)/customer/(has-dashboard)/account/birthday/page.tsx

@@ -1,23 +1,44 @@
-"use client";
-// import { useState, useEffect } from "react";
-// import { clientFetch } from "@/lib/restApiClient";
 import Image from "next/image";
+import { restApiFetch } from "@utils/bagisto/index";
 
-const BIRTH_MONTH = 3; 
-const BIRTH_DAY = 22;
-
+// 格式化日期为 "Apr. 22" 这种英文短格式
+function formatBirthdayDisplay(isoDate: string) {
+  if (!isoDate) return "";
+  const date = new Date(isoDate);
+  // 月份缩写 + 日期
+  return date.toLocaleDateString("en-US", {
+    month: "short",
+    day: "numeric",
+  });
+}
 
-function isTodayBirthday(): boolean {
+// 判断今天是否是生日(只对比月、日,忽略年份)
+function isTodayBirthday(isoDate: string): boolean {
+  if (!isoDate) return false;
   const now = new Date();
+  const birthDate = new Date(isoDate);
   const currentMonth = now.getMonth();
   const currentDate = now.getDate();
-  return currentMonth === BIRTH_MONTH && currentDate === BIRTH_DAY;
+  const birthMonth = birthDate.getMonth();
+  const birthDay = birthDate.getDate();
+  return currentMonth === birthMonth && currentDate === birthDay;
 }
-export default function GetPoints() {
-  const isBirthdayToday = isTodayBirthday();
+
+export default async function GetPoints() {
+  const res = await restApiFetch({
+    api: `/customer/birthday`,
+    method: "GET",
+    isRoute: false,
+  });
+  console.log("res----------------aaa", res);
+
+  // 从接口拿生日字符串,示例:"1998-10-08"
+  const birthDateStr = res?.body?.data?.date_of_birth ?? "";
+  const isBirthdayToday = isTodayBirthday(birthDateStr);
+  const birthdayDisplayText = formatBirthdayDisplay(birthDateStr);
 
   return (
-    <div className="w-full min-h-screen bg-white  h-full">
+    <div className="w-full min-h-screen bg-white h-full">
       <h1 className="font-normal text-2xl leading-ly-28 mb-6">
         Birthday Rewards
       </h1>
@@ -35,12 +56,10 @@ export default function GetPoints() {
               : "Birthday upcoming notice"
           }
           fill
-          className="object‑contain"
+          className="object-contain"
           priority
         />
-
         {isBirthdayToday ? (
-        
           <>
             <div className="absolute top-[44%] left-1/2 -translate-x-1/2 w-[62%] text-center text-white text-md md:text-lg font-medium">
               A Little Birthday Gift, Just for You
@@ -53,15 +72,15 @@ export default function GetPoints() {
             </div>
           </>
         ) : (
-       
           <>
             <div className="absolute top-[51%] left-1/2 -translate-x-1/2 w-[60%] text-center text-white text-md md:text-lg leading-relaxed">
               <p>Your special day is coming!</p>
-              <p>We&apos;ll be celebrating you all month long.</p>
+              <p>We'll be celebrating you all month long.</p>
               <p>Your $15 Birthday Gift Card will be ready soon.</p>
             </div>
+            {/* 这里动态渲染生日,不再写死Apr.22 */}
             <div className="absolute top-[75%] left-1/2 -translate-x-1/2 text-white text-xl font-bold">
-              [Apr. 22]
+              [{birthdayDisplayText}]
             </div>
           </>
         )}

+ 36 - 0
src/app/api/customer/birthday/route.ts

@@ -0,0 +1,36 @@
+import { NextResponse } from "next/server";
+import { restApiFetch } from "@/utils/bagisto";
+
+
+export async function GET() {
+  try {
+    //  const { id } = await params; // 解包拿到订单ID
+  
+
+   const apiUrl = `/customer/birthday`;
+
+    console.log("最终请求API地址:", apiUrl);
+
+    const response = await restApiFetch<any>({
+      api: apiUrl,
+      method: "GET",
+      cache: "no-store",
+
+    });
+
+    return NextResponse.json(response.body,{
+        status: response.status,
+    });
+
+  } catch (error) {
+
+
+    return NextResponse.json(
+      {
+        message: "Network error",
+        error: error instanceof Error ? error.message : error,
+      },
+      { status: 500 }
+    );
+  }
+}