179 lines
5.5 KiB
JavaScript
179 lines
5.5 KiB
JavaScript
import { config } from '../../config/index';
|
||
|
||
/** 创建商品评论 */
|
||
export function createComment(commentData) {
|
||
return new Promise((resolve, reject) => {
|
||
// 获取用户token
|
||
const token = wx.getStorageSync('token');
|
||
if (!token) {
|
||
reject(new Error('请先登录'));
|
||
return;
|
||
}
|
||
|
||
// 如果有orderItemId,直接使用原有逻辑
|
||
if (commentData.orderItemId) {
|
||
wx.request({
|
||
url: `${config.apiBaseUrl}/comments`,
|
||
method: 'POST',
|
||
header: {
|
||
'Authorization': `Bearer ${token}`,
|
||
'Content-Type': 'application/json'
|
||
},
|
||
data: {
|
||
order_item_id: commentData.orderItemId,
|
||
rating: commentData.rating,
|
||
content: commentData.content,
|
||
images: commentData.images || [],
|
||
is_anonymous: commentData.isAnonymous || false
|
||
},
|
||
success: (res) => {
|
||
if (res.statusCode === 200 && res.data.code === 200) {
|
||
resolve(res.data.data);
|
||
} else {
|
||
reject(new Error(res.data.message || '评论提交失败'));
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
reject(err);
|
||
}
|
||
});
|
||
return;
|
||
}
|
||
|
||
// 如果有orderNo和productId,先查找对应的订单项
|
||
if (commentData.orderNo && commentData.productId) {
|
||
// 先获取订单详情,找到对应的订单项
|
||
wx.request({
|
||
url: `${config.apiBaseUrl}/orders/${commentData.orderNo}`,
|
||
method: 'GET',
|
||
header: {
|
||
'Authorization': `Bearer ${token}`
|
||
},
|
||
success: (orderRes) => {
|
||
if (orderRes.statusCode === 200 && orderRes.data.code === 200) {
|
||
const order = orderRes.data.data;
|
||
// 调试:打印关键信息
|
||
console.log('[createComment] 查找的产品ID:', commentData.productId, '类型:', typeof commentData.productId);
|
||
console.log('[createComment] 订单项数量:', order.orderItemVOs?.length);
|
||
|
||
// 查找匹配的订单项
|
||
const orderItem = order.orderItemVOs?.find(item => {
|
||
// 确保数据类型一致进行比较
|
||
const itemSpuId = String(item.spuId);
|
||
const searchProductId = String(commentData.productId);
|
||
console.log('[createComment] 比较:', itemSpuId, '===', searchProductId, '结果:', itemSpuId === searchProductId);
|
||
|
||
return itemSpuId === searchProductId;
|
||
});
|
||
|
||
console.log('[createComment] 找到的订单项:', orderItem ? `ID: ${orderItem.id}, 商品: ${orderItem.goodsName}` : '未找到');
|
||
|
||
if (!orderItem) {
|
||
reject(new Error('未找到对应的订单项'));
|
||
return;
|
||
}
|
||
|
||
// 使用找到的订单项ID创建评论
|
||
wx.request({
|
||
url: `${config.apiBaseUrl}/comments`,
|
||
method: 'POST',
|
||
header: {
|
||
'Authorization': `Bearer ${token}`,
|
||
'Content-Type': 'application/json'
|
||
},
|
||
data: {
|
||
order_item_id: parseInt(orderItem.id),
|
||
rating: commentData.rating,
|
||
content: commentData.content,
|
||
images: commentData.images || [],
|
||
is_anonymous: commentData.isAnonymous || false
|
||
},
|
||
success: (res) => {
|
||
if (res.statusCode === 200 && res.data.code === 200) {
|
||
resolve(res.data.data);
|
||
} else {
|
||
reject(new Error(res.data.message || '评论提交失败'));
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
reject(err);
|
||
}
|
||
});
|
||
} else {
|
||
reject(new Error(orderRes.data.message || '获取订单信息失败'));
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
reject(err);
|
||
}
|
||
});
|
||
return;
|
||
}
|
||
|
||
reject(new Error('缺少必要的参数'));
|
||
});
|
||
}
|
||
|
||
/** 获取用户评论列表 */
|
||
export function fetchUserComments(params = {}) {
|
||
return new Promise((resolve, reject) => {
|
||
const token = wx.getStorageSync('token');
|
||
if (!token) {
|
||
reject(new Error('请先登录'));
|
||
return;
|
||
}
|
||
|
||
const { page = 1, pageSize = 10 } = params;
|
||
|
||
wx.request({
|
||
url: `${config.apiBaseUrl}/comments/user`,
|
||
method: 'GET',
|
||
header: {
|
||
'Authorization': `Bearer ${token}`
|
||
},
|
||
data: {
|
||
page,
|
||
page_size: pageSize
|
||
},
|
||
success: (res) => {
|
||
if (res.statusCode === 200 && res.data.code === 200) {
|
||
resolve(res.data.data);
|
||
} else {
|
||
reject(new Error(res.data.message || '获取评论列表失败'));
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
reject(err);
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
/** 获取未评论的订单项 */
|
||
export function fetchUncommentedOrderItems() {
|
||
return new Promise((resolve, reject) => {
|
||
const token = wx.getStorageSync('token');
|
||
if (!token) {
|
||
reject(new Error('请先登录'));
|
||
return;
|
||
}
|
||
|
||
wx.request({
|
||
url: `${config.apiBaseUrl}/user/uncommented-order-items`,
|
||
method: 'GET',
|
||
header: {
|
||
'Authorization': `Bearer ${token}`
|
||
},
|
||
success: (res) => {
|
||
if (res.statusCode === 200 && res.data.code === 200) {
|
||
resolve(res.data.data);
|
||
} else {
|
||
reject(new Error(res.data.message || '获取待评论订单失败'));
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
reject(err);
|
||
}
|
||
});
|
||
});
|
||
} |