new_Fetchx.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. isPromise = true
  37. // let LoginUrl = "http://172.20.11.88:80";
  38. BaseUrl = "http://"+data.url + (data.url.startsWith('https') ? '' : ':' + data.port)
  39. // 图片地址需要改
  40. ImageBaseUrl =
  41. "http://" + data.url + (data.url.startsWith('https') ? '' : ':' + data.port)
  42. // ImageBaseUrl = "http://172.20.11.71";
  43. Fetchx = createFetchx(BaseUrl, 35000)
  44. // 需要启动wbalone的工程,如果没启动需要设置到测试环境
  45. // LoginFetchx = createFetchx("http://10.16.2.52/", 10000);
  46. LoginFetchx = createFetchx(BaseUrl, 60000)
  47. }
  48. //factory fetch fn
  49. const createFetchx = (baseURL, timeout) => {
  50. MyHeaders = new Headers()
  51. _Fetchx = axios.create({
  52. baseURL: baseURL,
  53. timeout: timeout,
  54. headers: MyHeaders,
  55. withCredentials: true,
  56. })
  57. //Fetchx.interceptors
  58. // Add a request interceptor global
  59. _Fetchx.interceptors.request.use(
  60. config => {
  61. return config
  62. },
  63. error => {
  64. // Do something with request error
  65. return Promise.reject(error)
  66. }
  67. )
  68. // Add a response interceptor
  69. _Fetchx.interceptors.response.use(
  70. function(response) {
  71. // Do something with response data
  72. return response
  73. },
  74. function(error) {
  75. //timeout handler
  76. if (error.code === 'ECONNABORTED') {
  77. if (toast) {
  78. Toast.hide(toast)
  79. }
  80. toast = Toast.show(
  81. '登陆超时,请尝试重新登陆',
  82. { position: toastHeight }
  83. )
  84. return { data: { result: 9, code: 'timeOut' } }
  85. }
  86. // Do something with response error
  87. return Promise.reject(error)
  88. }
  89. )
  90. return _Fetchx
  91. }
  92. // function setToken(token) {
  93. // axios.defaults.headers.common["Cookie"] = token;
  94. // }
  95. export { Fetchx, BaseUrl, API, ImageBaseUrl, LoginFetchx, createFetchx }