123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- import axios, {
- AxiosError,
- AxiosResponse,
- AxiosRequestHeaders,
- AxiosRequestConfig,
- CreateAxiosDefaults,
- InternalAxiosRequestConfig
- } from '@ohos/axios';
- import CommonConstants from '../../constants/CommonConstants';
- //import PreferencesUtil from '../PreferencesUtil';
- import { printError, printRequest, printResponse, handleRes } from './Helps';
- //const baseUrl = "http://192.168.137.5:8000/"
- // const baseUrl = "http://0.0.0.0:8000/"
- const baseUrl = "http://10.88.20.120:8000/"
- const DEBUG = true //
- // 创建实例
- const JGRequest = axios.create(
- {
- baseURL: baseUrl,
- 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;
|