Fetchx.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. |--------------------------------------------------
  3. | Fetchx
  4. | The available instance methods are listed below. The specified config will be merged with the instance config.
  5. Methods:
  6. axios#request(config)
  7. axios#get(url[, config])
  8. axios#delete(url[, config])
  9. axios#head(url[, config])
  10. axios#options(url[, config])
  11. axios#post(url[, data[, config]])
  12. axios#put(url[, data[, config]])
  13. axios#patch(url[, data[, config]])
  14. |--------------------------------------------------
  15. */
  16. import axios from 'axios'
  17. import API from './Restful'
  18. import Toast from 'react-native-root-toast'
  19. // "http://123.103.9.212:18095/"; //测试环境外网
  20. // "http://172.20.11.88/"; //测试环境
  21. // "http://192.168.199.112/occ-b2b-buyer/";//bingxv
  22. // "http://192.168.199.177:8769/"; //gaopeng
  23. // "http://192.168.199.225:8080/"; //shuifeng
  24. // "http://172.20.15.52/"; //开发环境
  25. // "http://10.11.112.37/:8769"; //shuifeng开发环境
  26. //
  27. //init
  28. let Fetchx,
  29. LoginFetchx,
  30. MyHeaders,
  31. BaseUrl,
  32. ImageBaseUrl,
  33. ApiPrefix = '/api'
  34. let Token, toast
  35. export const setBaseUrl = data => {
  36. // let LoginUrl = "http://172.20.11.88:80";
  37. BaseUrl = 'http://' + data.url + ':' + data.port
  38. ImageBaseUrl = 'http://' + data.url + ':' + data.port
  39. Fetchx = createFetchx(BaseUrl, 30000)
  40. LoginFetchx = createFetchx(BaseUrl, 60000)
  41. // LoginFetchx = createFetchx(LoginUrl, 4000);
  42. }
  43. //factory fetch fn
  44. const createFetchx = (baseURL, timeout) => {
  45. MyHeaders = new Headers()
  46. _Fetchx = axios.create({
  47. baseURL: baseURL,
  48. timeout: timeout,
  49. headers: MyHeaders,
  50. })
  51. //Fetchx.interceptors
  52. // Add a request interceptor global
  53. _Fetchx.interceptors.request.use(
  54. config => {
  55. return config
  56. },
  57. error => {
  58. // Do something with request error
  59. return Promise.reject(error)
  60. }
  61. )
  62. // Add a response interceptor
  63. _Fetchx.interceptors.response.use(
  64. function(response) {
  65. // Do something with response data
  66. return response
  67. },
  68. function(error) {
  69. //timeout handler
  70. if (error.code === 'ECONNABORTED') {
  71. if (toast) {
  72. Toast.hide(toast)
  73. }
  74. toast = Toast.show(
  75. '登陆超时,请尝试重新登陆',
  76. { position: toastHeight }
  77. )
  78. return { data: { result: 9, code: 'timeOut' } }
  79. }
  80. // Do something with response error
  81. return Promise.reject(error)
  82. }
  83. )
  84. return _Fetchx
  85. }
  86. // function setToken(token) {
  87. // axios.defaults.headers.common["Cookie"] = token;
  88. // }
  89. export { Fetchx, BaseUrl, API, ImageBaseUrl, LoginFetchx, createFetchx }