123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- /**
- |--------------------------------------------------
- | Fetchx
- | The available instance methods are listed below. The specified config will be merged with the instance config.
- Methods:
- axios#request(config)
- axios#get(url[, config])
- axios#delete(url[, config])
- axios#head(url[, config])
- axios#options(url[, config])
- axios#post(url[, data[, config]])
- axios#put(url[, data[, config]])
- axios#patch(url[, data[, config]])
- |--------------------------------------------------
- */
- import axios from 'axios'
- import API from './Restful'
- import Toast from 'react-native-root-toast'
- // "http://123.103.9.212:18095/"; //测试环境外网
- // "http://172.20.11.88/"; //测试环境
- // "http://192.168.199.112/occ-b2b-buyer/";//bingxv
- // "http://192.168.199.177:8769/"; //gaopeng
- // "http://192.168.199.225:8080/"; //shuifeng
- // "http://172.20.15.52/"; //开发环境
- // "http://10.11.112.37/:8769"; //shuifeng开发环境
- //
- //init
- let Fetchx,
- LoginFetchx,
- MyHeaders,
- BaseUrl,
- ImageBaseUrl,
- ApiPrefix = '/api'
- let Token, toast
- export const setBaseUrl = data => {
- isPromise = true
- // let LoginUrl = "http://172.20.11.88:80";
- BaseUrl = "http://"+data.url + (data.url.startsWith('https') ? '' : ':' + data.port)
- // 图片地址需要改
- ImageBaseUrl =
- "http://" + data.url + (data.url.startsWith('https') ? '' : ':' + data.port)
- // ImageBaseUrl = "http://172.20.11.71";
- Fetchx = createFetchx(BaseUrl, 35000)
- // 需要启动wbalone的工程,如果没启动需要设置到测试环境
- // LoginFetchx = createFetchx("http://10.16.2.52/", 10000);
- LoginFetchx = createFetchx(BaseUrl, 60000)
- }
- //factory fetch fn
- const createFetchx = (baseURL, timeout) => {
- MyHeaders = new Headers()
- _Fetchx = axios.create({
- baseURL: baseURL,
- timeout: timeout,
- headers: MyHeaders,
- withCredentials: true,
- })
- //Fetchx.interceptors
- // Add a request interceptor global
- _Fetchx.interceptors.request.use(
- config => {
- return config
- },
- error => {
- // Do something with request error
- return Promise.reject(error)
- }
- )
- // Add a response interceptor
- _Fetchx.interceptors.response.use(
- function(response) {
- // Do something with response data
- return response
- },
- function(error) {
- //timeout handler
- if (error.code === 'ECONNABORTED') {
- if (toast) {
- Toast.hide(toast)
- }
- toast = Toast.show(
- '登陆超时,请尝试重新登陆',
- { position: toastHeight }
- )
- return { data: { result: 9, code: 'timeOut' } }
- }
- // Do something with response error
- return Promise.reject(error)
- }
- )
- return _Fetchx
- }
- // function setToken(token) {
- // axios.defaults.headers.common["Cookie"] = token;
- // }
- export { Fetchx, BaseUrl, API, ImageBaseUrl, LoginFetchx, createFetchx }
|