fix(shopUserAddress): 兼容多种地址列表响应格式并优化错误处理

- 支持响应为数组、标准包装格式和分页对象多种格式
- 兼容 code 为 0 或 200 的成功响应
- 在请求地址列表时增加日志输出,方便调试响应数据
- 删除 ShopUserAddress 接口定义,避免冗余
- request工具中支持 code 为 200 的请求成功判断
- useAddress钩子中添加加载地址日志和错误日志辅助调试
This commit is contained in:
2026-06-25 00:59:49 +08:00
parent b36205b066
commit 1d30897ab6
4 changed files with 26 additions and 22 deletions

View File

@@ -24,10 +24,30 @@ export async function listShopUserAddress(params?: ShopUserAddressParam) {
'/shop/shop-user-address',
params
);
if (res.code === 0 && res.data) {
console.log('[listShopUserAddress] raw response:', JSON.stringify(res)?.substring(0, 300))
// 兼容多种响应格式
// 1. 直接返回数组
if (Array.isArray(res)) {
return res as ShopUserAddress[];
}
// 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;
}
return Promise.reject(new Error(res.message));
// 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 || '获取地址列表失败'));
}
/**

View File

@@ -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;
}

View File

@@ -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)

View File

@@ -125,7 +125,7 @@ const responseInterceptor = <T>(response: any, config: RequestConfig): T => {
if (typeof data === 'object' && data !== null && 'code' in data) {
const apiResponse = data as ApiResponse<T>
if (apiResponse.code === 0) {
if (apiResponse.code === 0 || apiResponse.code === 200) {
return (config.returnRaw ? data : apiResponse.data) as T
}