route.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 page = searchParams.get("page");
  8. const limit = searchParams.get("limit");
  9. const status = searchParams.get("status");
  10. // 👇 构建查询参数数组(自动过滤空值)
  11. const queryParts: string[] = [];
  12. if (page) queryParts.push(`page=${page}`);
  13. if (limit) queryParts.push(`limit=${limit}`);
  14. if (status) queryParts.push(`status=${status}`);
  15. // 👇 最终拼接 URL
  16. let apiUrl = "/customer/orders";
  17. if (queryParts.length > 0) {
  18. apiUrl += "?" + queryParts.join("&");
  19. }
  20. console.log("最终请求API地址:", apiUrl);
  21. const response = await restApiFetch<any>({
  22. api: apiUrl,
  23. method: "GET",
  24. cache: "no-store",
  25. });
  26. return NextResponse.json(response.body,{
  27. status: response.status
  28. });
  29. } catch (error) {
  30. return NextResponse.json(
  31. {
  32. message: "Network error",
  33. error: error instanceof Error ? error.message : error,
  34. },
  35. { status: 500 }
  36. );
  37. }
  38. }