init
This commit is contained in:
84
miniprogram/services/comments/commentActions.js
Normal file
84
miniprogram/services/comments/commentActions.js
Normal file
@@ -0,0 +1,84 @@
|
||||
import { config } from '../../config/index';
|
||||
|
||||
/** 点赞/取消点赞评论 */
|
||||
export function toggleCommentLike(commentId) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const token = wx.getStorageSync('token');
|
||||
if (!token) {
|
||||
reject(new Error('请先登录'));
|
||||
return;
|
||||
}
|
||||
|
||||
wx.request({
|
||||
url: `${config.apiBaseUrl}/comments/${commentId}/like`,
|
||||
method: 'POST',
|
||||
header: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
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 replyComment(commentId, content) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const token = wx.getStorageSync('token');
|
||||
if (!token) {
|
||||
reject(new Error('请先登录'));
|
||||
return;
|
||||
}
|
||||
|
||||
wx.request({
|
||||
url: `${config.apiBaseUrl}/comments/${commentId}/reply`,
|
||||
method: 'POST',
|
||||
header: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
data: {
|
||||
content: content
|
||||
},
|
||||
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 fetchCommentDetail(commentId) {
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.request({
|
||||
url: `${config.apiBaseUrl}/comments/${commentId}`,
|
||||
method: 'GET',
|
||||
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);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
179
miniprogram/services/comments/createComment.js
Normal file
179
miniprogram/services/comments/createComment.js
Normal file
@@ -0,0 +1,179 @@
|
||||
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);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
79
miniprogram/services/comments/fetchComments.js
Normal file
79
miniprogram/services/comments/fetchComments.js
Normal file
@@ -0,0 +1,79 @@
|
||||
const { config } = require('../../config/index');
|
||||
|
||||
/** 获取商品评论 */
|
||||
function mockFetchComments(parmas) {
|
||||
const { delay } = require('../_utils/delay');
|
||||
const { getGoodsAllComments } = require('../../model/comments');
|
||||
return delay().then(() => getGoodsAllComments(parmas));
|
||||
}
|
||||
|
||||
/** 获取商品评论 */
|
||||
function fetchComments(params) {
|
||||
if (config.useMock) {
|
||||
return mockFetchComments(params);
|
||||
}
|
||||
|
||||
const { productId, page = 1, pageSize = 10, rating, hasImages } = params;
|
||||
console.log('[fetchComments] 接收到的参数:', params);
|
||||
console.log('[fetchComments] productId:', productId, '类型:', typeof productId);
|
||||
|
||||
// 验证productId参数
|
||||
if (productId === null || productId === undefined || productId === '' || productId === 'undefined' || productId === 'null') {
|
||||
console.error('[fetchComments] 商品ID为空或无效:', productId);
|
||||
return Promise.reject(new Error('商品ID不能为空'));
|
||||
}
|
||||
|
||||
// 确保productId是数字
|
||||
const numericProductId = Number(productId);
|
||||
console.log('[fetchComments] 转换后的数字ID:', numericProductId);
|
||||
|
||||
if (isNaN(numericProductId) || numericProductId <= 0) {
|
||||
console.error('[fetchComments] 商品ID不是有效的正整数:', productId, '->', numericProductId);
|
||||
return Promise.reject(new Error('商品ID必须是有效的正整数'));
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.request({
|
||||
url: `${config.apiBase}/comments/products/${numericProductId}`,
|
||||
method: 'GET',
|
||||
data: {
|
||||
page,
|
||||
page_size: pageSize,
|
||||
rating,
|
||||
has_images: hasImages
|
||||
},
|
||||
success: (res) => {
|
||||
if (res.statusCode === 200 && res.data.code === 200) {
|
||||
// 转换数据格式以匹配前端期望的结构
|
||||
const result = {
|
||||
pageNum: res.data.data.page,
|
||||
pageSize: res.data.data.page_size,
|
||||
totalCount: res.data.data.total.toString(),
|
||||
comments: res.data.data.list.map(comment => ({
|
||||
id: comment.id,
|
||||
product_id: comment.product_id,
|
||||
user_id: comment.user_id,
|
||||
user_name: comment.user_name || '匿名用户',
|
||||
user_avatar: comment.user_avatar || 'https://tdesign.gtimg.com/miniprogram/template/retail/avatar/avatar1.png',
|
||||
rating: comment.rating,
|
||||
content: comment.content,
|
||||
images: comment.images || [],
|
||||
is_anonymous: comment.is_anonymous || false,
|
||||
reply_content: comment.reply_content || '',
|
||||
created_at: comment.created_at,
|
||||
product_spec: comment.product_spec || ''
|
||||
}))
|
||||
};
|
||||
resolve(result);
|
||||
} else {
|
||||
reject(new Error(res.data.message || '获取评论失败'));
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { fetchComments };
|
||||
75
miniprogram/services/comments/fetchCommentsCount.js
Normal file
75
miniprogram/services/comments/fetchCommentsCount.js
Normal file
@@ -0,0 +1,75 @@
|
||||
const { config } = require('../../config/index');
|
||||
|
||||
/** 获取商品评论数 */
|
||||
function mockFetchCommentsCount(ID = 0) {
|
||||
const { delay } = require('../_utils/delay');
|
||||
const { getGoodsCommentsCount } = require('../../model/comments');
|
||||
return delay().then(() => getGoodsCommentsCount(ID));
|
||||
}
|
||||
|
||||
/** 获取商品评论数 */
|
||||
function fetchCommentsCount(productId = 0) {
|
||||
console.log('[fetchCommentsCount] 接收到的productId:', productId, '类型:', typeof productId);
|
||||
|
||||
// 验证productId参数
|
||||
if (productId === null || productId === undefined || productId === '' || productId === 'undefined' || productId === 'null') {
|
||||
console.error('[fetchCommentsCount] 商品ID为空或无效:', productId);
|
||||
return Promise.reject(new Error('商品ID不能为空'));
|
||||
}
|
||||
|
||||
// 确保productId是数字
|
||||
const numericProductId = Number(productId);
|
||||
console.log('[fetchCommentsCount] 转换后的数字ID:', numericProductId);
|
||||
|
||||
if (isNaN(numericProductId) || numericProductId <= 0) {
|
||||
console.error('[fetchCommentsCount] 商品ID不是有效的正整数:', productId, '->', numericProductId);
|
||||
return Promise.reject(new Error('商品ID必须是有效的正整数'));
|
||||
}
|
||||
|
||||
if (config.useMock) {
|
||||
return mockFetchCommentsCount(numericProductId);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.request({
|
||||
url: `${config.apiBase}/comments/products/${numericProductId}/stats`,
|
||||
method: 'GET',
|
||||
success: (res) => {
|
||||
if (res.statusCode === 200 && res.data.code === 200) {
|
||||
// 转换数据格式以匹配前端期望的结构
|
||||
const stats = res.data.data;
|
||||
const result = {
|
||||
// 原始数据保留
|
||||
total_count: stats.total_count || 0,
|
||||
average_rating: stats.average_rating || 0,
|
||||
rating_1_count: stats.rating_1_count || 0,
|
||||
rating_2_count: stats.rating_2_count || 0,
|
||||
rating_3_count: stats.rating_3_count || 0,
|
||||
rating_4_count: stats.rating_4_count || 0,
|
||||
rating_5_count: stats.rating_5_count || 0,
|
||||
good_count: (stats.rating_4_count || 0) + (stats.rating_5_count || 0),
|
||||
medium_count: stats.rating_3_count || 0,
|
||||
bad_count: (stats.rating_1_count || 0) + (stats.rating_2_count || 0),
|
||||
image_count: stats.has_images_count || stats.image_count || 0,
|
||||
|
||||
// 页面期望的数据格式
|
||||
commentCount: String(stats.total_count || 0),
|
||||
goodCount: String((stats.rating_4_count || 0) + (stats.rating_5_count || 0)),
|
||||
middleCount: String(stats.rating_3_count || 0),
|
||||
badCount: String((stats.rating_1_count || 0) + (stats.rating_2_count || 0)),
|
||||
hasImageCount: String(stats.has_images_count || stats.image_count || 0),
|
||||
uidCount: '0', // 用户自己的评论数,需要单独获取
|
||||
};
|
||||
resolve(result);
|
||||
} else {
|
||||
reject(new Error(res.data.message || '获取评论统计失败'));
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { fetchCommentsCount };
|
||||
Reference in New Issue
Block a user