这次是通过微信小程序的openid登陆,openid是用户的唯一标识
//app.js
var nickName;
var head;
var api = require('common/js/api.js')
App({
onLaunch: function () {
// 展示本地存储能力
var that = this;
login();
function login() {
wx.checkSession({
success: function () {
// wxlogin();
var mid = wx.getStorageSync('mid');
wxlogin();
if (mid.length === 0) {
wx.openSetting({
success: function (data) {
if (data) {
if (data.authSetting["scope.userInfo"] == true) {
// loginStatus = true;
wx.getUserInfo({
withCredentials: false,
success: function (res) {
wxlogin();
},
fail: function () {
console.info("2授权失败返回数据");
}
});
}
}
},
fail: function () {
console.info("设置失败返回数据");
}
});
wxlogin();
console.log('进来了')
}
},
fail: function () {
wxlogin();
}
});
};
function wxlogin() {
wx.login({
success: function (res) {
var js_code = res.code;
if (res.code) {
//发起网络请求
// 获取用户信息
wx.getUserInfo({
withCredentials: true,
success: function (res) {
nickName = res.userInfo.nickName;
head = res.userInfo.avatarUrl;
// 传入用户姓名和jscode
wx.request({
url: api.api.login,
header: { "Content-Type": "application/x-www-form-urlencoded" },
method: 'POST',
data: {
'js_code': js_code,
'nickname': nickName,
'head': head,
'encryptedData': res.encryptedData,
'iv': res.iv
},
success: function (res) {
that.globalData.nickname = nickName;
that.globalData.avatarUrl = head;
//获取session
var sess = res.data.data.key
wx.setStorageSync('mid', sess);
that.globalData.oid = wx.getStorageSync('mid')
console.log(that.globalData)
console.log(head)
}
});
}
});
} else {
console.log('获取用户登录态失败!' + res.errMsg)
}
}
});
}
},
globalData: {
userInfo: null,
oid: wx.getStorageSync('mid'),
resultList: [],
nickname: '',
avatarUrl: ''
}
})