import axios, {
  AxiosError,
  AxiosResponse,
  AxiosRequestHeaders,
  AxiosRequestConfig,
  CreateAxiosDefaults,
  InternalAxiosRequestConfig
} from '@ohos/axios';
import CommonConstants from '../../constants/CommonConstants';
import { printError, printRequest, printResponse, handleRes } from './Helps';


// jiaxiaoqiang：这里要改
 const baseUrl = "http://192.168.1.3:30010"//huajing
// const baseUrl = "http://192.168.1.246:30010" // huhao本地
const DEBUG = true //

// 创建实例
const ProcessRequest = axios.create(

  {
    baseURL: baseUrl,
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
    },
    timeout: 60 * 1000,
  }
)

// 添加请求拦截器
ProcessRequest.interceptors.request.use((config: InternalAxiosRequestConfig) => {

  // 以后登录之后可以在这里传
  config.headers.Authorization = CommonConstants.AUTH_TOKEN
  printRequest(config)
  return config;
}, (error: AxiosError) => {
  // 对请求错误做些什么
  printError(error)
  return Promise.reject(error);
});


// 添加响应拦截器
ProcessRequest.interceptors.response.use((response: AxiosResponse) => {
  // 对响应数据做点什么
  printResponse(response)
  let res = handleRes(response)
  let success = res[0] as boolean
  let msg = res[1] as string
  console.debug("handleRes的返回结果 ", success, msg)

  if (success) {
    return response.data.data;
  }
  else {
    return Promise.reject<string>(msg)
  }

}, (error: AxiosError) => {
  // 对响应错误做点什么
  printError(error)
  return Promise.reject(error);
});

export default ProcessRequest;




