getCode () {
const AppId = \'你的微信公众号appid\'
const code = this.getQueryVariable(\'code\')
const local = window.location.href
if (code == null || 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=1#wechat_redirect\'
} else {
this.code = code
}
},
getQueryVariable (variable) {
let query = window.location.search.substring(1)
let vars = query.split(\'&\')
let index = vars.findIndex(item => {
let pair = item.split(\'=\')
return pair[0] === variable
})
if (index !== -1) {
return vars[index].split(\'=\')[1]
} else {
return null
}
},
实现效果