demo
实现封装代码的文件
1 2 3 4 5 6 7 8 9 10 11
| ┣━ http ┃ ┣━ axios.config.js ┃ ┣━ default.js ┃ ┣━ index.js ┃ ┣━ request.js ┃ ┣━ response.js ┣━ http_store ┃ ┣━ default.js ┃ ┣━ index.js ┃ ┣━ request.js ┃ ┣━ response.js
|
实现原理
是在类class
中声明 requestList
请求队列变量,然后通过set
get
函数监听请求队列变化,当请求队列为空的时候,关闭loading
,反之显示。
axios.config.js
实现静默功能的主要代码,和声明axios
实例
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
| import { Indicator, Toast } from 'mint-ui'
import axios from './default' import request from './request' import response from './response'
export default new class Axios { constructor() { this.axios = axios.create() this.axios.interceptors.request.use( request.config, request.error, ) this.axios.interceptors.response.use( response.res, response.error, )
this._requestList = [] this.Indicator = Indicator this.Toast = Toast }
get requestList() { return this._requestList }
set requestList(list) { this._requestList = list if (this._requestList.length === 0) { this.Indicator.close() } else { this.Indicator.open({ spinnerType: 'fading-circle', }) } }
setRequestList({ id = '', remove = false } = {}) { const array = [...this._requestList] remove ? array.splice(array.indexOf(id), 1) : array.push(id) this.requestList = array } }()
|
default.js
axios
默认配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import axios from 'axios'
if (process.env.NODE_ENV === 'development') { axios.defaults.baseURL = 'http://dev.xxx.com' } else if (process.env.NODE_ENV === 'production') { axios.defaults.baseURL = 'http://prod.xxx.com' }
axios.defaults.withCredentials = true axios.defaults.headers.common['Content-Type'] = 'application/json' axios.defaults.baseURL = '/' axios.defaults.timeout = 5000
export default axios
|
request.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import qs from 'qs'
export default { config: config => { if ( config.method === 'post' || config.method === 'put' || config.method === 'delete' ) { config.headers['Content-Type'] = 'application/json; charset=utf-8' config.data = qs.stringify(config.data); } return config }, error: error => { return Promise.reject(error) } }
|
response.js
1 2 3 4 5 6 7 8 9 10 11 12 13
| export default { res: res => { let data = res.data if (~~data.code === 200) { return data } return Promise.reject(data) }, error: error => { return Promise.reject(error) }
}
|
index.js
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
| import Axios from './axios.config'
export default async function request({ url = '', method = 'get', data = {}, params = {}, headers = {}, success = () => {}, fail = () => {}, complete = () => {}, silent = false, all = false, } = {}) { const id = `${url}TIME${new Date().getTime()}` if (!silent) { Axios.setRequestList({ id }) } try { const result = await Axios.axios({ url, method, headers, data, params, }) Axios.setRequestList({ el: id, remove: true }) all ? success(result) : success(result.data) } catch (error) { Axios.setRequestList({ el: id, remove: true }) if (!silent) Axios.Toast(error.msg || error.message || error) fail(error) } finally { complete() } }
|
返回promise
的request
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
| import Axios from './axios.config'
export default function request({ url = '', method = 'get', data = {}, params = {}, headers = {}, silent = false } = {}) { const id = `${url}TIME${new Date().getTime()}` if (!silent) { Axios.setRequestList({ id }) }
return Axios.axios({ url, method, headers, data, params, }) .then(() => { Axios.setRequestList({ el: id, remove: true }) }) .catch(error => { Axios.setRequestList({ el: id, remove: true }) if (!silent) Axios.Toast(error.msg || error.message || error) }) }
|
git源码地址
axios 中文使用文档