uniap之微信公众号支付

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

先封装util.js,便于后面调用
javascript 复制代码
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
javascript 复制代码
import util from 'common/util.js'
再在onload里判断获取code
javascript 复制代码
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;
	}
}
最后再需要支付的地方进行调用
javascript 复制代码
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,至此,就结束了,喵~

相关推荐
Dxy12393102168 分钟前
Python在图片上画圆形:从入门到实战
开发语言·python
桌面运维家28 分钟前
IDV云桌面vDisk机房部署方案模板特性解析
java·开发语言·devops
飞翔的SA32 分钟前
从6.75%到100%!大模型Function Calling终极方案:Harness工程如何驯服
开发语言·ai·llm·harness
耿雨飞1 小时前
Python 后端开发技术博客专栏 | 第 09 篇 GIL 深度解析与并发编程实战 -- 多线程、多进程、协程的选型
开发语言·python
Stark-C1 小时前
NAS音乐必备神器,全平台音乐收割机!极空间部署『Go Music DL』
开发语言·后端·golang
Ulyanov1 小时前
像素迷宫:路径规划算法的可视化与实战
大数据·开发语言·python·算法
常利兵1 小时前
大文件上传不再卡顿:Spring Boot 分片上传、断点续传与进度条实现全解析
spring boot·后端·php
念何架构之路1 小时前
图解常见网络I/O复用模型
服务器·网络·php
枫叶丹41 小时前
【HarmonyOS 6.0】ArkWeb PDF预览回调功能详解:让PDF加载状态可控可感
开发语言·华为·pdf·harmonyos
小陈工2 小时前
数据库Operator开发实战:以PostgreSQL为例
开发语言·数据库·人工智能·python·安全·postgresql·开源