1、获取code方法
//微信登录获取code
Fun_code() {
//判断是否在微信浏览器中打开
let iswx = this.isWechat();
if(!iswx){
uni.navigateTo({url: '不在微信浏览器里面跳转的提示地址'});
return false;
}
// 当前H5页面的url
var local = encodeURIComponent('返回地址,要和公众号配置的链接地址一致');
var appid = '公众号appid';
//通过微信官方接口获取code之后,会重新刷新设置的回调地址
var wxUrl =
'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
appid +
'&redirect_uri=' +
local +
'&response_type=code' +
'&scope=snsapi_userinfo' +
'&state=STATE#wechat_redirect';
window.location.href = wxUrl;
},
2、onLoad函数中获取链接上面返回的code
//获取链接上面的code
let code = decodeURIComponent((new RegExp('[?|&]' + 'code' + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || null
3、判断是否在微信浏览器里面方法
//是否在微信浏览器里面
isWechat() {
return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
},