Files
xuan_wechat/pages/pay/index.js
2023-09-22 17:28:59 +08:00

124 lines
3.2 KiB
JavaScript

/*
* 手太欠
* 愿这世界都如故事里一样 美好而动人~
*/
Page({
/**
* 页面的初始数据
*/
data: {
total : "0.00",
orderNo : "",
modelId : "",
modelType : "",
payType : "",
orderNos : [],
can : {
coin : 0,
wechat : 0,
},
loding : false,
noShow : false
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
const { order_id, order_type, order_no } = JSON.parse(decodeURIComponent(options.params))
wx.showLoading({
title: '加载中...',
mask : true
})
wx.$api.pay.info({ order_id, order_type }).then(res => {
let { can, total, model_type, model_id, order_nos } = res.data
this.setData({
orderNo : order_no,
payType : res.data.default,
modelId : model_id,
modelType : model_type,
orderNos : order_nos,
total,
can
})
wx.hideLoading()
})
},
/**
* 选择支付方式
*/
onPayType(e){
let { value } = e.detail
this.setData({
payType: value
})
},
/**
* 立即支付
*/
onPay(){
this.setData({
loding: true
})
switch (this.data.payType) {
case 'wechat':
this.wechatPay()
break;
case 'coin':
wx.showToast({
title: '支付方式暂未开放',
icon : 'none'
})
this.setData({
loding: false
})
break;
}
},
/**
* 微信支付
*/
wechatPay(){
wx.login({
success: wxCode => {
let { code } = wxCode;
wx.$api.pay.wechatPay({
order_type : this.data.modelType,
order_id : this.data.modelId,
type : "mini",
code : code
}).then(res => {
let { wechat, trade_id } = res.data
let wechatObj = JSON.parse(wechat)
wx.requestPayment({
nonceStr : wechatObj.nonceStr,
package : wechatObj.package,
paySign : wechatObj.paySign,
timeStamp: wechatObj.timeStamp,
signType : wechatObj.signType,
success : () => {
wx.redirectTo({
url: './success/success?trade_id=' + trade_id,
})
}
})
}).finally(() => {
this.setData({
loding: false
})
})
}
})
},
/**
* 展开订单号
*/
noTap() {
this.setData({
noShow: !this.data.noShow
})
}
})