auth.js 9.97 KB
var t = require("./common.js");
module.exports = {
    app: function() {
        return getApp();
    },
    auth: function(t) {
        var e = this.app(), o = this;
        wx.checkSession({
            success: function() {
                !e.globalData.wechatUser && o.wxLogin(t);
            },
            fail: function() {
                o.wxLogin(t);
            }
        });
    },
    isAuth: function() {
        return !!this.app().globalData.wechatUser;
    },
    clearAuth: function() {
        this.app().globalData.wechatUser = null, wx.setStorageSync("isAuth", !1);
    },
    hadAuth: function() {
        try {
            return !!wx.getStorageSync("isAuth");
        } catch (t) {
            return wx.setStorageSync("isAuth", !1), !1;
        }
    },


    login_fir: function (e, o, n) {

      var a = this.app(), app = a, i = this;

      var userinfo=   wx.getStorageSync("userinfo");
      if(userinfo){
          a.globalData.userInfo=userinfo;
          a.globalData.user_id=userinfo.user_id;
          "function" == typeof n &&  n(a.globalData.userInfo, a.globalData.wechatUser);
          return true;
      }


      if (void 0 === e || "" == e) return a.globalData.wechatUser = null, i.alertLoginErrorAndGoHome("登录码为空,请重新尝试"),
        !1;
      var r = o.userInfo, s = a.globalData.setting.versionCode;
      console.log("thirdLogin");
      console.log(e);
      a.request.get("/api/weshop/users/openidandkey", {
        data: {
          js_code: e,
          store_id: a.globalData.setting.stoid,
        },
        success: function (e) {
          console.log("openidandkey");
          console.log(e);
          if (e.data.code == 0) {
              if (e.data.data.user_id != undefined) {
                app.globalData.user_id = e.data.data.user_id;
                app.globalData.openid = e.data.data.weapp_openid;
                app.globalData.sessionKey = e.data.data.sessionKey;

                wx.setStorageSync("userinfo",e.data.data);
                wx.setStorageSync("isAuth", !0), a.globalData.userInfo = e.data.data, a.globalData.userInfo.head_pic = t.getFullUrl(a.globalData.userInfo.head_pic);
                "function" == typeof n && n(a.globalData.userInfo, a.globalData.wechatUser);
              }else{
                 app.globalData.sessionKey = e.data.data.sessionKey;
                 app.globalData.openid = e.data.data.openid;
                 app.globalData.getu = r;

                "function" == typeof n && n();
                 return false;
              }
          } else {
             return app.showWarning("登入失败!"+e.data.msg);
          }
        }
      });
    },

    login: function(e, o, n) {
        var a = this.app(),app=a, i = this;
        if (void 0 === e || "" == e) return a.globalData.wechatUser = null, i.alertLoginErrorAndGoHome("登录码为空,请重新尝试"),
        !1;
        var r = o.userInfo, s = a.globalData.setting.versionCode;
        console.log("thirdLogin");
        console.log(e);
         a.request.get("/api/weshop/users/openidandkey", {
            data: {
              js_code: e,
              store_id: a.globalData.setting.stoid,
            },
            success: function (e) {
                console.log("openidandkey");
                console.log(e);
                if(e.data.code==0){
                  if (e.data.data.user_id==undefined){
                      app.globalData.sessionKey = e.data.data.sessionKey;
                      app.globalData.openid = e.data.data.openid;

                      app.globalData.getu = r;
                      wx.navigateTo({
                         url: '/pages/getphone/getphone',
                      })
                  }else{
                    app.globalData.user_id = e.data.data.user_id;
                    app.globalData.openid = e.data.data.weapp_openid;
                    app.globalData.sessionKey = e.data.data.sessionKey;

                    wx.setStorageSync("userinfo",e.data.data);
                    wx.setStorageSync("isAuth", !0), a.globalData.userInfo = e.data.data, a.globalData.userInfo.head_pic = t.getFullUrl(a.globalData.userInfo.head_pic);
                    "function" == typeof n && n(a.globalData.userInfo, a.globalData.wechatUser);

                  }
                }else{
                  return app.showWarning("登入失败!"+e.data.msg);
                }
            }
        });
    },

    //-------获取用户信息----
    get_u(t){
      var app_d = this.app().globalData, oo = app_d.setting;
      var ab = this.app().request;
      ab.get("/api/weshop/users/get/" + oo.stoid + "/" + app_d.user_id, {
        success: function (e) {
          app_d.userInfo = e.data.data;
          "function" == typeof t && t(e.data.data);
        },
      });
    },
    //-------微信登录--------
    wxLogin: function(t) {
      var app_d = this.app().globalData, oo = app_d.setting;
      if (app_d.user_id != "" && app_d.user_id != null  ){
        var ab = this.app().request;
        ab.get("/api/weshop/users/get/" + oo.stoid+"/"+app_d.user_id, {
          success: function (e) {
            app_d.userInfo = e.data.data;
            "function" == typeof t && t(e.data.data);
          },
        });
      }else{
        var e = this;
        wx.login({
            success: function(o) {
                console.log('wxLogin');
                console.log(o);
                o.code ? e.doGetWxUser(o.code, t) : wx.showModal({
                    title: "获取用户登录态失败",
                    content: o.errMsg,
                    showCancel: !1,
                    complete: function() {
                        e.goHome();
                    }
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
      }
    },

  wxLogin_fir: function (t) {
    var app_d = this.app().globalData, oo = app_d.setting;
    if (app_d.user_id != "" && app_d.user_id != null) {
      var ab = this.app().request;
      ab.get("/api/weshop/users/get/" + oo.stoid + "/" + app_d.user_id, {
        success: function (e) {
          app_d.userInfo = e.data.data;
          "function" == typeof t && t(e.data.data);
        },
      });
    } else {
      var e = this;
      wx.login({
        success: function (o) {
          console.log('wxLogin');
          console.log(o);
          o.code ? e.doGetWxUser(o.code, t,1) : wx.showModal({
            title: "获取用户登录态失败",
            content: o.errMsg,
            showCancel: !1,
            complete: function () {
                e.goHome();
            }
          });
        },
        fail: function (t) {
          console.log(t);
        }
      });
    }
  },


    doGetWxUser: function(t, e,ind) {
        var o = this, n = o.app();
        n.globalData.code = t;
        try {
          //获取本地存储
          var a = wx.getStorageSync("wx_user_info");
          var userInfo = a.userInfo;
          if (userInfo==undefined){ //判断本地是数据是否正确
                    wx.getUserInfo({
                        success: function (res)
                        {
                          console.log('doGetWxUser');
                          console.log(res);
                          console.log(ind);
                            //组装用户
                            var ar = {'nickName':res.userInfo.nickName,
                              'avatarUrl':res.userInfo.avatarUrl,
                              'gender': res.userInfo.gender};
                               //----会员记录到全局------
                               n.globalData.getu = ar;
                                var uu = {'userInfo': ar};
                                a=uu;

                                if(ind==1){
                                   a && void 0 != a ? (n.globalData.wechatUser = a, o.login_fir(t, a, e)) : o.goGetUserInfo();
                                }else{
                                    a && void 0 != a ? (n.globalData.wechatUser = a, o.login(t, a, e)) : o.goGetUserInfo();
                                }

                        },

                      fail: function () {
                        //---获取用户信息失败后。请跳转授权页面--
                        wx.showModal({
                          title: '警告',
                          content: '尚未进行授权,请点击确定跳转到授权页面进行授权。',
                          success: function (res) {
                            if (res.confirm) {
                              console.log('用户点击确定')
                              wx.navigateTo({
                                url: '/pages/togoin/togoin',
                              })
                            }
                          }
                        })
                      }

                    })
            }else{
              a && void 0 != a ? (n.globalData.wechatUser = a, o.login(t, a, e)) : o.goGetUserInfo();
            }

        } catch (t) {
            o.goGetUserInfo();
        }
    },

    failGetWxUser: function(t, e) {
        var o = this;
        wx.showModal({
            title: "请先授权登录哦",
            success: function(t) {
                t.confirm ? o.goGetUserInfo() : t.cancel && o.alertNoAuthAndGoHome();
            },
            fail: function(t) {
                o.goHome();
            }
        });
    },

    //一些提示的方法
    alertNoAuthAndGoHome: function() {
        var t = this;
        this.app().showWarning("你尚未授权登录", function() {
            t.goHome();
        }, null, !0);
    },
    alertLoginErrorAndGoHome: function(t) {
        "string" == typeof t && "" != t || (t = "登录时发生错误");
        var e = this;
        this.app().showWarning(t, function() {
            e.goHome();
        }, null, !0);
    },
    goHome: function() {
        wx.switchTab({
            url: "/pages/index/index/index"
        });
    },
    goGetUserInfo: function() {
        wx.navigateTo({
            url: "/pages/user/get_user_info/get_user_info"
        });
    }
};