refactor(api): 优化商品收藏接口调用并统一数据解包
- 新增 unwrap 函数统一处理响应数据解包 - 将收藏相关接口改为 async 函数,显式等待请求结果 - 统一调用 unwrap 提取响应中的 data 字段,保障数据一致性 - 确保收藏状态接口返回纯布尔值,避免类型不确定 - 收藏列表接口添加空数组和空对象默认值处理,防止异常 refactor(shop): 简化购物车和客服页面跳转逻辑 - 购物车跳转采用 navigateTo,确保非 tabBar 页面正常打开 - 客服跳转直接进入客服页面,去除复杂客服会话功能调用逻辑
This commit is contained in:
@@ -1,27 +1,44 @@
|
||||
import request from '@/utils/request'
|
||||
import type { ShopGoodsFavorite, ShopGoodsFavoriteParam } from './model'
|
||||
|
||||
/**
|
||||
* 解包 API 响应
|
||||
* request 工具默认 returnRaw=true,返回完整 {code, message, data} 包装
|
||||
* 此函数提取内层 data 字段,兼容 data 为 null/undefined 的情况
|
||||
*/
|
||||
function unwrap<T>(res: any): T {
|
||||
if (res && typeof res === 'object' && 'data' in res) {
|
||||
return res.data as T
|
||||
}
|
||||
return res as T
|
||||
}
|
||||
|
||||
// 添加收藏
|
||||
export function addShopGoodsFavorite(data: { goodsId: number }) {
|
||||
return request.post<boolean>('/shop/goods/favorite/add', data)
|
||||
export async function addShopGoodsFavorite(data: { goodsId: number }) {
|
||||
const res = await request.post('/shop/goods/favorite/add', data)
|
||||
return unwrap<boolean>(res)
|
||||
}
|
||||
|
||||
// 取消收藏
|
||||
export function removeShopGoodsFavorite(data: { goodsId: number }) {
|
||||
return request.post<boolean>('/shop/goods/favorite/remove', data)
|
||||
export async function removeShopGoodsFavorite(data: { goodsId: number }) {
|
||||
const res = await request.post('/shop/goods/favorite/remove', data)
|
||||
return unwrap<boolean>(res)
|
||||
}
|
||||
|
||||
// 查询收藏状态
|
||||
export function getShopGoodsFavoriteStatus(params: { goodsId: number }) {
|
||||
return request.get<boolean>('/shop/goods/favorite/status', params)
|
||||
// 查询收藏状态(返回 boolean:true=已收藏, false=未收藏)
|
||||
export async function getShopGoodsFavoriteStatus(params: { goodsId: number }) {
|
||||
const res = await request.get('/shop/goods/favorite/status', params)
|
||||
return !!unwrap<boolean>(res) // 确保返回纯布尔值
|
||||
}
|
||||
|
||||
// 收藏列表
|
||||
export function listShopGoodsFavorite(params: ShopGoodsFavoriteParam) {
|
||||
return request.get<ShopGoodsFavorite[]>('/shop/goods/favorite/list', params)
|
||||
export async function listShopGoodsFavorite(params: ShopGoodsFavoriteParam) {
|
||||
const res = await request.get('/shop/goods/favorite/list', params)
|
||||
return unwrap<ShopGoodsFavorite[]>(res) || []
|
||||
}
|
||||
|
||||
// 收藏列表(分页)
|
||||
export function pageShopGoodsFavorite(params: ShopGoodsFavoriteParam) {
|
||||
return request.get<{ list: ShopGoodsFavorite[]; total: number }>('/shop/goods/favorite/page', params)
|
||||
export async function pageShopGoodsFavorite(params: ShopGoodsFavoriteParam) {
|
||||
const res = await request.get('/shop/goods/favorite/page', params)
|
||||
return unwrap<{ list: ShopGoodsFavorite[]; total: number }>(res) || { list: [], total: 0 }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user