123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- import axios from 'axios'
- import {
- baseURL,
- contentType,
- messageName,
- requestTimeout,
- statusName,
- successCode,
- } from '@/config/net.config'
- import configpath from 'config/setting.config.js'
- let baseURL1 = ''
- let isAction, isDataAll;
- const inforeq = (cb) => {
- fetch('/static/.config.json')
- .then((res) =>res = res.json()).then((res)=>{
- let obj = {
- ...res,
- img_local_host: (process.env.NODE_ENV === 'production' ?
- res.prodIp || configpath.prodIp :
- process.env.NODE_ENV === 'test' ?
- res.testIp || configpath.testIp :
- res.devIp || configpath.devIp) + '/file/',
-
- baseURL: (process.env.NODE_ENV === 'production' ?
- res.prodIp || configpath.prodIp :
- process.env.NODE_ENV === 'test' ?
- res.testIp || configpath.testIp :
- res.devIp || configpath.devIp) + '/api/h5',
- }
- baseURL1 = obj.baseURL
- cb && cb()
- })
- }
- inforeq()
- const handleData = async ({
- config,
- data,
- status,
- statusText
- }) => {
-
- let code = data && data[statusName] ? data[statusName] : status
-
- switch (code) {
- case 200:
-
-
-
-
-
-
-
-
-
-
- return data
- case 401:
-
-
-
-
-
- break
- case 403:
-
- break
- }
- if (isDataAll) {
- return data
- }
-
-
-
-
-
-
-
-
-
- return Promise.reject(data)
- }
- const instance = axios.create({
- baseURL,
- timeout: requestTimeout,
- headers: {
- 'Content-Type': contentType,
- },
- })
- let pendingMap = new Map();
- let removePending = function(config) {
- var key = `${config.method}:${config.url}:${JSON.stringify(config.params)}`;
- var func = pendingMap.get(key);
- if (func) {
- func();
- pendingMap.delete(key);
- }
- }
- instance.interceptors.request.use(
- (config) => {
- config.baseURL = baseURL1
-
-
- removePending(config);
-
- config.cancelToken = new axios.CancelToken((cancelToken) => {
- pendingMap.set(`${config.method}:${config.url}:${JSON.stringify(config.params)}`, cancelToken)
- });
-
-
- if (
- config.data &&
- config.headers['Content-Type'] ===
- 'application/x-www-form-urlencoded;charset=UTF-8'
- )
- config.data = JSON.stringify(config.data)
- isAction = config.isAction
- isDataAll = config.isDataAll
- return config
- },
- (error) => {
- return Promise.reject(error)
- }
- )
- let url = '',
- path1 = ''
- instance.interceptors.response.use(
- (response) => {
- const config = response.config
- url = `${config.method}:${config.url}:${JSON.stringify(config.params)}`;
- path1 = config.url
- return handleData(response)
- },
- (error) => {
- const {
- response
- } = error
-
- if (response === undefined) {
- if (path1 !== '/eye/save_hand_img') {
-
-
-
-
-
- } else {
- pendingMap.delete(key);
- }
- return Promise.reject({
- code: -1
- })
- } else return handleData(response)
- }
- )
- export default instance
|