uniapp开发微信公众号登录
app.vue
onLaunch: function(options) {
let storage_token = getStorageSync('token');
if (storage_token) {
setStorageSync('token', storage_token);
this.upDateUserInforMutaions();
this.upDateConfigComnMutaions();
} else {
if (this.getUrlKey('code')) {
this.loginList(this.getUrlKey('code'), options.query.referid)
} else {
this.login()
}
}
},
methods: {
login() {
window.location.href = apiBaseConfig.weiXinUrl;
},
async loginList(wxcode, referid) {
let self = this;
const data_back = await getLoginCodeAuth({
code: wxcode,
refer_id: referid
})
const {
code,
data
} = data_back;
if (code === 0) {
setStorageSync("token", data.token)
self.upDateUserInforMutaions();
self.upDateConfigComnMutaions();
uni.reLaunch({
url: '/pages/index/index'
})
} else {
uni.showToast({
title: '抱歉,登录失败!',
icon: "none",
duration: 2000
});
}
},
getUrlKey(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) ||
[, ""
])[1].replace(/\+/g, '%20')) || null
},
}