import Env from '@/util/Env.js'
import store from '../store'
const baseUrl = Env.getBaseURL();

const http = ({ url = '', param = {}, ...other } = {}) => {
  //wx.showLoading({
  //   title: '啊嘞，色彩正在绘制'
  //});
  let timeStart = Date.now();
  return new Promise((resolve, reject) => {
    wx.request({
      url: getUrl(url),
      data: param,
      header:{
        'content-type': 'application/json', // 默认值，另一种是"content-type" : "application/x-www-form-urlencoded"
		'token':store.getters.appUser.token
      },
      ...other,
      complete(res) {
        wx.hideLoading();
        if(res.statusCode >= 200 && res.statusCode < 300){
          resolve(res.data);
        }else{
          reject(res);
        }
      }
    })
  })
}

const getUrl = (url) => {
  if(url.indexOf('://') == -1){
    url = baseUrl + url;
  }
  return url;
}

const get = (url, param = {}) => {
  return http({
    url,
    param
  });
}

const post = (url, param = {}) => {
  return http({
    url,
    param,
    method: 'post'
  });
}

const put = (url, param = {}) => {
  return http({
    url,
    param,
    method: 'put'
  });
}

const del =  (url, param ={}) => {
  return http({
    url,
    param,
    method: 'delete'
  });
}

module.exports = {
  baseUrl,
  get,
  post,
  put,
  del
}
