标签:setting sessionid div gate func lld span login tle
基本功能是OK,但是感觉传的数据太多,不安全,需要改写。
App({ d: { hostUrl: ‘http://www.test.com/index.php‘, //请填写您自己的小程序主机URL appId: "xxx", appKey: "xxx", ceshiUrl: ‘http://www.test.com/index.php‘,//请填写您自己的测试URL }, //小程序初始化完成时触发,全局只触发一次 onLaunch: function () { //调用API从本地缓存中获取数据 var logs = wx.getStorageSync(‘logs‘) || [] logs.unshift(Date.now()) wx.setStorageSync(‘logs‘, logs); //login this.getUserInfo(); }, getUserInfo: function (cb) { var that = this if (this.globalData.userInfo) { typeof cb == "function" && cb(that.globalData.userInfo) } else { wx.getSetting({ success: function (res) { if (res.authSetting[‘scope.userInfo‘]) { wx.login({ success: function (res) { //console.log(res); var code = res.code; //get wx user simple info wx.getUserInfo({ withCredentials: true, success: function (res) { //如果已经授权过那么会执行这里代码,console.log("已授权标记"); that.globalData.userInfo = res.userInfo; typeof cb == "function" && cb(that.globalData.userInfo); //get user sessionKey that.getUserSessionKey(code); if (that.userInfoReadyCallback) { that.userInfoReadyCallback(res) } } }); } }) } else { // 没有授权,重定向到 loading 启动页 wx.navigateTo({ url: ‘../tologin/tologin‘, }) } } }) } }, getUserSessionKey: function (code) { //用户的订单状态 var that = this; wx.request({ url: that.d.ceshiUrl + ‘/Api/Login/getsessionkey‘, method: ‘post‘, data: { code: code }, header: { ‘Content-Type‘: ‘application/x-www-form-urlencoded‘ }, success: function (res) { //--init data var data = res.data; if (data.status == 0) { wx.showToast({ title: data.err, duration: 2000 }); return false; } that.globalData.userInfo[‘sessionId‘] = data.session_key; that.globalData.userInfo[‘openid‘] = data.openid; that.onLoginUser(); }, fail: function (e) { wx.showToast({ title: ‘网络异常!err:getsessionkeys‘, duration: 2000 }); }, }); }, //授权登录 onLoginUser: function () { var that = this; var user = that.globalData.userInfo; wx.request({ url: that.d.ceshiUrl + ‘/Api/Login/authlogin‘, method: ‘post‘, data: { SessionId: user.sessionId, gender: user.gender, NickName: user.nickName, HeadUrl: user.avatarUrl, openid: user.openid }, header: { ‘Content-Type‘: ‘application/x-www-form-urlencoded‘ }, success: function (res) { //--init data var data = res.data.arr; var status = res.data.status; if (status != 1) { wx.showToast({ title: res.data.err, duration: 3000 }); return false; } that.globalData.userInfo[‘id‘] = data.ID; that.globalData.userInfo[‘NickName‘] = data.NickName; that.globalData.userInfo[‘HeadUrl‘] = data.HeadUrl; var userId = data.ID; if (!userId) { wx.showToast({ title: ‘登录失败!‘, duration: 3000 }); return false; } that.d.userId = userId; }, fail: function (e) { wx.showToast({ title: ‘网络异常!err:authlogin‘, duration: 2000 }); }, }); }, globalData: { userInfo: null }, onPullDownRefresh: function () { wx.stopPullDownRefresh(); } });
如果不想首页自动登陆,在其它页调用登陆:
// pages/user/user.js var app = getApp() Page({ data: { userInfo: {}, orderInfo: {}, loadingText: ‘加载中...‘, loadingHidden: false, }, onLoad: function () { var that = this //调用应用实例的方法获取全局数据 app.getUserInfo(function (userInfo) { //更新数据 that.setData({ userInfo: userInfo, loadingHidden: true }) }); console.log("个人中心:--" + app.d.userId); this.loadOrderStatus(); }, })
标签:setting sessionid div gate func lld span login tle
原文地址:https://www.cnblogs.com/aguncn/p/11100110.html