const appId = 'wx04262fa98a7de5b6';

App({
  onLaunch: function() {
    var that = this;
    console.log("onlaunch---", "onlaunch");
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    wx.login({
      success: function(res) {
        if (res.code) {
          var requestdata = {
            "grant_type": "wx_app_code",
            "code": res.code,
            "scope": "global_access:end_user,tenant:" + appId
          };
          that.getTokenByCode(requestdata, true, "")
        }
      }
    })
  },

  onShow: function(res) {
    console.log("onShow----", res);
    var that = this;
    that.globalData.onshow_count++;
    if (that.globalData.onshow_count > 1) { //除第一次进入小程序之外的打开时
      if (res.path == "pages/main/smartchat/chat") { //聊天页面锁屏再打开,走该逻辑
        var requestdata = {
          "grant_type": "refresh_token",
          "refresh_token": that.globalData.refresh_token,
          "scope": "global_access:end_user,tenant:" + that.globalData.com_id
        };
        that.getTokenByCode(requestdata, false, "")
      } else {
        wx.login({
          success: function(res) {
            if (res.code) {
              var requestdata = {
                "grant_type": "wx_app_code",
                "code": res.code,
                "scope": "global_access:end_user,tenant:" + appId
              };
              that.getTokenByCode(requestdata, false, "chat")
            }
          }
        })
      }
    }
  },

  getTokenByCode: function(requestdata, relaunch, str) {
    var that = this;
    var baseUrl = that.globalData.baseUrl;
    wx.request({
      url: baseUrl + 'uaa/v1/auth/tokens',
      data: requestdata,
      method: "POST",
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        console.log("getTokenByCode", res.data)
        var user = res.data.user;
        var tenant = res.data.tenant;
        if (tenant) {
          that.globalData.tenant_id = tenant.id;
        }
        that.globalData.userInfo = res.data.user;
        that.globalData.wx_open_id = user.wx_open_id;
        that.globalData.Authorization = "Bearer " + res.data.access_token;
        that.globalData.refresh_token = res.data.refresh_token;
        if (user && user.type == "0") { //匿名用户
          wx.redirectTo({
            url: '../../getPhone/getPhone'
          })
        } else if (user && user.type == "1") { //注册用户
          that.configOssUrl()
          if (relaunch) {
            wx.reLaunch({
              url: '../guide/guide'
            })
          }
        }
      },
      fail: function(res) {
        console.log("login-res-fail", res);
      },
      complete: function(res) {
        
      }
    })
  },

  getUserInfo: function(cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.getUserInfo({
        withCredentials: false,
        success: function(res) {
          console.log("userInfo---", res)
          that.globalData.userInfo = res.userInfo
          typeof cb == "function" && cb(that.globalData.userInfo)
        }
      })
    }
  },

  configOssUrl: function() {
    wx.showLoading()
    var that = this
    wx.request({
      url: this.globalData.baseUrl + "filemeta/v1/config",
      header: {
        'Authorization': this.globalData.Authorization
      },
      method: 'GET',
      success: function(result) {
        console.log('获取OSSURL 成功', result.data)
        that.globalData.OSSUrl = result.data.app_url;
      },
      fail: function(res) {
        console.log('获取OSSURL 失败', res)
        wx.showToast({
          icon: "none",
          title: "云存储功能失效,请联系开发人员",
        })
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  globalData: {
    appId: appId,
    com_id: "",
    onshow_count: 0,
    // baseUrl: "http://47.99.47.16:20000/",
    // baseUrl: "http://47.96.75.229:20000/",
    // baseUrl: "http://192.144.144.220:20000/",
    // baseUrl: "http://154.8.229.55:20000/",
    // baseUrl: "http://47.110.250.177:20000/",
    // baseUrl: "http://47.110.158.110:20000/",
    baseUrl: "https://api.fjhxrl.com/",
    OSSUrl: "", //"https://oss.workai.com.cn/",
    userInfo: null,
    hostInfo: null,
    hasLogin: false,
    openid: null,
    Authorization: null,
    refresh_token: "",
    tenant_id: "",
    wx_open_id: "",
    newComerOrder: null
  }
})