route.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { NextRequest, NextResponse } from "next/server";
  2. import { restApiFetch } from "@/utils/bagisto";
  3. export async function GET(req: NextRequest) {
  4. try {
  5. // 👇 取出所有前端传过来的查询参数
  6. const searchParams = req.nextUrl.searchParams;
  7. const track_number = searchParams.get("track_number");
  8. // 👇 构建查询参数数组(自动过滤空值)
  9. const queryParts: string[] = [];
  10. if (track_number) queryParts.push(`track_number=${track_number}`);
  11. // 👇 最终拼接 URL
  12. let apiUrl = "/customer/orders/tracking";
  13. if (queryParts.length > 0) {
  14. apiUrl += "?" + queryParts.join("&");
  15. }
  16. console.log("最终请求API地址:", apiUrl);
  17. const response = await restApiFetch<any>({
  18. api: apiUrl,
  19. method: "GET",
  20. cache: "no-store",
  21. });
  22. return NextResponse.json(response.body,{
  23. status: response.status
  24. });
  25. } catch (error) {
  26. return NextResponse.json(
  27. {
  28. message: "Network error",
  29. error: error instanceof Error ? error.message : error,
  30. },
  31. { status: 500 }
  32. );
  33. }
  34. }