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}` }) } })
本文来自博客园,作者:熬夜的布偶猫#,转载请注明原文链接:https://www.cnblogs.com/prince11/p/17807119.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Blazor Hybrid适配到HarmonyOS系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 解决跨域问题的这6种方案,真香!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库
· 分享4款.NET开源、免费、实用的商城系统