uniapp做app跳转小程序支付功能
1.app里面代码
app向小程序路径传参的时候,如果太长或者是里面有特殊符号建议先使用编码然后再小程序端解码实现传送(编码代码如下)
encodeURIComponent(JSON.stringify(params)) // 编码
JSON.parse(decodeURIComponent(option.params)) //解码
plus.share.getServices((s) => { let shares = {}; for (let i = 0; i < s.length; i++) { let t = s[i]; shares[t.id] = t; } let sweixin = shares['weixin']; this.weixin = sweixin this.weixin ? this.weixin.launchMiniProgram({ path: `pages/institutionalPay/institutionalPay?params=${params}&token=${value}&cid=${this.cid}&uuid=${this.uuid}`,//路径可以传值 type: 0, //可取值: 0-正式版; 1-测试版; 2-体验版。 默认值为0。 id: 'xxxxxxxxx', //小程序的原始id }, res => { console.log(res) }, err => { console.log(err); }, ) : plus.nativeUI.alert('当前环境不支持微信操作!'); return }, function(e) { console.log("获取分享服务列表失败:" + e.message); });
2.小程序端在onload接收,首先获取openid(此方法和小程序支付有可能有延迟,所以建议在获取到openid自后再进行支付操作)
//获取openid
wx.login ({ success : function (res) { if (res.code) { const params = { code:res.code } getOpenidNew (params).then (value =>{ console.log('openid',value) uni.setStorageSync('openid',value.data.openId) that.payMoney() } ).catch(res =>{ console.log(res) }) } else { console.log('登录失败!' + res.errMsg) } } })
通过后端接口获取支付参数
唤起支付 (支付五大要素)
uni.requestPayment({ provider: 'wxpay', timeStamp:elements.data.timeStamp , nonceStr:elements.data.nonceStr , package:elements.data.packageValue , signType:elements.data.signType, paySign:elements.data.paySign, success: async (res) =>{ const allreadyPay = await appletAllreadyPay(this.orderParams) //支付成功 uni.showToast({ title: '支付成功', icon: 'none', duration: 3000 }) this.orderParams = encodeURIComponent(JSON.stringify(this.orderParams)) uni.reLaunch({ url:`/pages/institutionalPay/unpaid/unpaid?flag=${0}&orderParams=${this.orderParams}` }) }, fail: (err) => { uni.showToast({ title: '支付失败', icon: 'none', duration: 3000 }) this.orderParamsOrder = encodeURIComponent(JSON.stringify(this.orderParamsOrder)) this.orderParams = encodeURIComponent(JSON.stringify(this.orderParams)) //取消支付 uni.reLaunch({ url:`/pages/institutionalPay/unpaid/unpaid?flag=${1}&orderParamsOrder=${this.orderParamsOrder}&orderParams=${this.orderParams}` }) } })
本文来自博客园,作者:prince11,转载请注明原文链接:https://www.cnblogs.com/prince11/p/17807119.html