import axios, { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from '@ohos/axios';

import { printError, printRequest, printResponse, handleRes } from './Helps';
import uploadInstance from './UploadUtil';


// 创建实例
const JGRequest = axios.create(
  {
    baseURL: uploadInstance.uploadParams.stationIP + ':8000', //"http://192.168.101.150:8000", //
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    },
    timeout: 60 * 1000,
  }
)


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

  // 以后登录之后可以在这里传
  //config.headers.Authorization = CommonConstants.AUTH_TOKEN
  //  config.headers.Authorization="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOnsiaWQiOjM3LCJsb2dpblR5cGUiOiJhaW8ifSwiZGV2aWNlIjoiYWlvIiwiZWZmIjoxNzE3NTExMjYwODI4LCJyblN0ciI6InRSenNBTGdlZ3lqS0FHeDZTSkdYZTNLbFY3eWh1OG1PIn0.FVAeESiz_PH1NtBFDmGZr0IwtXzubV2d8JTQdGdJnxc"
  printRequest(config)

  return config;
}, (error: AxiosError) => {
  // 对请求错误做些什么
  printError(error)
  return Promise.reject(error);
});
// 添加响应拦截器
JGRequest.interceptors.response.use((response: AxiosResponse) => {
  // 对响应数据做点什么
  printResponse(response)

  let res = handleRes(response)
  let success = res[0] as boolean
  let msg = res[1] as string
  // console.log('textTag'+"handleRes的返回结果 ", success, msg)

  if (success) {
    //  console.log('textTag'+"handleRes的返回结果 ", JSON.stringify(response.data))
    // return response.data.data;
    return response.data;
  }
  else {
    return Promise.reject<string>(msg)

  }

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

  return Promise.reject(error);
});


export default JGRequest;




