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){
		  //--生成会员 --
		  getApp().promiseGet("/api/weshop/users/get/" + userinfo.store_id + "/" + userinfo.user_id,{}).then(res=>{
			  if(res.data.code==0){
				  //更新会员
				  userinfo= e.data.data;	  
				  a.globalData.userInfo=userinfo;
				  a.globalData.user_id=userinfo.user_id;
				  wx.setStorageSync("userinfo",userinfo);
				  
				  "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", {
		isShowLoading:0,
        data: {
          js_code: e,
          store_id: a.globalData.setting.stoid,
          nickname: a.clear_word(a.globalData.getu.nickName),
          head_pic: a.clear_word(a.globalData.getu.avatarUrl)          
        },
        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,
              nickname:a.globalData.getu.nickName,
            },
            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: '/packageE/pages/togoin/togoin',
                      })
                  }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;
	  if(!app_d.user_id) return false;  
      ab.get("/api/weshop/users/get/" + oo.stoid + "/" + app_d.user_id, {
		data:{r:Math.random()},	
        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, {
		  data:{r:Math.random()},				
          success: function (e) {
              app_d.userInfo = e.data.data;
              app_d.user_id = e.data.data.user_id;
              wx.setStorageSync("userinfo",app_d.userInfo);
             "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, {
		isShowLoading:0,
        success: function (e) {
            app_d.userInfo = e.data.data;
            app_d.user_id = e.data.data.user_id;
            wx.setStorageSync("userinfo",app_d.userInfo);
          "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){ 
			    n.goto("/packageE/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.navigateTo({
            url: "/pages/index/index/index"
        });
    },
    goGetUserInfo: function() {
        wx.navigateTo({
            url: "/packageE/pages/togoin/togoin"
        });
    }
};
