const { default: api } = require("../../js/api"); const util = require('../../utils/util.js') // pages/orderConfirm/orderConfirm.js Page({ /** * 页面的初始数据 */ data: { nozzleId: 0, type: '后支付', order: null, // order: { // oilName: '好名字', // nozzle: "1", // volume: "1", // amount: "1", // payAmount: "1", // discount: "1-1", // orderId: "1", // time: "2025-4-3" // }, oprationBtn1: '', oprationBtn2: '对此订单有疑问?' }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { const link = decodeURIComponent(options.q) // 获取到二维码原始链接 var id = link.split('wxapp?id=')[1]; if (id == undefined) { id = options.id; } this.setData({ nozzleId: id }) this.getOrder() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { }, /** 获取订单信息 */ getOrder() { wx.showLoading({ title: '正在获取订单', }) api.request_GetMiniProgramTransactionsUnpaidNozzle(Number(this.data.nozzleId)) .then(res => { wx.hideLoading() if (res.data.statusCode == 203) { //若为203,证明还未登录,跳转到登录页,这里可能刚从主页跳转过来,频繁的跳转可能会跳转页面超时,故而加上延时 setTimeout(() => { wx.navigateTo({ url: '../login/login' }) }, 500) return } console.log("获取到未支付订单", res) var order = res.data.data[0]; var time = util.formatDateNotSecond(order.fuelItemTransactionEndTime); this.setData({ order: { oilName: order.productName, nozzle: order.nozzleId, volume: order.originalQty, amount: order.originalAmount, payAmount: order.actualPaymentAmount, discount: order.originalAmount - order.actualPaymentAmount, orderId: order.id, time: time, price:order.price } }) }).catch(err => { wx.hideLoading() console.log("未获取到未支付订单") }) }, /** 后支付查看更多交易 */ toMordOrderPage() { wx.showLoading({ title: '正在跳转', }) var that = this; wx.navigateTo({ url: '../moreOrder/moreOrder?nozzle=' + this.data.nozzleId, events: { acceptOrderFromMoreOrder: function (data) { console.log("获取到传送过来的订单", data) that.setData({ order: data }) } } }) wx.hideLoading() }, /** 支付按钮点击事件 */ toPay() { wx.showLoading({ title: '正在跳转', }) var that = this; api.request_wechatPay(this.data.order.orderId).then(res => { console.log("支付结果", res) if(res.data.statusCode != 200) { wx.hideLoading() wx.showModal({ title: '提示', content: '支付失败', }) return } this.subMessage() // this.pay() }).catch(err => { wx.hideLoading() console.log("支付失败", err) wx.showModal({ title: '提示', content: '支付失败', }) }) }, /** 订阅消息模板 */ subMessage() { const that = this; wx.requestSubscribeMessage({ tmplIds: ['V0tl-4n-5hwNZc4SrEttvrmawAyM-SB0pQWZNwp54Ks'], // 最多支持3条 success(res) { that.pay() // 'accept'表示用户同意订阅该条id对应的模板消息 if (res['V0tl-4n-5hwNZc4SrEttvrmawAyM-SB0pQWZNwp54Ks'] === 'accept') { // 用户同意订阅,调用云函数或服务器接口发送订阅消息 // wx.cloud.callFunction({ // name: 'sendSubscribeMessage', // data: { // templateId: '配置好的模板ID', // openid: 'o8pFb5cWH1KkBDvGls2X7yMiFkGA', // data: { // thing1: { // value: '活动名称' // }, // // 其他参数... // } // }, // success(res) { // console.log('订阅消息发送成功', res) // }, // fail(err) { // console.error('订阅消息发送失败', err) // } // }) } }, fail(err) { that.pay() wx.showModal({ title: '提示', content: '目前暂未获取到未支付订单', }) } }) }, pay() { const that = this; api.request_wechatPay(this.data.order.orderId) .then(res => { wx.requestPayment({ timeStamp: res.data.data.unifiedOrderResult.timeStamp, nonceStr: res.data.data.unifiedOrderResult.nonceStr, package: res.data.data.unifiedOrderResult.package, signType: res.data.data.unifiedOrderResult.signType, paySign: res.data.data.unifiedOrderResult.paySign, success: res => { wx.hideLoading() that.sendMessage() that.toPayResult() }, fail: res => { wx.hideLoading() wx.showModal({ title: '提示', content: '支付失败', }) } }) }); }, /** 发送消息模板 */ sendMessage() { const message = { trxid: this.data.order.orderId, orderType: '支付' } api.request_sendMessage(message).then(res => { console.log("发送消息模板结果", res) }).catch(err => { console.log("发送消息模板失败", err) }) }, /** 跳转到支付结果页 */ toPayResult() { wx.redirectTo({ url: '../payResult/payResult', }) }, /** 跳转到支付结果页(不用这个) */ toPayResult_back() { const that = this; setTimeout(function () { wx.navigateTo({ url: '../payResult/payResult?nozzle=' + that.data.nozzleId, events: { acceptOrderFromPayResult: function (data) { console.log("获取到来自result页面传送过来的订单", data) that.setData({ order: data }) } } }) }, 500) } })