Initial commit
This commit is contained in:
222
miniprogram/pages/goods/comments/create/index.js
Normal file
222
miniprogram/pages/goods/comments/create/index.js
Normal file
@@ -0,0 +1,222 @@
|
||||
import { createComment } from '../../../../services/comments/createComment';
|
||||
import Toast from 'tdesign-miniprogram/toast/index';
|
||||
|
||||
Page({
|
||||
data: {
|
||||
serviceRateValue: 5,
|
||||
goodRateValue: 5,
|
||||
conveyRateValue: 5,
|
||||
isAnonymous: false,
|
||||
uploadFiles: [],
|
||||
images: [],
|
||||
gridConfig: {
|
||||
width: 218,
|
||||
height: 218,
|
||||
column: 3,
|
||||
},
|
||||
isAllowedSubmit: false,
|
||||
submitting: false,
|
||||
imgUrl: '',
|
||||
title: '',
|
||||
goodsDetail: '',
|
||||
orderNo: '',
|
||||
goodsId: '',
|
||||
maxImages: 9,
|
||||
imageProps: {
|
||||
mode: 'aspectFit',
|
||||
},
|
||||
},
|
||||
|
||||
onLoad(options) {
|
||||
console.log('评价页面加载参数:', options);
|
||||
this.setData({
|
||||
imgUrl: options.imgUrl ? decodeURIComponent(options.imgUrl) : '',
|
||||
title: options.title ? decodeURIComponent(options.title) : '',
|
||||
goodsDetail: options.specs ? decodeURIComponent(options.specs) : '',
|
||||
orderNo: options.orderNo || '',
|
||||
goodsId: options.goodsId || '',
|
||||
});
|
||||
this.updateButtonStatus();
|
||||
},
|
||||
|
||||
onRateChange(e) {
|
||||
const { value } = e?.detail;
|
||||
const item = e?.currentTarget?.dataset?.item;
|
||||
this.setData({ [item]: value }, () => {
|
||||
this.updateButtonStatus();
|
||||
});
|
||||
},
|
||||
|
||||
onAnonymousChange(e) {
|
||||
const status = !!e?.detail?.checked;
|
||||
this.setData({ isAnonymous: status });
|
||||
},
|
||||
|
||||
|
||||
|
||||
handleSuccess(e) {
|
||||
// 兼容原有的上传组件
|
||||
const { files } = e.detail;
|
||||
const images = files.map(file => file.url);
|
||||
this.setData({
|
||||
uploadFiles: files,
|
||||
images
|
||||
});
|
||||
},
|
||||
|
||||
handleRemove(e) {
|
||||
const { index } = e.detail;
|
||||
const { uploadFiles, images } = this.data;
|
||||
uploadFiles.splice(index, 1);
|
||||
images.splice(index, 1);
|
||||
this.setData({
|
||||
uploadFiles,
|
||||
images
|
||||
});
|
||||
},
|
||||
|
||||
// 上传图片到服务器
|
||||
async uploadImages(images) {
|
||||
if (!images || images.length === 0) return [];
|
||||
|
||||
const uploadPromises = images.map(imagePath => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const uploadUrl = `${getApp().globalData.config.apiBaseUrl}/upload/image`;
|
||||
const token = wx.getStorageSync('token');
|
||||
console.log('上传URL:', uploadUrl);
|
||||
console.log('Token:', token);
|
||||
|
||||
wx.uploadFile({
|
||||
url: uploadUrl,
|
||||
filePath: imagePath,
|
||||
name: 'file',
|
||||
header: {
|
||||
'Authorization': `Bearer ${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);
|
||||
},
|
||||
|
||||
onTextAreaChange(e) {
|
||||
const value = e?.detail?.value;
|
||||
this.textAreaValue = value;
|
||||
this.updateButtonStatus();
|
||||
},
|
||||
|
||||
updateButtonStatus() {
|
||||
const { serviceRateValue, goodRateValue, conveyRateValue, submitting } = this.data;
|
||||
const { textAreaValue } = this;
|
||||
const temp = serviceRateValue && goodRateValue && conveyRateValue && textAreaValue && !submitting;
|
||||
this.setData({ isAllowedSubmit: temp });
|
||||
},
|
||||
|
||||
async onSubmitBtnClick() {
|
||||
const {
|
||||
isAllowedSubmit,
|
||||
submitting,
|
||||
serviceRateValue,
|
||||
goodRateValue,
|
||||
conveyRateValue,
|
||||
isAnonymous,
|
||||
images,
|
||||
orderNo,
|
||||
goodsId
|
||||
} = this.data;
|
||||
|
||||
if (!isAllowedSubmit || submitting) return;
|
||||
|
||||
const content = this.textAreaValue;
|
||||
if (!content || !content.trim()) {
|
||||
Toast({
|
||||
context: this,
|
||||
selector: '#t-toast',
|
||||
message: '请输入评价内容',
|
||||
icon: 'error-circle',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
this.setData({ submitting: true });
|
||||
this.updateButtonStatus();
|
||||
|
||||
try {
|
||||
// 上传图片
|
||||
let uploadedImages = [];
|
||||
if (images.length > 0) {
|
||||
Toast({
|
||||
context: this,
|
||||
selector: '#t-toast',
|
||||
message: '正在上传图片...',
|
||||
icon: 'loading',
|
||||
});
|
||||
uploadedImages = await this.uploadImages(images);
|
||||
}
|
||||
|
||||
// 计算综合评分(取三个评分的平均值)
|
||||
const averageRating = Math.round((serviceRateValue + goodRateValue + conveyRateValue) / 3);
|
||||
|
||||
// 提交评论
|
||||
const commentData = {
|
||||
orderNo: orderNo,
|
||||
productId: goodsId,
|
||||
rating: averageRating,
|
||||
content: content.trim(),
|
||||
images: uploadedImages,
|
||||
isAnonymous: isAnonymous
|
||||
};
|
||||
|
||||
console.log('提交评论数据:', commentData);
|
||||
|
||||
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 });
|
||||
this.updateButtonStatus();
|
||||
}
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user