小程序app.js,注册登录逻辑

app.js

//app.js
import Storage from "./common/auth/Storage";
import request from "./common/request";
App({
  onLaunch: function () {
    // 一进入就执行的方法
    this.checkLogin();
    getCartCount();
  },
  checkLogin: function () {
    const that = this;
    const storage = Storage.get("userData");
    if (!storage) {
      return that.loginRegister();
    }
    wx.checkSession({
      success: () => {
        that.globalData.openid = storage.openid;
        that.globalData.session_key = storage.session_key;
        that.globalData.uid = storage.uid;
        that.globalData.ruid = storage.ruid;
        return;
      },
      fail: () => {
        return that.loginRegister();
      }
    });
  },

  //注册判断
  loginRegister: function () {
    Storage.clear(); // 执行登录操作移除垃圾数据
    const that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          that.code = res.code;
        }
        request("getOpenId", {
          appid: that.globalData.app_id,
          secret: that.globalData.app_secret,
          js_code: res.code,
          grant_type: "authorization_code"
        }).then(({ data }) => {
          that.globalData.openid = data.openid;
          that.globalData.session_key = data.session_key;
          that.globalData.uid = data.uid;
          that.globalData.ruid = data.ruid;
          return Storage.set(data, "userData");
        });
      },
      fail: function () {
        console.log("网络错误");
      }
    });
  },

  globalData: {
    app_id: "",
    app_secret: "",
    openid: "",
    uid: "",
    ruid: "", // 已绑定的推荐人uid
    share_uid: "", // 分享过来的uid
    base_color: "#DBB975", //e93323  e93323  e93323
    base_color2: "#DBB975",
    wo_title: ""
  }
});

判断是否登录,没有登录则进行注册登录操作。
登录成功之后,将openid,uid等信息,存储到Storage和globalData变量中。

posted @ 2020-05-13 15:27  TBHacker  阅读(628)  评论(0编辑  收藏  举报