笔记:uniapp授权微信公众号

由于是第一次对接uniapp授权微信公众号,过程也很复杂,记录一下,下次不迷路。

	onLoad() {
			//获取当前页面的url
			let link =window.location.href;
			console.log('我是请求的url')
			console.log(link)
			// let link = 'http://xxxxxx.com/?code=001vGk&state=123#/';
			let code = null;
			// 判断link中有没有code=字符串,  
			if (link.indexOf('code=') == -1) {
				console.log('我是需要授权页的判断')
				//没有code= 发请求
				let appid = 'xxxxxx';// 公众号的固定appid
				let uri = encodeURIComponent(link);// 编码后的回调地址
				let authURL =`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`;
				window.location.href = authURL;
				
			} else {
				console.log('我是授权成功后跳转的页面')
				let temp = decodeURIComponent((new RegExp('[?|&]' + 'code' + '=' + '([^&;]+?)(&|#|;|$)').exec(link) || [,''])[1].replace(/\+/g, '%20')) || null
				//  回调函数已经执行 返回的链接存在code= 地址解析
				this.code = temp;
				console.log('我是code的值')
				console.log(this.code)
				// 接口的条用跳转
				this.$httpApi.wxcallback(this.code).then(res => {
					console.log('我是res')
					console.log(res);
					console.log('我是callback的unionid')
					console.log(res.data.unionid)
					this.unionid  = res.data.unionid;
					uni.setStorageSync('openid',this.unionid);
				})
			}
		},
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值