//截取code
//getUrlParam方法就是使用正则截取地址栏里的code,有兴趣可以研究一下,没兴趣直接拿来用
getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]);
return null;
},
// 判断微信还是浏览器
isWeiXin() {
var ua = window.navigator.userAgent.toLowerCase();
// console.log(ua);//mozilla/5.0 (iphone; cpu iphone os 9_1 like mac os x) applewebkit/601.1.46 (khtml, like gecko)version/9.0 mobile/13b143 safari/601.1
if (ua.match(/MicroMessenger/i) == 'micromessenger') {
return true;
} else {
return false;
}
},
getCode() {
const code = this.getUrlParam('code') // 截取路径中的code
if (code == null || code == '') {
const local = encodeURIComponent(window.location.href)
let appid = ' ';
let url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid + '&redirect_uri=' +
local + '&response_type=code&scope=snsapi_base&href=' + window.location.href +
'#wechat_redirect';
window.location.replace(url)
} else {
this.getOpenId(code)
}
},
H5获取用户code,换openID
最新推荐文章于 2024-10-22 21:36:43 发布