小程序授权登陆

//app.js

App({

globalData: {

appid: '',//appid需自己提供,此处的appid我随机编写

secret: '',//secret需自己提供,此处的secret我随机编写

openid: null,

userInfo: '',

sid:''

},

onLaunch: function () {

var that = this;

// 展示本地存储能力

var logs = wx.getStorageSync('logs') || []

logs.unshift(Date.now())

wx.setStorageSync('logs', logs)

var user = wx.getStorageSync('user') || {};

var userInfo = wx.getStorageSync('userInfo') || {};

if ((!user.openid || (user.expires_in || Date.now()) < (Date.now() + 600)) && (!userInfo.nickName)) {

wx.login({

success: function (res) {

if (res.code) {

// console.log(res.code);

wx.getUserInfo({

success: function (ress) {

// console.log('111111');

var objz = {};

objz.avatarUrl = ress.userInfo.avatarUrl;

objz.nickName = ress.userInfo.nickName;

wx.setStorageSync('userInfo', objz);//存储userInfo

},

fail:function(ress){

console.log(ress)

}

});

var d = that.globalData;//这里存储了appid、secret、token串

var l = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + d.appid + '&secret=' + d.secret + '&js_code=' + res.code + '&grant_type=authorization_code';

wx.request({

url: l,

data: {},

method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT

// header: {}, // 设置请求的 header

success: function (res) {

var obj = {};

obj.openid = res.data.openid;

obj.unionid = res.data.unionid;

obj.expires_in = Date.now() + res.data.expires_in;

wx.setStorageSync('user', res.data);//存储openid

}

});

} else {

console.log('获取用户登录态失败!' + res.errMsg)

}

}

});

}

},

//获取openid

getOpenId: function (cb) {

var that = this

if (this.globalData.openid) {

typeof cb == "function" && cb(this.globalData.openid)

} else {

wx.login({

success: function (res) {

if (res.code) {

var d = that.globalData;//这里存储了appid、secret、token串

var l = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + d.appid + '&secret=' + d.secret + '&js_code=' + res.code + '&grant_type=authorization_code';

//发起网络请求

wx.request({

url: l,

data: {},

method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT

success: function (result) {

var obj = {};

obj.openid = result.data.openid;

obj.unionid = result.data.unionid;

wx.setStorageSync('user', result.data);//存储openid

that.globalData.openid = result.data.openid

typeof cb == "function" && cb(that.globalData.openid);

}

})

} else {

conso1e.log('获取用户登录态失败!' + res.errM3g)

}

}

});

}

},

getUserInfo: function (cb) {

var that = this

if (that.globalData.userInfo) {

typeof cb == "function" && cb(that.globalData.userInfo)

} else {

//调用登录接口

wx.getSetting({

success: res => {

console.log(res.authSetting['scope.userInfo'])

if (res.authSetting['scope.userInfo']) {

wx.getUserInfo({

success: function (res) {

wx.setStorageSync('userInfo', res.userInfo);//存储userInfo

var userInfo = res.userInfo;

// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回

that.globalData.userInfo = res.userInfo;

// 所以此处加入 callback 以防止这种情况

// if (this.userInfoReadyCallback) {

// this.userInfoReadyCallback(res)

// }

typeof cb == "function" && cb(that.globalData.userInfo);

}

})

} else {

// 未授权,跳转到授权页面

wx.reLaunch({

url: '/pages/auth/auth',

})

}

}

})

}

}

})

猜你喜欢

转载自blog.csdn.net/weixin_42593599/article/details/81983708
今日推荐