//方法:用来判断是否是微信内置的浏览器
isWechat() {
// #ifdef H5
return String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
// #endif
},
//方法:用来提取code
getUrlCode(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) ||
[, ''
])[1]
.replace(/\+/g, '%20')) || null
},
//检查浏览器地址栏中微信接口返回的code
checkWeChatCode() {
if (!uni.getStorageSync('userData')) {
let that = this
let code = this.getUrlCode('code')
if (code) {
this.getOpenidAndUserinfo(code)
} else {
that.getWeChatCode()
}
}
},
//请求微信接口,用来获取code
getWeChatCode() {
let local = encodeURIComponent(window.location.href); //获取当前页面地址作为回调地址
//通过微信官方接口获取code之后,会重新刷新设置的回调地址【redirect_uri】
window.location.href =
"https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
this.wxAppid +
"&redirect_uri=" + 'http://wx.tuanzhangapp.com' +
"&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
},
//把code传递给后台接口,静默登录
getOpenidAndUserinfo(code) {
let params = {
code: code
}
this.$api.goods.loginwxs(params).then((res) => {
if (res.data != null || res.data != '' || res.data != undefined) {
uni.setStorageSync('userData', res.data)
}
uni.showToast({
title: '登录成功',
icon: 'none'
})
})
},
h5微信内置浏览器登录
于 2022-09-14 10:16:32 首次发布