125 lines
2.9 KiB
JavaScript
125 lines
2.9 KiB
JavaScript
import Toast from 'tdesign-miniprogram/toast/index';
|
|
import config from '../../../config/index';
|
|
|
|
Page({
|
|
data: {
|
|
orderNo: '',
|
|
orderItems: [],
|
|
loading: true,
|
|
},
|
|
|
|
onLoad(options) {
|
|
const { orderNo } = options;
|
|
if (orderNo) {
|
|
this.setData({ orderNo });
|
|
this.fetchOrderItems();
|
|
} else {
|
|
Toast({
|
|
context: this,
|
|
selector: '#t-toast',
|
|
message: '订单号不能为空',
|
|
icon: 'error',
|
|
});
|
|
setTimeout(() => {
|
|
wx.navigateBack();
|
|
}, 1500);
|
|
}
|
|
},
|
|
|
|
// 获取订单商品列表
|
|
async fetchOrderItems() {
|
|
try {
|
|
wx.showLoading({
|
|
title: '加载中...',
|
|
mask: true
|
|
});
|
|
|
|
const token = wx.getStorageSync('token');
|
|
if (!token) {
|
|
Toast({
|
|
context: this,
|
|
selector: '#t-toast',
|
|
message: '请先登录',
|
|
icon: 'error',
|
|
});
|
|
setTimeout(() => {
|
|
wx.navigateTo({
|
|
url: '/pages/login/index'
|
|
});
|
|
}, 1500);
|
|
return;
|
|
}
|
|
|
|
const res = await new Promise((resolve, reject) => {
|
|
wx.request({
|
|
url: `${config.apiBase}/api/v1/orders/${this.data.orderNo}`,
|
|
method: 'GET',
|
|
header: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${token}`
|
|
},
|
|
success: resolve,
|
|
fail: reject
|
|
});
|
|
});
|
|
|
|
wx.hideLoading();
|
|
|
|
if (res.statusCode === 200 && res.data.code === 200) {
|
|
const order = res.data.data;
|
|
const orderItems = order.order_items || [];
|
|
|
|
// 过滤出可以评论的商品(已确认收货且未评论)
|
|
const commentableItems = orderItems.filter(item =>
|
|
order.status === 'completed' && !item.is_commented
|
|
);
|
|
|
|
this.setData({
|
|
orderItems: commentableItems,
|
|
loading: false
|
|
});
|
|
|
|
if (commentableItems.length === 0) {
|
|
Toast({
|
|
context: this,
|
|
selector: '#t-toast',
|
|
message: '暂无可评论的商品',
|
|
icon: 'info',
|
|
});
|
|
}
|
|
} else {
|
|
throw new Error(res.data.message || '获取订单信息失败');
|
|
}
|
|
} catch (error) {
|
|
wx.hideLoading();
|
|
console.error('获取订单商品失败:', error);
|
|
Toast({
|
|
context: this,
|
|
selector: '#t-toast',
|
|
message: error.message || '网络错误,请重试',
|
|
icon: 'error',
|
|
});
|
|
this.setData({ loading: false });
|
|
}
|
|
},
|
|
|
|
// 点击评论商品
|
|
onCommentItem(e) {
|
|
const { item } = e.currentTarget.dataset;
|
|
wx.navigateTo({
|
|
url: `/pages/order/comment/index?orderNo=${this.data.orderNo}&orderItemId=${item.id}`,
|
|
});
|
|
},
|
|
|
|
// 返回上一页
|
|
onBack() {
|
|
wx.navigateBack();
|
|
},
|
|
|
|
// 下拉刷新
|
|
onPullDownRefresh() {
|
|
this.fetchOrderItems().finally(() => {
|
|
wx.stopPullDownRefresh();
|
|
});
|
|
}
|
|
}); |