笔记:uniapp授权微信公众号
由于是第一次对接uniapp授权微信公众号,过程也很复杂,记录一下,下次不迷路。onLoad() {//获取当前页面的urllet link =window.location.href;console.log('我是请求的url')console.log(link)// let link = 'http://xxxxxx.com/?code=001vGk&state=123#/';let
·
由于是第一次对接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);
})
}
},
更多推荐
已为社区贡献1条内容
所有评论(0)