uniapp使用微信公众号授权登录


mounted() {
console.log(window.localStorage.getItem('token'))
if(!window.localStorage.getItem('token')){ // 如果缓存localStorage中没有微信token,则需用code去后台获取
this.getCode()
} else {
//别的业务逻辑
// this.$post('user/officialLogin', {
// codes:window.localStorage.getItem('code'),
// }, function(result) {
// window.localStorage.setItem('code', window.localStorage.getItem('code'))
// console.log(result)
// });
// 获取用户信息

}
},
methods: {
getCode() { // 非静默授权,第一次有弹框
this.code = ''
// var local = 'http://192.168.1.15:8081/' // 获取页面url
var local = '' // 获取h5服务器上页面url
var appid = '' //公众号appid
this.code = this.getUrlCode().code // 截取code
if (this.code == null || this.code === '') { // 如果没有code,则去请求
window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`
} else {
// 你自己的业务逻辑
// this.$post('user/officialLogin', {
// codes:this.code,
// }, function(result) {
// console.log(result)
// window.localStorage.setItem('token', result.data.token)
// window.localStorage.setItem('user_id', result.data.user_id)
// });
}
},
getUrlCode() { // 截取url中的code方法
var url = location.search
this.winUrl = url
var theRequest = new Object()
if (url.indexOf("?") != -1) {
var str = url.substr(1)
var strs = str.split("&")
for(var i = 0; i < strs.length; i ++) {
theRequest[strs[i].split("=")[0]]=(strs[i].split("=")[1])
}
}
return theRequest
},

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM