【支付】h5端在 微信内置浏览器中 微信支付 uniapp h5端绑定微信(微信内置浏览器)

  1. 项目中,得先绑定微信
  2. 安装 微信JS-SDK npm install jweixin-module --save 官网 搜索jweixin
  3. 绑定微信后才能调起微信支付

在这里插入图片描述

项目中绑定微信

目前是在 登录成功后,自动绑定微信,首次 登录会有弹窗确定出现,往后是静默绑定,只能在线上测试

onShow() {
		var ua = window.navigator.userAgent.toLowerCase();
		if (ua.match(/micromessenger/i) == 'micromessenger') {
			this.getweixinCode();
		} else {
			console.log('不在微信环境中')
		}
		
},
	
	
=======================分界线,以下的是在 methods: {} 方法里调用


// 获取code
getweixinCode() { 
	this.code = ''
	var local = window.location.href // 获取页面url
	var appid = 'wx45c457658667234421djh021fa44e644'
	this.code = this.getUrlCode().code // 截取code
	if (this.code == null || this.code === '') { // 如果没有code,则去请求
		window.location.href =
			`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`
	} 
},
getUrlCode() {
	// 截取url中的code方法
	var url = location.search
	var theRequest = new Object()
	if (url.indexOf("?") != -1) {
		var str = url.substr(1)
		var strs = str.split("&")
		for (var i = 0; i < strs.length; i++) {
			theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1])
		}
	}
	return theRequest
},
// 绑定微信
bindingXH5() {
	let that = this;
		//判断是否在微信中
		var ua = window.navigator.userAgent.toLowerCase();
		if (ua.match(/micromessenger/i) == 'micromessenger') {
					
				if(!that.code){
					that.getweixinCode();
				}
				
				// 调用 绑定微信 的接口 start ======== 根据自身项目情况而定
				that.post(that.apis.bangdingweixin, {
					data: {
						  userId:uni.getStorageSync('id'),
						  code: that.code
					}
				}).then(res => {
					console.log(res, '绑定微信');
					if (res.code == 0) {
						
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				});
		// 调用 绑定微信 的接口 end==========
			
		} else {
			uni.reLaunch({
				url:'/pages/index/index'
			});
		}
},

页面中调起微信支付

//h5 绑定微信公众号 微信支付 res 是后端返回的 数据,里面包含需要的信息
// appId: '', // 必填,公众号的唯一标识
// timestamp: , // 必填,生成签名的时间戳
// nonceStr: '', // 必填,生成签名的随机串
// signature: '',// 必填,签名
	wxH5Pay(res){
			let that = this;
			var data = res;
			//判断是否在微信中
			var ua = window.navigator.userAgent.toLowerCase();
			if (ua.match(/micromessenger/i) == 'micromessenger') {
				// console.log('是在微信客户端')
				// 绑定微信
				WeixinJSBridge.invoke(
					'getBrandWCPayRequest', {
						"appId": data.appId, //公众号名称,由商户传入     
						"timeStamp": data.timeStamp, //时间戳,自1970年以来的秒数     
						"nonceStr":data.nonceStr, //随机串     
						"package": data.package,
						"signType": data.signType, //微信签名方式:     
						"paySign": data.paySign,
					},
					res=> {
						// 支付成功
						if (res.err_msg == "get_brand_wcpay_request:ok") {
					
							uni.showToast({
								title:'支付成功',
								icon:'success'
							});
					
							that.showPay = false;
							that.isCreateOrder = false;
							that.isPayOrder = false;
						
						}
						// 支付过程中用户取消
						if (res.err_msg == "get_brand_wcpay_request:cancel") {
								uni.showToast({
									title:'支付已取消',
									icon:'none'
								});
						}
						// 支付失败
						if (res.err_msg == "get_brand_wcpay_request:fail") {
							uni.showToast({
								title:'支付失败',
								icon:'none'
							});
						}
						/**
						 * 其它
						 * 1、请检查预支付会话标识prepay_id是否已失效
						 * 2、请求的appid与下单接口的appid是否一致
						 * */
						if (res.err_msg == "调用支付JSAPI缺少参数:total_fee") {
							uni.showToast({
								title:'支付失败',
								icon:'none'
							});
						}
					});
					
			} else {
					// console.log('不是微信客户端')
					uni.showToast({
						title:'请在微信浏览器中打开',
						icon:'none'
					});
					return;
			}
	},
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在UniApp实现在微信浏览器进行H5绑定微信的功能,你可以按照以下步骤进行操作: 1. 在微信开放平台上创建一个应用,获取到AppID。 2. 在UniApp项目的`manifest.json`文件,添加微信登录的权限配置。在`uni-app`字段下添加以下代码: ```json "wx": { "appid": "你的AppID", "scope": "snsapi_login", "state": "uniapp", "redirect_uri": "http://your-domain.com/auth" } ``` 将其的`appid`替换为你在微信开放平台上获取到的AppID。`redirect_uri`是用户登录后重定向的URL,需要替换为你自己的URL。 3. 在需要进行微信绑定的页面,引入UniApp提供的wx组件,示例代码如下: ```html <template> <view> <button @click="bindWechat">绑定微信</button> </view> </template> <script> import { uniLogin } from '@/common/utils/wx' export default { methods: { async bindWechat() { try { const res = await uniLogin() // 处理绑定微信成功后的逻辑 } catch (error) { // 处理绑定微信失败的逻辑 } } } } </script> ``` 4. 在项目创建一个`common/utils/wx.js`文件,实现微信登录的方法。可以使用`uni.login`和`uni.getUserInfo`进行登录操作,示例代码如下: ```js export function uniLogin() { return new Promise((resolve, reject) => { uni.login({ provider: 'weixin', success: (res) => { uni.getUserInfo({ provider: 'weixin', success: (infoRes) => { resolve({ code: res.code, userInfo: infoRes.userInfo }) }, fail: () => { reject(new Error('获取用户信息失败')) } }) }, fail: () => { reject(new Error('微信登录失败')) } }) }) } ``` 这里使用了`uni.login`获取登录凭证,再通过`uni.getUserInfo`获取用户信息。你可以根据需要进行适当的修改。 以上是在UniApp实现在微信浏览器进行H5绑定微信的步骤,希望能对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值