/**
 * author：AbuCoder
 * date:2022年5月22日13:53:23
 * 封装的异步请求处理函数
 * 需要在main.js引入与挂载 import request from 'utils/request.js'  //引入异步请求函数
 *  Vue.prototype.request = request.request //挂载到全局
 * 使用方法为:
 * request('接口名称',{key:value},'请求方式(默认为GET)')
 * .then(res=>{console.log(res)})
 */
import { getToken} from "./auth";
import { baseUrl,uploadUrl } from "./apiconfig";
// let baseUrl = 'https://ga.rasmall.cn/prod-api/wxapi/';
// let baseUrl = 'http://localhost:8083/wxapi/'
async function request(mehtod, params, type, callBack) {
  //创建一个名为request请求的方法函数
  if (!type) {
    type = 'GET';
  }
  let header = {
    //设置请求头信息
    // 'Authorization': getToken(),
    // 'Authorization': 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImQzYWQ1NjAyLTNlMTUtNGFhMi04NTg5LWRhNTExNWM4ZTExMSJ9.5CVV8Al17dM_9LcY-DKbOf9Px7E5iBC9o62ZPxl_jcDe3VZM4IZk4p0IMx4TNvQXF843b8jKEV8gv6nB2OCc2Q',
    // 'X-Requested-With': 'XMLHttpRequest',
    "Accept": "application/json",
    "Content-Type": "application/json; charset=UTF-8"
  };
  let http = {
    url: baseUrl + mehtod,
    data: params,
    method: type,
    header: header
  };
  let promise = new Promise((resolve, reject) => {
    uni.request(http).then(res => {
		console.log('接口数据',res)
      // let newdata = res[1].data; // if (newdata.code == 403) {
      let newdata = res; 
      if (!newdata) {
        //如果错误码为 -1 提示
        uni.showToast({
          title: '请求失败，请稍后重试',
          icon: 'none'
        });
      }
      resolve(res);
    }).catch(err => {
      reject(err);
      console.log(err);
    });
  });
  return promise;
}

async function requestJson(mehtod, params, type, headerParams, callBack) {
  //创建一个名为request请求的方法函数
  let header = {
    //设置请求头信息
    'X-Requested-With': 'XMLHttpRequest',
    "Accept": "application/json",
    "Content-Type": "application/json; charset=UTF-8"
  };
  if(headerParams){
	  header = Object.assign(header, headerParams);
  }
  let http = {
    url:  mehtod,
    data: params,
    method: type,
    header: header
  };
  let promise = new Promise((resolve, reject) => {
    uni.request(http).then(res => {
      resolve(res);
    }).catch(err => {
      reject(err);
      console.log(err);
    });
  });
  return promise;
}

async function requestForm(mehtod, params, type, callBack) {
  //创建一个名为request请求的方法函数
  let header = {
    //设置请求头信息
    'X-Requested-With': 'XMLHttpRequest',
    "Accept": "application/json",
    "Content-Type": "application/x-www-form-urlencoded"
  };
  let http = {
    url:  mehtod,
    data: params,
    method: type,
    header: header
  };
  let promise = new Promise((resolve, reject) => {
    uni.request(http).then(res => {
      resolve(res);
    }).catch(err => {
      reject(err);
      console.log(err);
    });
  });
  return promise;
}
export default {
  request,
  requestJson,
  requestForm
};