239 lines
5.5 KiB
JavaScript
239 lines
5.5 KiB
JavaScript
import { createComment } from '../../../services/comments/createComment';
|
|
import Toast from 'tdesign-miniprogram/toast/index';
|
|
|
|
Page({
|
|
data: {
|
|
orderItem: null,
|
|
rating: 5,
|
|
content: '',
|
|
images: [],
|
|
isAnonymous: false,
|
|
submitting: false,
|
|
maxImages: 9,
|
|
maxContentLength: 500
|
|
},
|
|
|
|
onLoad(options) {
|
|
console.log('评论页面加载', options);
|
|
|
|
// 从页面参数获取订单项信息
|
|
if (options.orderItemData) {
|
|
try {
|
|
const orderItem = JSON.parse(decodeURIComponent(options.orderItemData));
|
|
this.setData({ orderItem });
|
|
console.log('订单项信息', orderItem);
|
|
} catch (e) {
|
|
console.error('解析订单项数据失败', e);
|
|
Toast({
|
|
context: this,
|
|
selector: '#t-toast',
|
|
message: '参数错误',
|
|
icon: 'error-circle',
|
|
});
|
|
setTimeout(() => {
|
|
wx.navigateBack();
|
|
}, 1500);
|
|
}
|
|
} else {
|
|
Toast({
|
|
context: this,
|
|
selector: '#t-toast',
|
|
message: '缺少订单信息',
|
|
icon: 'error-circle',
|
|
});
|
|
setTimeout(() => {
|
|
wx.navigateBack();
|
|
}, 1500);
|
|
}
|
|
},
|
|
|
|
// 评分变化
|
|
onRatingChange(e) {
|
|
this.setData({
|
|
rating: e.detail.value
|
|
});
|
|
},
|
|
|
|
// 评论内容输入
|
|
onContentInput(e) {
|
|
this.setData({
|
|
content: e.detail.value
|
|
});
|
|
},
|
|
|
|
// 匿名评论切换
|
|
onAnonymousChange(e) {
|
|
this.setData({
|
|
isAnonymous: e.detail.value
|
|
});
|
|
},
|
|
|
|
// 选择图片
|
|
onChooseImage() {
|
|
const { images, maxImages } = this.data;
|
|
const remainCount = maxImages - images.length;
|
|
|
|
if (remainCount <= 0) {
|
|
Toast({
|
|
context: this,
|
|
selector: '#t-toast',
|
|
message: `最多只能上传${maxImages}张图片`,
|
|
icon: 'error-circle',
|
|
});
|
|
return;
|
|
}
|
|
|
|
wx.chooseMedia({
|
|
count: remainCount,
|
|
mediaType: ['image'],
|
|
sourceType: ['album', 'camera'],
|
|
success: (res) => {
|
|
const newImages = res.tempFiles.map(file => file.tempFilePath);
|
|
this.setData({
|
|
images: [...images, ...newImages]
|
|
});
|
|
},
|
|
fail: (err) => {
|
|
console.error('选择图片失败', err);
|
|
Toast({
|
|
context: this,
|
|
selector: '#t-toast',
|
|
message: '选择图片失败',
|
|
icon: 'error-circle',
|
|
});
|
|
}
|
|
});
|
|
},
|
|
|
|
// 删除图片
|
|
onDeleteImage(e) {
|
|
const { index } = e.currentTarget.dataset;
|
|
const { images } = this.data;
|
|
images.splice(index, 1);
|
|
this.setData({ images });
|
|
},
|
|
|
|
// 预览图片
|
|
onPreviewImage(e) {
|
|
const { index } = e.currentTarget.dataset;
|
|
const { images } = this.data;
|
|
|
|
wx.previewImage({
|
|
current: images[index],
|
|
urls: images
|
|
});
|
|
},
|
|
|
|
// 上传图片到服务器
|
|
async uploadImages(images) {
|
|
const uploadPromises = images.map(imagePath => {
|
|
return new Promise((resolve, reject) => {
|
|
wx.uploadFile({
|
|
url: `${getApp().globalData.config.apiBaseUrl}/upload/image`,
|
|
filePath: imagePath,
|
|
name: 'file',
|
|
header: {
|
|
'Authorization': `Bearer ${wx.getStorageSync('token')}`
|
|
},
|
|
success: (res) => {
|
|
try {
|
|
const data = JSON.parse(res.data);
|
|
if (data.code === 200) {
|
|
resolve(data.data.url);
|
|
} else {
|
|
reject(new Error(data.message || '上传失败'));
|
|
}
|
|
} catch (e) {
|
|
reject(new Error('解析上传结果失败'));
|
|
}
|
|
},
|
|
fail: reject
|
|
});
|
|
});
|
|
});
|
|
|
|
return Promise.all(uploadPromises);
|
|
},
|
|
|
|
// 提交评论
|
|
async onSubmit() {
|
|
const { orderItem, rating, content, images, isAnonymous, submitting } = this.data;
|
|
|
|
if (submitting) return;
|
|
|
|
if (!content.trim()) {
|
|
Toast({
|
|
context: this,
|
|
selector: '#t-toast',
|
|
message: '请输入评论内容',
|
|
icon: 'error-circle',
|
|
});
|
|
return;
|
|
}
|
|
|
|
this.setData({ submitting: true });
|
|
|
|
try {
|
|
// 上传图片
|
|
let uploadedImages = [];
|
|
if (images.length > 0) {
|
|
Toast({
|
|
context: this,
|
|
selector: '#t-toast',
|
|
message: '正在上传图片...',
|
|
icon: 'loading',
|
|
});
|
|
uploadedImages = await this.uploadImages(images);
|
|
}
|
|
|
|
// 提交评论
|
|
const commentData = {
|
|
orderItemId: orderItem.id,
|
|
productId: orderItem.product_id,
|
|
rating,
|
|
content: content.trim(),
|
|
images: uploadedImages,
|
|
isAnonymous
|
|
};
|
|
|
|
await createComment(commentData);
|
|
|
|
Toast({
|
|
context: this,
|
|
selector: '#t-toast',
|
|
message: '评论提交成功',
|
|
icon: 'check-circle',
|
|
});
|
|
|
|
// 设置上一页刷新标志
|
|
const pages = getCurrentPages();
|
|
const prevPage = pages[pages.length - 2]; // 上一页(订单详情页)
|
|
if (prevPage) {
|
|
prevPage.setData({ backRefresh: true });
|
|
}
|
|
|
|
// 延迟返回上一页
|
|
setTimeout(() => {
|
|
wx.navigateBack({
|
|
delta: 1
|
|
});
|
|
}, 1500);
|
|
|
|
} catch (error) {
|
|
console.error('提交评论失败', error);
|
|
Toast({
|
|
context: this,
|
|
selector: '#t-toast',
|
|
message: error.message || '提交失败,请重试',
|
|
icon: 'error-circle',
|
|
});
|
|
} finally {
|
|
this.setData({ submitting: false });
|
|
}
|
|
},
|
|
|
|
// 返回上一页
|
|
onBack() {
|
|
wx.navigateBack();
|
|
}
|
|
}); |