/*
 * @Description:
 * @version:
 * @Author: hy
 * @Date: 2020-04-27 09:22:03
 * @LastEditors: hanyu
 * @LastEditTime: 2020-08-28 22:15:38
 */

// 对Request请求进行统一配置
// import http from './request';
import jsConfig from '@/config/index.js';
import store from '@/store';
import http from 'uview-ui/libs/request';

// #ifdef  MP-WEIXIN
http.setConfig({
  baseUrl: process.env.VUE_APP_URL,
  loadingText: '正在加载'
});
// #endif

// #ifdef  H5
http.setConfig({
  // baseUrl: process.env.VUE_APP_TYPE == 'dev' ? '' : process.env.VUE_APP_URL,
  baseUrl: process.env.VUE_APP_URL,
  loadingText: '正在加载'
});
// #endif
// 请求拦截部分，如配置，每次请求前都会执行
http.interceptor.request = config => {
  if (config.url.indexOf('auth/mobile/token/sms') >= 0) {
    config.header['Authorization'] = jsConfig.noLoginAccessToken;
  } else if (config.url.indexOf('auth/oauth/token') >= 0) {
    config.header['Authorization'] = 'Basic dGVzdDp0ZXN0';
    config.header['TENANT-ID'] = '1';
  } else if (config.url.indexOf('dept/csm') >= 0) {
    config.header['responseType'] = 'arraybuffer';
  } else {
    //如果登录的话
    let accessToken = store.getters.accessToken;
    config.header['Authorization'] = `Bearer ${accessToken}`;
  }

  return config;
};

// 响应拦截，如配置，每次请求结束都会执行本方法

http.interceptor.response = res => {
  if (res.msg && !res.data) {
    if (res.msg.includes('任务已达上限')) {
      return res;
    }
    uni.showToast({
      title: res.msg,
      icon: 'none'
    });
    return res;
  } else {
    return res;
  }
};
export const postHttp = (url, data) => {
  return http.post(url, data);
};

export const getHttp = (url, data) => {
  return http.get(url, data);
};
