App({
  APPID: 'wxd9f93ef41a607dd5',
  // 本地测试时不用加/api
  DOMAIN_NAME: 'http://192.168.0.11:9227', //接口域名
  IMG_NAME: 'http://192.168.0.11:9227',
  // DOMAIN_NAME: 'https://demo.metasoft.vip/api',
//   IMG_NAME: 'https://demo.metasoft.vip/api',
  globals: {
    refreshMyPages: false,
    homedata: {},
    editPro: {
      projectId: 0, //项目ID
      isedit: 0, //是否编辑
      json: {} //项目编辑后的详细数据
    },
    trainBeginCity: ''
  },

  onLaunch(res) {
    var that = this
  },
  ///////////////////////////////////////////////////////////////////////////
  //获取当前用户的openid
  Getopenid: function () {
    var token = wx.getStorageSync('token');
    return token;
  },

  /**
   * 对象转URL
   */
  urlEncode(data) {
    var _result = [];
    for (var key in data) {
      var value = data[key];
      if (value.constructor == Array) {
        value.forEach(_value => {
          _result.push(key + "=" + _value);
        });
      } else {
        _result.push(key + '=' + value);
      }
    }
    return _result.join('&');
  },

  /**
   * 显示失败提示框
   */
  showError(msg, callback) {
    wx.showModal({
      title: '友情提示',
      content: msg,
      showCancel: false,
      success(res) {
        // callback && (setTimeout(() => {
        //   callback();
        // }, 1500));
        callback && callback();
      }
    });
  },

  /**
   * 显示成功提示框
   */
  showSuccess(msg, callback) {
    wx.showToast({
      title: msg,
      icon: 'success',
      success() {
        callback && (setTimeout(() => {
          callback();
        }, 1500));
      }
    });
  },

  // 根据出生日期计算年龄周岁 传参格式为1996-06-08
  getAge: function (strBirthday) {
    var returnAge = '';
    var mouthAge = '';
    var strBirthdayArr = strBirthday.split("-");
    var birthYear = strBirthdayArr[0];
    var birthMonth = strBirthdayArr[1];
    var birthDay = strBirthdayArr[2];
    var d = new Date();
    var nowYear = d.getFullYear();
    var nowMonth = d.getMonth() + 1;
    var nowDay = d.getDate();
    if (nowYear == birthYear) {
      // returnAge = 0; //同年 则为0岁
      var monthDiff = nowMonth - birthMonth; //月之差 
      if (monthDiff < 0) {} else {
        mouthAge = monthDiff + '个月';
      }
    } else {
      var ageDiff = nowYear - birthYear; //年之差
      if (ageDiff > 0) {
        if (nowMonth == birthMonth) {
          var dayDiff = nowDay - birthDay; //日之差 
          if (dayDiff < 0) {
            returnAge = ageDiff - 1 + '岁';
          } else {
            returnAge = ageDiff + '岁';
          }
        } else {
          var monthDiff = nowMonth - birthMonth; //月之差 
          if (monthDiff < 0) {
            returnAge = ageDiff - 1 + '岁';
          } else {
            mouthAge = monthDiff + '个月';
            returnAge = ageDiff + '岁';
          }
        }
      } else {
        returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天
      }
    }
    return returnAge + mouthAge; //返回周岁年龄+月份
  },

  Upload: function (tempFilePaths, call) {
    wx.showLoading({
      mask: true,
      title: '正在上传'
    });
    var that = this
    wx.uploadFile({
      url: that.DOMAIN_NAME + '/dfs/upload',
      header: {
        'Authorization': 'Bearer ' + that.Getopenid()
      },
      filePath: tempFilePaths,
      name: 'file',
      success(res) {
        var deta = JSON.parse(res.data)
        if (call) call(deta);
        if (deta.code == 401) {
          wx.hideLoading()
          wx.showModal({
            confirmText: '好的',
            content: deta.msg || '身份已过期,需重登录',
            success: res => {
              wx.reLaunch({
                url: '/pages/login/login',
              })
            }
          });
        } else if (deta.code != 0) {
          wx.hideLoading()
          wx.showModal({
            confirmText: '好的',
            content: '服务器开小差去了，请重试',
            showCancel: false
          })
        } else if (deta.code == 0) {
          wx.hideLoading()
        }
      },
      fail: function (ret) {
        wx.hideLoading()
        wx.showModal({
          confirmText: '再试一次',
          content: '无法连接网络',
          showCancel: false,
          success: function () {
            if (res.confirm) {
              that.Upload(tempFilePaths, call);
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    })
  },

  getParkid() {
    var token = wx.getStorageSync('token');
    return token
  },

  getlogin() {
    wx.showModal({
      confirmText: '好的',
      content: '请先登录后再点击查看',
      success: res => {
        wx.reLaunch({
          url: '/pages/login/login',
        })
      }
    })
  },


  //统一请求封装
  //mandate 接口值
  //data 传值，已自动转换为JSON
  //call 请求成功后回调的方法
  AjaxRequest: function (methods, header, url, data, call) {
    var token = wx.getStorageSync('token');
    if (wx.getStorageSync('parkId')) {
      data.parkId = wx.getStorageSync('parkId')
    }
    wx.showLoading({
      mask: true,
      title: '正在加载'
    })
    var that = this
    //发送请求
    wx.request({
      header: header,
      method: methods,
      url: that.DOMAIN_NAME + url,
      data: data,
      success: function (ret) {
        if (call) call(ret.data);
        if (ret.data.code == 402 || ret.data.code == 401) {
          wx.hideLoading()
          wx.showModal({
            confirmText: '确认',
            content: '身份已过期,需重登录',
            success(res) {
              wx.removeStorageSync('MemberInfo')
              wx.removeStorageSync('token')
              if (res.confirm) {
                wx.reLaunch({
                  url: '/pages/login/login',
                })
              }
            }
          })
        } else if (ret.data.code != 0) {
          wx.hideLoading()
          wx.showModal({
            confirmText: '好的',
            content: ret.data.msg || '服务器开小差去了，请重试',
            showCancel: false,
            success(res) {
              if (res.confirm) {
                if (!wx.getStorageSync('parkId')) {
                  if (ret.data.msg == '请选择园区！!') {
                    wx.reLaunch({
                      url: '../index/parkList/parkList'
                    })
                  }
                }
              }
            }
          })
        } else if (ret.data.code == 0) {
          wx.hideLoading()
        }
      },
      fail: function (ret) {
        wx.hideLoading();
        wx.showModal({
          confirmText: '再试一次',
          content: '无法连接网络',
          success(res) {
            if (res.confirm) {
              that.AjaxRequest(methods, header, url, data, call);
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    })
  },

  ToWebURL(url){
    wx.navigateTo({
      url: pages/outer/outer,//要跳转的页面（需要新建立一个page页面）
      success: () => {

      }
    })
  },
})