141 lines
4.0 KiB
JavaScript
141 lines
4.0 KiB
JavaScript
import { config } from '../../config/index';
|
|
|
|
/**
|
|
* 检查商品是否已收藏
|
|
* @param {number} productId 商品ID
|
|
* @returns {Promise<boolean>} 是否已收藏
|
|
*/
|
|
export function checkIsFavorite(productId) {
|
|
return new Promise((resolve, reject) => {
|
|
// 获取用户token
|
|
const token = wx.getStorageSync('token') || wx.getStorageSync('jwt_token');
|
|
if (!token) {
|
|
console.log('用户未登录,默认未收藏');
|
|
resolve(false);
|
|
return;
|
|
}
|
|
|
|
wx.request({
|
|
url: `${config.apiBase}/products/${productId}/favorite/status`,
|
|
method: 'GET',
|
|
header: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${token}`
|
|
},
|
|
success: (res) => {
|
|
if (res.statusCode === 200 && (res.data?.code === 200 || res.data?.success === true || res.data?.code === 'Success')) {
|
|
try {
|
|
const payload = res.data || {};
|
|
const data = payload.data || payload;
|
|
const isFavorite = (
|
|
data.is_favorite ??
|
|
data.isFavorite ??
|
|
data.favorite ??
|
|
data.favorited ??
|
|
false
|
|
);
|
|
resolve(Boolean(isFavorite));
|
|
} catch (e) {
|
|
console.error('解析收藏状态失败:', e, res);
|
|
resolve(false);
|
|
}
|
|
} else {
|
|
console.error('检查收藏状态失败:', res);
|
|
resolve(false);
|
|
}
|
|
},
|
|
fail: (error) => {
|
|
console.error('检查收藏状态请求失败:', error);
|
|
resolve(false);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 添加商品到收藏
|
|
* @param {number} productId 商品ID
|
|
* @returns {Promise<boolean>} 操作是否成功
|
|
*/
|
|
export function addToFavorite(productId) {
|
|
return new Promise((resolve, reject) => {
|
|
// 获取用户token
|
|
const token = wx.getStorageSync('token') || wx.getStorageSync('jwt_token');
|
|
if (!token) {
|
|
reject(new Error('用户未登录'));
|
|
return;
|
|
}
|
|
|
|
wx.request({
|
|
url: `${config.apiBase}/products/${productId}/favorite`,
|
|
method: 'POST',
|
|
header: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${token}`
|
|
},
|
|
success: (res) => {
|
|
if ((res.statusCode === 200 || res.statusCode === 201) && (res.data?.code === 200 || res.data?.success === true)) {
|
|
resolve(true);
|
|
} else {
|
|
console.error('添加收藏失败:', res);
|
|
reject(new Error(res.data.message || '收藏失败'));
|
|
}
|
|
},
|
|
fail: (error) => {
|
|
console.error('添加收藏请求失败:', error);
|
|
reject(error);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 取消收藏商品
|
|
* @param {number} productId 商品ID
|
|
* @returns {Promise<boolean>} 操作是否成功
|
|
*/
|
|
export function removeFromFavorite(productId) {
|
|
return new Promise((resolve, reject) => {
|
|
// 获取用户token
|
|
const token = wx.getStorageSync('token') || wx.getStorageSync('jwt_token');
|
|
if (!token) {
|
|
reject(new Error('用户未登录'));
|
|
return;
|
|
}
|
|
|
|
wx.request({
|
|
url: `${config.apiBase}/products/${productId}/favorite`,
|
|
method: 'DELETE',
|
|
header: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${token}`
|
|
},
|
|
success: (res) => {
|
|
if ((res.statusCode === 200 || res.statusCode === 204) && (res.data?.code === 200 || res.data?.success === true)) {
|
|
resolve(true);
|
|
} else {
|
|
console.error('取消收藏失败:', res);
|
|
reject(new Error(res.data.message || '取消收藏失败'));
|
|
}
|
|
},
|
|
fail: (error) => {
|
|
console.error('取消收藏请求失败:', error);
|
|
reject(error);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 切换收藏状态
|
|
* @param {number} productId 商品ID
|
|
* @param {boolean} currentStatus 当前收藏状态
|
|
* @returns {Promise<boolean>} 新的收藏状态
|
|
*/
|
|
export function toggleFavorite(productId, currentStatus) {
|
|
if (currentStatus) {
|
|
return removeFromFavorite(productId).then(() => false);
|
|
} else {
|
|
return addToFavorite(productId).then(() => true);
|
|
}
|
|
} |