当前位置: 首页 > news >正文

uniap之微信公众号支付

近来用uniapp开发H5的时候,需要接入支付,原来都是基于后端框架来做的,所以可谓是一路坑中过,今天整理下大致流程分享给大家。

先封装util.js,便于后面调用
const isWechat =function(){return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === 'micromessenger';
}
const wechatAppid = function() {return '你的appid';
}
const payed = function(data){WeixinJSBridge.invoke('getBrandWCPayRequest', data, function(respay) {if (respay.err_msg === "get_brand_wcpay_request:ok") {uni.showToast({title:'支付成功',icon:"none"})} else if (respay.err_msg === "get_brand_wcpay_request:cancel") {uni.showToast({title:"取消支付",icon:"none",duration:2000})} else if (respay.err_msg === "get_brand_wcpay_request:fail") {uni.showToast({title:"支付失败",icon:"none",duration:2000})}}, function(err) {uni.showToast({title:err,icon:"none",duration:2000})})
}
/*** http请求* action 方法名* data  传输数据* Method 请求方式 GET POST*/
const Requests = function (action,data,Method='GET',event) {var headers = {'content-type': 'application/json' // 默认值}if (Method == 'POST') {headers = {'content-type': 'application/x-www-form-urlencoded' // 默认值}}uni.request({url: config.requestUrl + action,method:Method,header:headers,data: data,success(res) {if (res.data.status == 100) {return event(res.data);}else {uni.showToast({title: res.data.msg,icon:'none'})}},fail() {uni.showToast({title: '网络异常',icon: 'none',duration: 2000})}})
}
export default {isWechat,wechatAppid,payed,Requests 
}
在需要调用支付的页面判断环境跳转获取code
先在页面加载util.js
import util from 'common/util.js'
再在onload里判断获取code
if(!options.code === false){this.code  =options.code
}else{if(util.isWechat()){let appid = util.wechatAppid();let local = window.location.href;window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appid+'&redirect_uri='+encodeURIComponent(local)+'&response_type=code&scope=snsapi_base&state=1#wechat_redirect'return;}
}
最后再需要支付的地方进行调用
var that = this
//先创建订单
util.Requests('order/createOrder',{id:that.id},'POST',  function(eve) {var eves = eve.result//再从后台获取统一下单支付参数util.Requests('pay/pay',{order_id:eves,code:that.code},'POST',  function(event) {util.payed(event.result)})
})

ok,至此,就结束了,喵~

http://www.lryc.cn/news/345215.html

相关文章:

  • Django知识点总结
  • 算法(C++
  • Python专题:六、循环语句(1)
  • 力扣2105---给植物浇水II(Java、模拟、双指针)
  • Windows设置Redis为开机自启动
  • 行业早报5.10
  • Java+SpringBoot+JSP实现在线心理评测与咨询系统
  • 机器学习算法应用——K近邻分类器(KNN)
  • python数据分析——数据的选择和运算
  • 《CKA/CKAD应试指南/从docker到kubernetes 完全攻略》学习笔记 第8章 deployment
  • 步态识别论文(6)GaitDAN: Cross-view Gait Recognition via Adversarial Domain Adaptation
  • K8S中的弹性云服务如何搭建,可能遇到的问题,如何解决!(稳啦!!!!全都稳啦!!!)
  • 新增分类——后端
  • 20232801 2023-2024-2 《网络攻防实践》实践九报告
  • 类和对象--this引用原理
  • 力扣:416. 分割等和子集(Java,动态规划:01背包问题)
  • Vue进阶之Vue项目实战(一)
  • 预告 | 飞凌嵌入式邀您共聚2024上海充换电展
  • vite 打包配置并部署到 nginx
  • ResponseHttp
  • 【题解】非对称之美(规律)
  • 遇到如此反复的外贸客户,你可以这样做~
  • 【数据库】简单SQL语句
  • K邻算法:在风险传导中的创新应用与实践价值
  • 【小白的大模型之路】基础篇:Transformer细节
  • Golang | Leetcode Golang题解之第73题矩阵置零
  • JMeter性能压测脚本录制
  • 缓存雪崩、缓存击穿、缓存穿透是什么、之间的区别及解决办法
  • Pytorch张量广播
  • AI算法-高数2-导数定义和公式