diff --git a/src/plugins/axiosExtend.js b/src/plugins/axiosExtend.js new file mode 100644 index 0000000..22fc06f --- /dev/null +++ b/src/plugins/axiosExtend.js @@ -0,0 +1,38 @@ +import Axios from 'axios' + +const timeout = 3000 + +Axios.defaults.timeout = timeout + +function interception (fn, methods) { + return (...args) => { + // Request params num + const argsLen = args.length + + // GET Request + if (methods === 'get' && argsLen > 1) { + const requerParams = Object.entries(args[1]).map(([key, value]) => { + return `${encodeURIComponent(key)}=${encodeURIComponent(value)}` + }).join('&') + + args[0] += `?${requerParams}` + + args.pop() + + // POST Request + } else if (methods === 'post' && argsLen > 2) { + args[1] = Object.entries(args[1]).map(([key, value]) => `${key}=${value}`).join('&') + + args.pop() + } + + return fn.apply(this, args) + } +} + +export default function (Vue) { + Vue.prototype.$http = Axios + + Vue.prototype.$http.get = interception(Vue.prototype.$http.get, 'get') + Vue.prototype.$http.post = interception(Vue.prototype.$http.post, 'post') +} diff --git a/src/plugins/index.js b/src/plugins/index.js index 6f3d5df..8af570d 100644 --- a/src/plugins/index.js +++ b/src/plugins/index.js @@ -2,7 +2,10 @@ import methodsExtend from './methodsExtend' import canvasExtend from './canvasExtend' +import axiosExtend from './axiosExtend' + export default function (Vue) { methodsExtend(Vue) canvasExtend(Vue) + axiosExtend(Vue) }