//判断是否为公众号模拟器环境
const isWechat = () => {
return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === 'micromessenger';
};
//判断公众号截取code
const getUrlParam = name => {
let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
let r = window.location.search.substr(1).match(reg);
if (r != null) {
return unescape(r[2]);
}
return null;
};
onLoad: function() {
this.getWxCode();
},
getWxCode() {
var _this = this;
if (isWechat()) {
let appid = '公众号appid'; //小程序appid和公众号appid不一样
let code = getUrlParam('code'); //是否存在code
let local = window.location.href;
if (code == null || code === '' || code == undefined) {
//不存在就打开上面的地址进行授权
window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURICompo
微信H5登录功能实现
最新推荐文章于 2024-06-23 10:41:54 发布