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

最新文章

  1. c++ 接口和抽象类
  2. sql语句的优化分析
  3. Use Qt in Debian for OpenCASCADE
  4. iOS之清除缓存
  5. 如何使用JS脚本从HTML中分离图片标签与文本,替换文本中指定的内容并加粗(原创)
  6. git的使用(二)
  7. 详解MySQL---DDL语句、DML语句与DCL语句
  8. JDK里的设计模式
  9. Install MongoDB on Windows (Windows下安装MongoDB)
  10. 高可用集群(HA)配置
  11. Deep Compression Compressing Deep Neural Networks With Pruning, Trained QuantizationAnd Huffman Coding
  12. Python Extension Packages下载链接
  13. .net基础收集
  14. disk.go
  15. sqlserver数据库方面的排序四大王
  16. data_type
  17. linux4.10.8 内核移植(二)---初步裁剪、分区修改和文件系统
  18. JVM 堆内存和非堆内存
  19. hdwiki 部署
  20. [LeetCode] 133. Clone Graph_ Medium tag: BFS, DFS

热门文章

  1. Zabbix 3.0 配置企业微信报警(配置zabbix-web)
  2. CentOS7.5 使用二进制程序部署Kubernetes1.12.2(三)
  3. matlab循环中显示figure时窗口跳动
  4. 如何Dockerize您的端到端验收测试
  5. java 调用Spring接口上传文件及其他参数填充
  6. 九校联考-DL24凉心模拟Day2总结
  7. 20、Outer Apply 和 Cross Apply
  8. 1 matplotlib绘制折线图
  9. SSO实现机制
  10. git reset --hard HEAD^后显示more?的原因及如何解决