route.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { NextRequest, NextResponse } from "next/server";
  2. import { restApiFetch } from "@/utils/bagisto";
  3. // import type { GiftListBody,FetchWrap } from '@/types/api/gift/lists';
  4. export async function GET(req: NextRequest) {
  5. try {
  6. // 从url取查询参数 id
  7. const addressId = req.nextUrl.searchParams.get("id");
  8. const apiUrl = addressId
  9. ? `/customer/token/address/${addressId}`
  10. : `/customer/token/address`;
  11. console.log("11111----:", apiUrl);
  12. const response = await restApiFetch<any>({
  13. api: apiUrl, //
  14. method: "GET",
  15. cache: "no-store",
  16. });
  17. return NextResponse.json(response.body,{
  18. status: response.status,
  19. });
  20. } catch (error) {
  21. return NextResponse.json(
  22. {
  23. message: "Network error",
  24. error: error instanceof Error ? error.message : error,
  25. },
  26. { status: 500 },
  27. );
  28. }
  29. }
  30. export async function POST(req: NextRequest) {
  31. try {
  32. const params = await req.json();
  33. const response = await restApiFetch<{
  34. data: any; // 这个是返回结果的数据类型,暂时写成any,具体看后端反的数据结构再改成确定的类型1
  35. variables: {
  36. first_name: string;
  37. last_name: string;
  38. email: string;
  39. phone: string;
  40. address: string;
  41. country: string;
  42. state: string;
  43. city: string;
  44. postcode: string;
  45. default_address: number;
  46. };
  47. }>({
  48. api: "/customer/token/address",
  49. method: "POST",
  50. cache: "no-store",
  51. variables: params,
  52. });
  53. // 打印后端原始返回结构
  54. console.log(
  55. "接口原始response.body =",
  56. JSON.stringify(response.body, null, 2),
  57. );
  58. return NextResponse.json(response.body,{
  59. status: response.status,
  60. });
  61. } catch (error) {
  62. console.log("/customer/token/address---", error); // 调试用
  63. return NextResponse.json(
  64. {
  65. message: "Network error",
  66. error: error instanceof Error ? error.message : error,
  67. },
  68. { status: 500 },
  69. );
  70. }
  71. }
  72. export async function PUT(req: NextRequest) {
  73. try {
  74. const params = await req.json();
  75. // 从url取查询参数 id
  76. const addressId = req.nextUrl.searchParams.get("id");
  77. const response = await restApiFetch<{
  78. data: any; // 这个是返回结果的数据类型,暂时写成any,具体看后端反的数据结构再改成确定的类型1
  79. variables: {
  80. first_name: string;
  81. last_name: string;
  82. email: string;
  83. phone: string;
  84. address: string;
  85. country: string;
  86. state: string;
  87. city: string;
  88. postcode: string;
  89. default_address: number;
  90. };
  91. }>({
  92. api: `/customer/token/address/${addressId}`,
  93. method: "PUT",
  94. cache: "no-store",
  95. variables: params,
  96. });
  97. // 打印后端原始返回结构
  98. console.log(
  99. "接口原始response.body =",
  100. JSON.stringify(response.body, null, 2),
  101. );
  102. return NextResponse.json(response.body,{
  103. status: response.status,
  104. });
  105. } catch (error) {
  106. console.log("/customer/token/address---", error); // 调试用
  107. return NextResponse.json(
  108. {
  109. message: "Network error",
  110. error: error instanceof Error ? error.message : error,
  111. },
  112. { status: 500 },
  113. );
  114. }
  115. }
  116. export async function DELETE(req: NextRequest) {
  117. try {
  118. const addressId = req.nextUrl.searchParams.get("id");
  119. const response = await restApiFetch<any>({
  120. api: `/customer/token/address/${addressId}`, //
  121. method: "DELETE",
  122. cache: "no-store",
  123. });
  124. return NextResponse.json(response.body,{
  125. status: response.status,
  126. });
  127. } catch (error) {
  128. return NextResponse.json(
  129. {
  130. message: "Network error",
  131. error: error instanceof Error ? error.message : error,
  132. },
  133. { status: 500 },
  134. );
  135. }
  136. }