App({
  onLaunch: function () {
   
  },
  post: function (url, data, headerParams) {
    /**
     * 自定义post函数,返回Promise
     * +-------------------
     * @param {String}      url 接口网址
     * @param {arrayObject} data 要传的数组对象 like: {name: 'name', age: 32}
     * +-------------------
     * @return {Promise}    promise 返回promise供后续操作
     */
    // wx.showLoading({
    //   title: '加载中',
    // })
    wx.showNavigationBarLoading()
    wx.showLoading({
      title: '加载中',
    })
    var promise = new Promise((resolve, reject) => {
      //init
      let that = this;
      let postData = data;
     let baseUrl = 'http://anleguo.w.bronet.cn';
      // let baseUrl = 'http://192.168.1.15/public';
      // postData.signature = that.makeSign(postData);
      //网络请求
      let header = {
        'content-type': 'application/x-www-form-urlencoded'
      }
      header = Object.assign(header, headerParams)
      wx.request({
        url: baseUrl + url,
        data: postData,
        method: 'POST',
        header: header,
        success: function (res) {//返回取得的数据
          if (res.data.code == '20000') {
            resolve(res.data.data);
          } else if (res.data.code == '40000') {//返回错误提示信息
            
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          } else if (res.data.code == '40001') {//返回错误提示信息
            wx.showModal({
              title: '提示',
              content: '登录已过期请重新登录!',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  wx.removeStorageSync('token');
                  wx.navigateTo({
                    // url: '/pages/startRecord/startRecord',
                  })
                }
              }
            })
          } else if (res.data.code == '40002') {
            console.log(res)
            // wx.showModal({
            //     title: '提示',
            //     // content: res.data.msg,
            //     showCancel: false,
            //     success: function (res) { }
            // })
          } else if (res.data.code == '40003') {
            // 初始化用户信息判断
            let usetrInfoType = {
              hasUserInfo: false
            }
            resolve(usetrInfoType);
            // wx.showModal({
            //   title: '提示',
            //   content: res.data.msg,
            //   showCancel: false
            // })
          } else {
            console.log(res)
            // wx.showModal({
            //     title: '提示',
            //     // content: res.data.msg,
            //     showCancel: false
            // })
            reject(res.data)
          }
          setTimeout(function () {
            wx.hideLoading()
            wx.hideNavigationBarLoading()
          }, 600)
        },
       fail: function (e) {
          reject('网络出错');
          // wx.hideLoading()
          wx.hideNavigationBarLoading()
        }
      })
    });
    return promise;
  },

     /**
     * 自定义上传文件,返回文件路径
     * +-------------------
     * @filetype{String}      文件类型.image,video,audio,file
     * @filePath String 要传的文件路径 
     * +-------------------
     * @return url 返回文件路径
     */
 upload (filetype, file) {
  
  var promise = new Promise((resolve, reject) => {
   wx.showNavigationBarLoading()
   wx.showLoading({
    title: '上传中',
   })
   let url = 'http://anleguo.w.bronet.cn/portal/common/upload';
   let head = {
    'XX-Token': wx.getStorageSync('token'),
    'XX-Device-Type': ''
   }
   let typename = {
    filetype: filetype
   }
   wx.uploadFile({
    url: url, //仅为示例,非真实的接口地址
    filePath: file,
    name: 'file',
    header: {},
    formData: typename,
    success: function (res) {
     console.log('上传文件后',res)
     let temdata = JSON.parse(res.data);
     let urlobj = {
      url: temdata.data.preview_url
     }
     resolve(urlobj);
     wx.hideNavigationBarLoading();
     wx.hideLoading();
    },
    fail: function (res) {
     reject('网络出错');
     wx.hideNavigationBarLoading()
     wx.hideLoading()
    },
    complete: () => {
     wx.hideNavigationBarLoading()
     wx.hideLoading()
    },
   })
  });
  return promise;
 },
  formCollect(id) {
    let url = '/sign/formid'
    let params = {
      formid: id,
      openid: wx.getStorageSync('openId')
    }
    this.post(url, params).then((res) => {
      console.log(res)
    }).catch((errMsg) => {

    })
  },
  nowDate() {
    let date = new Date();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    let dayobj={};
   dayobj.newdate = date.getFullYear() + '年' + (month > 9 ? month : ('0' + month)) + '月' + (day > 9 ? day : ('0' + day)) + '日';
   dayobj.date = date.getFullYear() + '-' + (month > 9 ? month : ('0' + month)) + '-' + (day > 9 ? day : ('0' + day)) ;
   return dayobj
  },
  initDate(a) {
    let date1 = new Date();
    let time1 = date1.getFullYear() + "年" + (date1.getMonth() + 1) + "月" + date1.getDate() + '日';
    let date2 = new Date(date1);
    date2.setDate(date1.getDate() + a);
    let time2 = date2.getFullYear() + "年" + (date2.getMonth() + 1) + "月" + date2.getDate() + '日';
    return time2;
  },
  // 数组移除指定元素
  arryRemove(arr, item) {
    return arr.filter(function (ele) {
      return ele != item;
    })
  },
  strTime(strtime) {
    let str = 'bronet' + strtime + 'beijing'
    return md5(md5(str))
  },
  //数组变字符串,中间用,隔开
  array_to_string(arr){
   let str='';
   for (let i = 0; i < arr.length;i++){
    if (i == (arr.length-1)){
     str +=arr[i]
    }else{
     str += arr[i]+','
    }
   }
   return str;

  },
  globalData: {
    userInfo: null,
    baseUrl: 'http://beijing.w.bronet.cn'
  }
})