博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
我感觉这个书上的微信小程序登陆写得不好
阅读量:4309 次
发布时间:2019-06-06

本文共 4296 字,大约阅读时间需要 14 分钟。

基本功能是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.jsvar 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();  },})

 

转载于:https://www.cnblogs.com/aguncn/p/11100110.html

你可能感兴趣的文章
图文介绍openLDAP在windows上的安装配置
查看>>
优化UITableViewCell高度计算的那些事(RunLoop)
查看>>
Source Insight上手教程
查看>>
Xamarin.forms--------------Picker
查看>>
C++ 并发编程实战
查看>>
Asp调用MVC后部署服务器
查看>>
C# XML操作类
查看>>
bzoj1998: [Hnoi2010]Fsk物品调度
查看>>
选择监听事件ItemListener(是否被选择)
查看>>
java web开发(一) 环境搭建
查看>>
hdu_1017(水水水,坑格式)
查看>>
lua编译出so文件
查看>>
架构设计
查看>>
一张图解决Struts2添加源码
查看>>
Alpha 冲刺 (10/10)
查看>>
学习js(Jquery)
查看>>
Gitolite v3安装配置指南
查看>>
adb 之android的神器am
查看>>
Linux2.6内核--内存管理(1)--分页机制
查看>>
对ASP.NET程序员非常有用的85个工具
查看>>