// app.js
var domain = "http://192.168.5.4:8088"
// domain = "http://192.168.2.90:8088"
// domain = "https://cansino-fxcapi.vandh.cn/prod-api"
// domain = "https://fxc.vandh.cn/prod-api"
// domain = "https://fxcapi.zhongyihz.cn/prod-api"
var serverDomain = domain + ""
var downloadDomain = "https://oss.zhongyihz.cn"
App({
  onLaunch: function () {
    // 获取系统状态栏信息
    wx.getSystemInfo({
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;
        let capsule = wx.getMenuButtonBoundingClientRect();
        if (capsule) {
          this.globalData.Custom = capsule;
          this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
        } else {
          this.globalData.CustomBar = e.statusBarHeight + 50;
        }
      }
    })
    if(!wx.getStorageSync('status')){
      wx.setStorageSync('status', JSON.stringify(this.globalData.status));
    }
    if(!wx.getStorageInfoSync('labelStatus')){
      wx.setStorageSync('status', JSON.stringify(this.globalData.labelStatus));
    }
  },
  requestP: function (options = {}) {
    var that = this;
    let {
      needToken = true,
      url,
      data,
      method,
      dataType,
      contentType,
      responseType,
      success,
      fail,
      complete
    } = options;
    let header = {
      // "Authorization": 'Bearer ' + wx.getStorageSync('token'),
      "Authorization": wx.getStorageSync('token'),
      'content-type': contentType || "application/x-www-form-urlencoded; charset=UTF-8", //"application/json"
    }
    if (!needToken) { //不允许带token的接口屏蔽token
      delete header.Authorization
    }
    return new Promise((res, rej) => {
      wx.request({
        url: that.globalData.$path + url,
        data: data, //JSON.stringify(data),
        header: header,
        method: method || "POST",
        contentType: contentType || "application/x-www-form-urlencoded; charset=UTF-8", //"application/json",
        dataType,
        responseType,
        success(r) {
          if (r.data.code == 200) {
            res(r);
          } else if (r.data.code == 401) {
            wx.redirectTo({
              url: '/pages/login/login',
            })
          } else if(r.data.code == 101) {
            that.showMsg(r.data.msg,'error');
          }else{
            rej(r);
          }
          wx.hideLoading()
        },
        fail(err) {
          console.log(err)
          // rej(err);
          wx.hideLoading()
          that.showMsg('请刷新重试','error');
        },
        complete
      });
    });
  },
  globalData: {
    app:"KXN",//用于切换不同app使用; KXN:康希诺
    $path: serverDomain,
    $filePath: downloadDomain,
    // appId: "wx92dc65d230789297",
    // secret: "d184c4c85ad2551924979e1de1f1e1c9",
    userInfo: null,
    share: false,
    title:'冷链追溯系统',
    tabarData:{},
    status:{
      checkedMap:true,
      statisticsInfo:true,
      statusLabelTest:true,
      statusLabelNum:true,
      statusLabelType:true,
      expressStatu:true,
      transportStatus:true,
      tempChart:true,
      onlineNum:true,
      packageStatu:true,
      onlineSituat:true,
      newSituat:true
    },
    labelStatus:{
      lableX101: true,
      lableX102: true,
      lableX103: true,
      lableX104: true,
      lableX105: true,
      lableX106: true,
      lableX107: true,
      lableX201: true,
      lableX202: true,
      lableX203: true,
    }
  },
  //上传文件
  wxUploadFile: function (options, ...params) {
    var that = this;
    options.url = that.globalData.$path + options.url //+ "?platSign=" + that.globalData.platSign
    options.header = {
      "Authorization" : 'Bearer ' +  wx.getStorageSync('token'),
    }
    return new Promise((resolve, reject) => {
      const extras = {
        success: resolve,
        fail: reject
      }
      wx.uploadFile({ ...options, ...extras }, ...params)
    })
  },
  showToptip: function (that, type, text, time = 2500) { //自定义toptip
    that.setData({
      CustomBar: this.globalData.CustomBar,
      tipType: {
        "error": "bg-red",
        "success": "bg-gradual-green",
        "worning": "bg-orange"
      } [type],
      tipText: text,
      tipStaus: "show"
    })
    setTimeout(function () {
      that.setData({
        tipStaus: null
      })
    }, time)
  },
  showMsg:function (title,type){
    wx.showToast({
      title: title,
      icon:type,
      duration: 1000,
      mask:true
    })
  }
})