import {
  base
} from "../config/index"
export const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

export const ajax = data => {
  return new Promise((resolve, reject) => {
    const t = setTimeout(() => {
      clearTimeout(t);
      wx.showLoading({
        title: '加载中...',
        mask: true
      })
    }, 200);
    wx.request({
      url: base[data.urlType || 'apiurl'] + (data.api || ''),
      data: data.data,
      header: data.header,
      timeout: data.timeout || 60000,
      method: (data.method || 'get').toUpperCase(),
      responseType: data.responseType || 'text',
      enableCache: data.enableCache || false,
      success(res) {
        if (res.statusCode !== 200) {
          wx.showToast({
            title: '请求失败',
            icon: 'none'
          })
          return
        }
        resolve(res.data)
      },
      fail(err) {
        reject(err)
      },
      complete() {
        t && clearTimeout(t);
        wx.hideLoading();
      }
    })
  })
}

export const ele_height = (e, rWidth) => {
  const {
    detail: {
      width,
      height
    }
  } = e;
  const h = rWidth / width * height;
  return h
}