APP端微信登录代码如下(示例):
// 定义多类型登录的function
other_login(loginRes, infoRes, type) {
let _this = this;
let url;
let infos = {};
// _this.loginRes=JSON.stringify(loginRes).toString();
// _this.infoRes=JSON.stringify(infoRes).toString();
switch (type) {
case 'wx':
url = '/token/sys/login-wechat';
infos = {
openid: loginRes.authResult.openid,
nickname: infoRes.userInfo.nickName,
sex: infoRes.userInfo.gender,
headimgurl: infoRes.userInfo.avatarUrl,
refreshToken: loginRes.authResult.refresh_token
}
break;
default:
}
console.log(infos)
// 这里的infos就是用户相关的信息,下一步就应该是用这些数据向后端发去请求,再返回相应的数据
}
//APP端授权微信登录
login_weixin() {
if (!this.agree) {
uni.showToast({
title: "请先阅读并同意相关协议",
icon: "none"
})
return
}
let _this = this;
uni.login({
provider: 'weixin',
success: function(loginRes) {
// 获取用户信息
uni.getUserInfo({
provider: 'weixin',
success: function(infoRes) {
// 调用上方定义的的other_login方法
_this.other_login(loginRes, infoRes, 'wx');
}
});
}
});
}
uniapp编译到微信小程序端,用户授权登录的操作比较简单,见下方代码:
// 微信小程序登录
wxGetUserInfo() {
var that = this
// 我个人用node.js写的后端,一定要先拿到登录的凭证,一起传给后台
wx.login({
success: function(r) {
console.log(r.code) //登录凭证
}
})
wx.getUserProfile({
desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写用途
success: (res) => {
console.log(res)
var arr = res
// 下一步就是用拿到的用户数据向后端发起请求,返回你想要的数据
that.push(arr)
}
})
},
uniapp适配多端登录的文档内容有点多,本文章只讲具体实现的代码,想了解更多关于登录的可以移步官方文档-关于登录,进行阅读了解
讲讲个人在使用后,觉得应该注意的几个点: