Files
qingcheng-xiaochengxu/pages/course/createCourseOrder/createCourseOrder.js
2025-08-15 11:14:42 +08:00

230 lines
5.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { baseUrl, globalImgUrl } from "../../../request";
const { notLogin } = require('../../../utils/util')
// pages/course/createCourseOrder/createCourseOrder.js
Page({
/**
* 页面的初始数据
*/
data: {
courseId: 0, // 课程id
courseObj: '', // 课程对象
globalImgUrl, // 全局图片
isMaskVisible: false,
isNoticeVisible: true,
noticeHtml: `
<h3>一、购买与使用</h3>
<p>1购买后请在 <strong>「我的订单」</strong> 中查看并进入课程学习;</p>
<p>2课程为虚拟内容服务一经购买概不支持无理由退款</p>
<h3>二、账号与权益</h3>
<p>1课程仅限购买账号本人使用不可转借、分享或用于商业用途</p>
<p>2如发现恶意盗链、传播等行为我们有权封禁账号并追究法律责任。</p>
<h3>三、发票与售后</h3>
<p>如需发票或遇到问题,请在课程详情页联系客服。</p>
<h3>四、其他</h3>
<p>购买即视为同意本须知及平台服务协议。</p>
`,
},
// 打开/关闭弹窗
openNotice() {
this.setData({ isNoticeVisible: true });
},
closeNotice() {
this.setData({ isNoticeVisible: false });
},
// 阻止冒泡/滚动穿透的空函数
noop() {},
// 创建订单方法
createOrder() {
const { courseId } = this.data;
// 1. 显示遮罩,阻止二次点击
this.setData({ isMaskVisible: true });
wx.showLoading({ title: '正在创建订单...' });
let orderId ;
wx.request({
url: baseUrl + '/courseOrder/add',
method: 'POST',
data: {
courseId: courseId
},
header: {
Authorization :wx.getStorageSync('token'),
},
success : res => {
orderId = res.data.data
this.setData({ orderId })
wx.hideLoading();
if (res.data.code === 1) {
this.showIsPayModal(orderId)
} else {
// 下单失败,关闭遮罩
this.setData({ isMaskVisible: false });
wx.showModal({
title: '下单失败',
content: res.data.message || '下单失败',
showCancel: false,
confirmText: '知道了'
});
}
},
fail: () => {
wx.hideLoading();
this.setData({ isMaskVisible: false });
wx.showToast({ title: '网络错误,下单失败', icon: 'none' });
}
})
},
showIsPayModal(orderId) {
wx.showModal({
title: '下单成功',
content: '您确定要支付吗?',
cancelText: '取消',
confirmText: '确定',
success: (res) => {
if (res.confirm) {
this.payOrder(orderId);
} else if (res.cancel) {
wx.navigateTo({
url: `/pages/course/orderDetail/orderDetail?id=${orderId}`,
success: res => {
// 先把遮罩关掉
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.navigateTo({
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' });
}
});
},
// 获取课程详情
getCourseDetail() {
const cid = this.data.courseId;
wx.request({
url: baseUrl + '/course/detail/id',
method: 'POST',
data: {
id: cid
},
header: {
Authorization :wx.getStorageSync('token'),
},
success : res => {
console.log(res);
if (res.data.code === 1) {
this.setData({
courseObj: res.data.data,
})
} else {
notLogin(res.data.message)
}
}
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
console.log('课程id--->',options.id);
this.setData({
courseId: options.id,
})
this.getCourseDetail()
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})