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,至此,就结束了,喵~

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
//微信充值 //支付接口测试 function balance(url, data) { uni.request({ url: cfg.originUrl + '/wx/mp/js_sig.do', data: { route: url }, method: 'GET', success: (res) => { jweixin.config({ debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来 appId: res.data.appId, // 必填,公众号的唯一标识 timestamp: res.data.timestamp, // 必填,生成签名的时间戳 nonceStr: res.data.nonceStr, // 必填,生成签名的随机串 signature: res.data.signature, // 必填,签名 jsApiList: ['chooseWXPay'] // 必填,需要使用的JS接口列表 }); jweixin.ready(function() { uni.request({ url: cfg.originUrl + '/wx/recharge/pay.do', method: 'POST', header: { 'Content-type': "application/x-www-form-urlencoded", }, data: JSON.stringify(data), success: function(res) { alert("下单成功"); alert(JSON.stringify(res)); alert(res.data.order_id); all.globalData.orderId = res.data.order_id; uni.setStorageSync('orderId', res.data.order_id); jweixin.chooseWXPay({ timestamp: res.data.payParams.timeStamp, // 支付签名时间戳 nonceStr: res.data.payParams.nonceStr, // 支付签名随机串 package: res.data.payParams.package, // 接口返回的prepay_id参数 signType: res.data.payParams.signType, // 签名方式 paySign: res.data.payParams.paySign, // 支付签名 success: function(e) { alert("支付成功"); alert(JSON.stringify(e)); // 支付成功后的回调函数 } }); } }) }); jweixin.error(function(res) { // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。 console.log("验证失败!") }); } }) }
Uniapp是一款跨平台开发工具,可以使用一套代码开发多个平台的应用程序,包括微信小程序。在Uniapp中使用百度地图需要安装百度地图API插件,并在小程序的app.vue文件中进行配置。 具体步骤如下: 1. 在HBuilderX中打开Uniapp项目,在manifest.json文件中添加百度地图插件。可以在“插件”选项卡中搜索“百度地图”并添加。 2. 在app.vue文件中引入百度地图API,并在onLaunch生命周期函数中进行初始化配置。代码示例如下: ``` import bmap from '@/static/js/bmap.js' export default { onLaunch: function () { // 初始化百度地图API var BMap = new bmap.BMapWX({ ak: 'your ak' // 这里需要替换成自己的百度地图AK }) this.globalData.BMap = BMap }, globalData: { userInfo: null, BMap: null } } ``` 3. 在需要使用百度地图的页面中,引入并使用BMapWX实例进行调用。例如,在某个页面中需要获取当前位置的经纬度,代码示例如下: ``` export default { data() { return { longitude: '', latitude: '' } }, mounted() { this.getLocation() }, methods: { getLocation() { var vm = this vm.$uni.showLoading({ title: '正在获取位置' }) vm.$uni.getLocation({ type: 'wgs84', success: function(res) { var BMap = getApp().globalData.BMap var location = res.longitude + ',' + res.latitude BMap.regeocoding({ location: location, success: function(res) { vm.longitude = res.originalData.result.location.lng vm.latitude = res.originalData.result.location.lat vm.$uni.hideLoading() }, fail: function() { vm.$uni.hideLoading() vm.$uni.showToast({ title: '获取位置失败', icon: 'none' }) } }) }, fail: function() { vm.$uni.hideLoading() vm.$uni.showToast({ title: '获取位置失败', icon: 'none' }) } }) } } } ``` 在以上示例中,首先通过uni.getLocation()方法获取当前位置的经纬度,然后使用BMapWX实例的regeocoding方法将经纬度转换为地址信息,最后将地址信息中的经纬度提取出来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

袁威

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值