Last active
March 13, 2019 01:35
-
-
Save Xiaohantx/78c27ace75627bffcfac5b9bb2928076 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios' | |
import { baseURL } from '@/config' | |
class HttpRequest { | |
constructor (baseUrl = baseURL) { | |
this.baseUrl = baseUrl | |
this.queue = {} | |
} | |
getInsideConfig () { | |
const config = { | |
baseURL: this.baseUrl, | |
headers: { | |
// | |
} | |
} | |
return config | |
} | |
distroy (url) { | |
delete this.queue[url] | |
if (!Object.keys(this.queue).length) { | |
// Spin.hide() | |
} | |
} | |
interceptors (instance, url) { | |
instance.interceptors.request.use(config => { | |
// 添加全局的loading... | |
if (!Object.keys(this.queue).length) { | |
// Spin.show() | |
} | |
this.queue[url] = true | |
config.headers['Authorization'] = getToken() | |
return config | |
}, error => { | |
return Promise.reject(error) | |
}) | |
instance.interceptors.response.use(res => { | |
this.distroy(url) | |
const { data } = res | |
return data | |
}, error => { | |
this.distroy(url) | |
return Promise.reject(error.response.data) | |
}) | |
} | |
request (options) { | |
const instance = axios.create() | |
options = Object.assign(this.getInsideConfig(), options) | |
this.interceptors(instance, options.url) | |
return instance(options) | |
} | |
} | |
export default HttpRequest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment