Files
qingcheng-xiaochengxu/pages/course/courseOrderList/courseOrderList.js

202 lines
5.7 KiB
JavaScript
Raw Normal View History

2025-07-20 18:22:59 +08:00
import { baseUrl } from "../../../request";
2025-06-25 15:45:25 +08:00
// pages/course/courseOrderList/courseOrderList.js
Page({
data: {
2025-07-20 18:22:59 +08:00
orderList: [], // 后端返回的订单列表
hasModalShown: false, // 弹框只显示一次
2025-08-08 19:21:04 +08:00
isMaskVisible: false
2025-06-25 15:45:25 +08:00
},
onLoad(options) {
2025-07-20 18:22:59 +08:00
this.fetchOrders();
2025-06-25 15:45:25 +08:00
},
onHide() {
2025-07-20 18:22:59 +08:00
clearInterval(this._timer);
2025-06-25 15:45:25 +08:00
},
onUnload() {
2025-07-20 18:22:59 +08:00
clearInterval(this._timer);
},
onShow() {
this.fetchOrders()
2025-06-25 15:45:25 +08:00
},
2025-07-20 18:22:59 +08:00
// 拉取后端接口
fetchOrders() {
wx.request({
url: baseUrl + '/courseOrder/query/list', // 替换为真实接口
method: 'POST',
header: {
Authorization: wx.getStorageSync('token')
},
success: res => {
console.log('课程订单列表---->',res.data.data);
if (res.data.code === 1) {
const now = Date.now();
let list = res.data.data.map(item => {
// 计算从 createTime 到 now 剩余秒数
const createMs = new Date(item.createTime.replace(/-/g,'/')).getTime();
2025-08-08 19:21:04 +08:00
let diff = Math.floor((createMs + 15*60*1000 - now) / 1000);
2025-07-20 18:22:59 +08:00
// 只有“待支付”才需要倒计时、过期置“交易取消”
if (item.orderStatus === '待支付') {
if (diff <= 0) {
item.orderStatus = '交易取消';
diff = 0;
// 首次检测到过期就弹框
if (!this.data.hasModalShown) {
wx.showModal({
title: '提示',
content: '订单超时未支付,已取消',
showCancel: false
});
this.setData({ hasModalShown: true });
}
}
item.countDown = diff;
const m = Math.floor(diff / 60);
const s = diff % 60;
item.countDownStr = `${m}${s < 10 ? '0'+s : s}`;
} else {
item.countDown = 0;
item.countDownStr = '';
}
return item;
});
this.setData({ orderList: list }, () => {
// 初始处理完后启动全局定时器
this.startTimer();
});
} else {
wx.showToast({ title: res.data.message || '获取失败', icon: 'none' });
}
},
fail: () => {
wx.showToast({ title: '网络错误', icon: 'none' });
2025-06-25 15:45:25 +08:00
}
2025-07-20 18:22:59 +08:00
});
2025-06-25 15:45:25 +08:00
},
2025-07-20 18:22:59 +08:00
// 每秒更新所有待支付订单的倒计时
startTimer() {
2025-08-08 19:21:04 +08:00
// ← 新增:如果已有定时器,先清掉它
if (this._timer) {
clearInterval(this._timer);
}
2025-07-20 18:22:59 +08:00
this._timer = setInterval(() => {
const updated = this.data.orderList.map(item => {
if (item.orderStatus === '待支付' && item.countDown > 0) {
const cd = item.countDown - 1;
item.countDown = cd;
const m = Math.floor(cd / 60);
const s = cd % 60;
item.countDownStr = `${m}${s < 10 ? '0'+s : s}`;
if (cd <= 0) {
item.orderStatus = '交易取消';
item.countDownStr = '';
if (!this.data.hasModalShown) {
wx.showModal({
title: '提示',
content: '订单超时未支付,已取消',
showCancel: false
});
this.setData({ hasModalShown: true });
}
}
}
return item;
});
this.setData({ orderList: updated });
}, 1000);
2025-06-25 15:45:25 +08:00
},
2025-08-08 19:21:04 +08:00
showIsPayModal(e) {
const orderId = e.currentTarget.dataset.orderId;
wx.showModal({
title: '下单成功',
content: '您确定要支付吗?',
cancelText: '取消',
confirmText: '确定',
success: (res) => {
if (res.confirm) {
this.payOrder(orderId);
} else if (res.cancel) {
this.setData({ isMaskVisible: false });
}
},
fail: () => {
wx.hideLoading();
wx.showToast({
title: '网络错误,下单失败',
icon: 'none'
});
}
});
},
payOrder(orderId) {
// 同样先显示遮罩
this.setData({ isMaskVisible: true });
wx.showLoading({ title: '支付中...'});
wx.request({
url: baseUrl + '/courseOrder/payment',
method: 'POST',
header: { Authorization: wx.getStorageSync('token') },
data: { id: orderId},
success: res => {
wx.hideLoading();
if (res.data.code === 1) {
// 支付成功,跳转详情页
wx.redirectTo({
url: `/pages/course/orderDetail/orderDetail?id=${orderId}`,
success: res => {
// 先把遮罩关掉
this.setData({ isMaskVisible: false });
}
});
} else {
this.setData({ isMaskVisible: false });
wx.showToast({ title: res.data.message || '支付失败', icon: 'none' });
}
},
fail: () => {
wx.hideLoading();
this.setData({ isMaskVisible: false });
wx.showToast({ title: '网络错误,支付失败', icon: 'none' });
}
});
},
2025-07-20 18:22:59 +08:00
// 跳转订单详情
gotoOrderDetail(e) {
const orderId = e.currentTarget.dataset.id;
wx.navigateTo({
url: `/pages/course/orderDetail/orderDetail?id=${orderId}`,
})
},
2025-06-25 15:45:25 +08:00
2025-07-20 18:22:59 +08:00
// 取消订单
2025-08-06 13:16:08 +08:00
cancelOrder(e) {
// console.log(e);
const id = e.currentTarget.dataset.id;
2025-07-20 18:22:59 +08:00
wx.showModal({
title: '取消订单',
content: '是否要取消订单?',
success: res => {
if (res.confirm) {
wx.request({
url: baseUrl + "/courseOrder/cancel",
method: 'POST',
2025-08-06 13:16:08 +08:00
data: { courseId: id },
2025-07-20 18:22:59 +08:00
header: { Authorization: wx.getStorageSync('token') },
2025-08-06 13:16:08 +08:00
success: () => this.fetchOrders()
2025-07-20 18:22:59 +08:00
});
}
}
});
2025-08-08 19:21:04 +08:00
}
2025-06-25 15:45:25 +08:00
});