onLoad() {
this.serviceIp = this.GLOBAL.serviceIp;
var that = this;
// #ifdef H5
uni.switchTab({
url: '../index/index'
});
// #endif
// #ifdef MP-WEIXIN
wx.getSetting({
success(res) {
//判断是否 授权用户信息
if (!res.authSetting['scope.userInfo']) {
//授权 会弹出 用户授权框
wx.authorize({
scope: 'scope.userInfo',
success() {
//登录
that.wxgetInfo();
}
});
} else {
// 登录
that.wxgetInfo();
}
}
});
// #endif
},
methods: {
wxgetInfo: function() {
let that = this;
//获取 用户信息 然后 去登录
wx.getUserInfo({
success(res) {
let userInfo=res.userInfo;
that.wxlogin(userInfo.nickName ,userInfo.avatarUrl);
}
});
},
wxlogin: function(nickName,avatarUrl) {
let that = this;
wx.login({
success(res) {
if (res.code) {
// 发起网络请求
wx.request({
url: that.serviceIp + 'wxmini/wxlogin',
data: {
code: res.code,
// 中文解决方案
nickName:encodeURI(nickName),
avatarUrl:avatarUrl
},
success(res) {
uni.setStorageSync('storage_usermodel', res.data.obj);
uni.switchTab({
url: '../index/index'
});
}
});
} else {
console.log('登录失败!' + res.errMsg);
}
}
});
}
}
//服务器 通过
https://api.weixin.qq.com/sns/jscode2session?appid=appid&secret=appsecret&js_code=code&grant_type=authorization_code
获取 openId 和 session_key 注册用户数据 进行保存
处理完后 返回给 客户端