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:11010/" //chuangke //const baseUrl = "http://192.168.1.174:8079"//huajing const baseUrl = "http://192.168.1.3:8079"//huajing // const baseUrl = "http://192.168.1.4:8079/" //chuangke const DEBUG = true // // 创建实例 const WmsRequest = axios.create( { baseURL: baseUrl, headers: { 'Content-Type': 'application/json;charset=UTF-8', }, timeout: 60 * 1000, } ) // 添加请求拦截器 WmsRequest.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); }); // 添加响应拦截器 WmsRequest.interceptors.response.use((response: AxiosResponse) => { console.log("hhtest", "---------------------------") // 对响应数据做点什么 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(msg) } }, (error: AxiosError) => { // 对响应错误做点什么 printError(error) return Promise.reject(error); }); export default WmsRequest;