From 1d30897ab6ae138439a6a7a1be6ede342a8dd53e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=BF=A0=E6=9E=97?= <170083662@qq.com> Date: Thu, 25 Jun 2026 00:59:49 +0800 Subject: [PATCH] =?UTF-8?q?fix(shopUserAddress):=20=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E5=A4=9A=E7=A7=8D=E5=9C=B0=E5=9D=80=E5=88=97=E8=A1=A8=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E6=A0=BC=E5=BC=8F=E5=B9=B6=E4=BC=98=E5=8C=96=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 支持响应为数组、标准包装格式和分页对象多种格式 - 兼容 code 为 0 或 200 的成功响应 - 在请求地址列表时增加日志输出,方便调试响应数据 - 删除 ShopUserAddress 接口定义,避免冗余 - request工具中支持 code 为 200 的请求成功判断 - useAddress钩子中添加加载地址日志和错误日志辅助调试 --- src/api/shop/shopUserAddress/index.ts | 26 +++++++++++++++++++++++--- src/api/shop/shopUserAddress/model.ts | 17 ----------------- src/hooks/useAddress.ts | 3 ++- src/utils/request.ts | 2 +- 4 files changed, 26 insertions(+), 22 deletions(-) delete mode 100644 src/api/shop/shopUserAddress/model.ts diff --git a/src/api/shop/shopUserAddress/index.ts b/src/api/shop/shopUserAddress/index.ts index dde2623..35d7b45 100644 --- a/src/api/shop/shopUserAddress/index.ts +++ b/src/api/shop/shopUserAddress/index.ts @@ -24,10 +24,30 @@ export async function listShopUserAddress(params?: ShopUserAddressParam) { '/shop/shop-user-address', params ); - if (res.code === 0 && res.data) { - return res.data; + console.log('[listShopUserAddress] raw response:', JSON.stringify(res)?.substring(0, 300)) + // 兼容多种响应格式 + // 1. 直接返回数组 + if (Array.isArray(res)) { + return res as ShopUserAddress[]; } - return Promise.reject(new Error(res.message)); + // 2. 标准包装 { code, data, message } + if (res && typeof res === 'object') { + // 兼容 code: 0 和 code: 200 + const isSuccess = res.code === 0 || res.code === 200 + if (isSuccess) { + // data 是数组 + if (Array.isArray(res.data)) { + return res.data; + } + // data 是分页对象 { list, count } + if (res.data && Array.isArray((res.data as any).list)) { + return (res.data as any).list as ShopUserAddress[]; + } + // data 为 null/undefined 但 code 成功,返回空数组 + return []; + } + } + return Promise.reject(new Error((res as any)?.message || '获取地址列表失败')); } /** diff --git a/src/api/shop/shopUserAddress/model.ts b/src/api/shop/shopUserAddress/model.ts deleted file mode 100644 index c6c5433..0000000 --- a/src/api/shop/shopUserAddress/model.ts +++ /dev/null @@ -1,17 +0,0 @@ -/** 收货地址 */ -export interface ShopUserAddress { - id?: number; - userId?: number; - name?: string; // 收货人姓名 - phone?: string; // 收货人电话 - province?: string; // 省份 - city?: string; // 城市 - district?: string; // 区县 - region?: string; // 区域(部分接口用 region) - detail?: string; // 详细地址 - lng?: string; // 经度 - lat?: string; // 纬度 - isDefault?: number; // 是否默认 0-否 1-是 - createTime?: string; - updateTime?: string; -} diff --git a/src/hooks/useAddress.ts b/src/hooks/useAddress.ts index cdac3fc..573a580 100644 --- a/src/hooks/useAddress.ts +++ b/src/hooks/useAddress.ts @@ -29,9 +29,10 @@ export function useAddress(): UseAddressReturn { setLoading(true) try { const list = await listShopUserAddress() + console.log('[useAddress] loaded addresses:', list?.length, JSON.stringify(list)?.substring(0, 200)) setAddresses(Array.isArray(list) ? list : []) } catch (error) { - console.error('Load addresses error:', error) + console.error('[useAddress] Load addresses error:', error) setAddresses([]) } finally { setLoading(false) diff --git a/src/utils/request.ts b/src/utils/request.ts index b0b865e..e9c3eda 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -125,7 +125,7 @@ const responseInterceptor = (response: any, config: RequestConfig): T => { if (typeof data === 'object' && data !== null && 'code' in data) { const apiResponse = data as ApiResponse - if (apiResponse.code === 0) { + if (apiResponse.code === 0 || apiResponse.code === 200) { return (config.returnRaw ? data : apiResponse.data) as T }