//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    // var logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs)
    var openid = wx.getStorageSync('openid') || "";
    wx.setStorageSync('state', -1);
    // 登录
    if (!openid){
      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          // console.log(res)
          if (res.code) {
            console.log(res.code)
            wx.request({
              url: this.globalData.httpUrl+'openid',
              header: {
                code: res.code
              },
              success: e => {
                console.log(e)
                if (e.data.code == 200) {
                  this.globalData.openId = e.data.data.openid;
                  this.globalData.session_key = e.data.data.session_key;
                  this.globalData.token = e.data.data.token;
                  wx.setStorageSync('openid', e.data.data.openid)
                  wx.setStorageSync('session_key', e.data.data.session_key)
                  wx.setStorageSync('token', e.data.data.token)
                  this.getUserType();
                  if (this.globalData.openidSuccessFuc) {
                    this.globalData.openidSuccessFuc();
                  }
                }
              }
            })
          } else {
            console.log("登陆失败" + res.errMsg)
          }
        }
      })
    }else{
      this.globalData.openId = openid;
      this.getUserType();
    }
  },
  // onShow:function(){
  //   this.getUserType();
  // },
  getUserType: function () {//获取用户注册状态
    console.log(this.globalData.openId)
    this.wxRequest(this.globalData.httpUrl + 'getinfo', {},e=>{
      console.log(e)
      if(e.code==201){//未注册跳到注册页面
        wx.setStorageSync('state', 0);
        wx.setStorageSync("noUser", 0)
      } else if (e.code == 202) {//已注册
        wx.setStorageSync('state', 1);
        this.globalData.userInfo = e.data;
        e.data.avatarUrl = e.data.headimgurl;
        e.data.nickName = e.data.nickname;
        wx.setStorageSync('user_phone', e.data.user_phone);
        wx.setStorageSync("userInfo", e.data)
        if (e.data.item_state == 1) {
          wx.setStorageSync("noUser", 0)
        } else {
          wx.setStorageSync("noUser", 1)
        }
      }
      if(this.globalData.userInfoBackFn){
        this.globalData.userInfoBackFn(wx.getStorageSync('state'));
      }
    },this)
  },

  globalData: {
    openidSuccessFuc: null,//方法回调
    userInfoSuccessFuc: null,//方法回调
    present_id:"",//判断用户是通过领取优惠券进来的还是直接进的
    number:"",//赠送优惠券的数量
    openId:"",
    session_key:"",
    token:"",
    userInfo:"",
    httpUrl:"https://laomenkuang.jiyou-tech.com/apiWx/",
    user_phone:"",//用户手机号
  },
  wxRequest: function (url, params, callback, thisArg, methods, openid) {
    let that = this;
    var httpUrl = url;
    var str = "";
    var count = 0;
    for (let key in params) {
      if (count) {
        str += "&" + key + "=" + params[key];
      } else {
        str += key + "=" + params[key];
      }
      count++;
    }
    if (str) {
      httpUrl += "?" + str;
    }
    if (!methods) {
      methods = "GET";
    }
    if (methods == "POST") {
      wx.request({
        url: url,
        data: params,
        method: methods,
        header: {
          "content-type": "application/x-www-form-urlencoded",
          "openid": this.globalData.openId
        },
        success: function (re) {
          // if (re.data.code == -1002) {
          //   wx.removeStorageSync('openid');
          //   that.wxLogin();
          //   // that.wxRequest(url, params, callback, thisArg, methods, openid);
          // }
          if (callback && thisArg) {
            callback.call(thisArg, re.data);
          }
        },
        fail: function (re) {
          wx.hideLoading();
        }
      })
    } else {
      wx.request({
        url: httpUrl,
        method: methods,
        header: {
          "openid": this.globalData.openId,
        },
        success: function (re) {
          // if (re.data.code == -1002) {
          //   wx.removeStorageSync('openid');
          //   that.wxLogin();
          //   // that.wxRequest(url, params, callback, thisArg, methods, openid);
          // }
          if (callback && thisArg) {
            callback.call(thisArg, re.data);
          }
        },
        fail: function (res) {
          wx.hideLoading();
        }
      })
    }
  },
  // requestGet:function(url,data,callback){//get接口调用
  //   wx.request({
  //     url: url, 
  //     header: {
  //         "openid": this.globalData.openId
  //     },
  //     data: data,
  //     success:res=>{
  //       if (callback) {
          
  //       }
  //     }
      
      
  //   })
  // },
  // requestPost: function (url, data) {//post接口调用
  //   wx.request({
  //     url: url,
  //     header: {
  //       "Content-Type":"application/x-www-form-urlencoded",
  //       openid: this.globalData.openId
  //     },
  //     data: data,
  //     success(res) {
  //       console.log(res.data)
  //     }
  //   })
  // }
})