1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| import axios from 'axios'
class HttpRequest{ constructor(){ this.baseURL = process.enV.NODE_ENV === 'production'? '/' : 'http://localhost:8001' this.timeout = 3000 this.queue = {} } setInterceptor(instance,url){ if(Object.keys(this.queue).length == 0){ } let token = localStorage.getItem('token') if(token){ config.headers.authorization = token } let CancelToken = axios.CancelToken config.cancelToken = new CancelToken(c=>{ store.commit(Types.SET_TOKEN,c) }) this.queue[url] = true return config } request(options){ let instance = axios.create() let config = { baseURL: this.baseURL, timeout: this.timeout, ...options } this.setInterceptor(instance,config.url) return instance(config) } get(url,data = {}){ return this.request({ url, method:'get', ...data }) } post(url,data = {}){ return this.request({ url, method:'get', data }) } }
export default new HttpRequest
|