ProcessRequest.ets 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import axios, {
  2. AxiosError,
  3. AxiosResponse,
  4. AxiosRequestHeaders,
  5. AxiosRequestConfig,
  6. CreateAxiosDefaults,
  7. InternalAxiosRequestConfig
  8. } from '@ohos/axios';
  9. import CommonConstants from '../../constants/CommonConstants';
  10. import { printError, printRequest, printResponse, handleRes } from './Helps';
  11. // jiaxiaoqiang:这里要改
  12. //const baseUrl = "http://192.168.1.3:11010/" //chuangke
  13. //const baseUrl = "http://192.168.1.174:8079"//huajing
  14. const baseUrl = "http://192.168.1.3:20010"//huajing
  15. // const baseUrl = "http://192.168.1.4:8079/" //chuangke
  16. const DEBUG = true //
  17. // 创建实例
  18. const ProcessRequest = axios.create(
  19. {
  20. baseURL: baseUrl,
  21. headers: {
  22. 'Content-Type': 'application/json;charset=UTF-8',
  23. },
  24. timeout: 60 * 1000,
  25. }
  26. )
  27. // 添加请求拦截器
  28. ProcessRequest.interceptors.request.use((config: InternalAxiosRequestConfig) => {
  29. // 以后登录之后可以在这里传
  30. config.headers.Authorization = CommonConstants.AUTH_TOKEN
  31. //config.headers.Authorization ="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOnsic3RhdGlvbkNvZGUiOiJ6aHVhbmd0aWFvMSIsInN0YXRpb25UeXBlIjoiMTgiLCJkZXB0SWQiOjE3LCJpZCI6MTAwMDAsInN0YXRpb25JcCI6IjE5Mi4xNjguMS4xMSIsInVzZXJOYW1lIjoiYWRtaW4iLCJvcmdJZCI6MTcsInN0YXRpb25JZCI6Njh9LCJkZXZpY2UiOiJhaW8iLCJlZmYiOjE3NDgwNTQ4ODg5MzUsInJuU3RyIjoiMmJ5NnJlOVpMa2JZOE1CcjBHSlBENjlXZTNwSjgyREIifQ.nY0PPEjgjW2-PX1TqI_SGxa5Mu4dGOCwbGDitvN_oqA"
  32. //config.headers.Authorization ="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOnsiaWQiOjM3LCJsb2dpblR5cGUiOiJhaW8ifSwiZGV2aWNlIjoiYWlvIiwiZWZmIjoxNzE3NTExMjYwODI4LCJyblN0ciI6InRSenNBTGdlZ3lqS0FHeDZTSkdYZTNLbFY3eWh1OG1PIn0.FVAeESiz_PH1NtBFDmGZr0IwtXzubV2d8JTQdGdJnxc"
  33. printRequest(config)
  34. return config;
  35. }, (error: AxiosError) => {
  36. // 对请求错误做些什么
  37. printError(error)
  38. return Promise.reject(error);
  39. });
  40. // 添加响应拦截器
  41. ProcessRequest.interceptors.response.use((response: AxiosResponse) => {
  42. // 对响应数据做点什么
  43. printResponse(response)
  44. let res = handleRes(response)
  45. let success = res[0] as boolean
  46. let msg = res[1] as string
  47. console.debug("handleRes的返回结果 ", success, msg)
  48. if (success) {
  49. return response.data.data;
  50. }
  51. else {
  52. return Promise.reject<string>(msg)
  53. }
  54. }, (error: AxiosError) => {
  55. // 对响应错误做点什么
  56. printError(error)
  57. return Promise.reject(error);
  58. });
  59. export default ProcessRequest;