import { baseUrl, globalImgUrl } from "../../../request"; // pages/course/createCourseOrder/createCourseOrder.js Page({ /** * 页面的初始数据 */ data: { courseId: 0, // 课程id courseObj: '', // 课程对象 globalImgUrl, // 全局图片 isMaskVisible: false }, // 创建订单方法 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, }) } } }) }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { console.log('课程id--->',options.id); this.setData({ courseId: options.id, }) this.getCourseDetail() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })