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:20010"//huajing // const baseUrl = "http://192.168.1.4:8079/" //chuangke 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 //config.headers.Authorization ="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOnsic3RhdGlvbkNvZGUiOiJ6aHVhbmd0aWFvMSIsInN0YXRpb25UeXBlIjoiMTgiLCJkZXB0SWQiOjE3LCJpZCI6MTAwMDAsInN0YXRpb25JcCI6IjE5Mi4xNjguMS4xMSIsInVzZXJOYW1lIjoiYWRtaW4iLCJvcmdJZCI6MTcsInN0YXRpb25JZCI6Njh9LCJkZXZpY2UiOiJhaW8iLCJlZmYiOjE3NDgwNTQ4ODg5MzUsInJuU3RyIjoiMmJ5NnJlOVpMa2JZOE1CcjBHSlBENjlXZTNwSjgyREIifQ.nY0PPEjgjW2-PX1TqI_SGxa5Mu4dGOCwbGDitvN_oqA" //config.headers.Authorization ="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOnsiaWQiOjM3LCJsb2dpblR5cGUiOiJhaW8ifSwiZGV2aWNlIjoiYWlvIiwiZWZmIjoxNzE3NTExMjYwODI4LCJyblN0ciI6InRSenNBTGdlZ3lqS0FHeDZTSkdYZTNLbFY3eWh1OG1PIn0.FVAeESiz_PH1NtBFDmGZr0IwtXzubV2d8JTQdGdJnxc" 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(msg) } }, (error: AxiosError) => { // 对响应错误做点什么 printError(error) return Promise.reject(error); }); export default ProcessRequest;