Skip to content

Commit

Permalink
feat(request): add interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
xianshenglu committed Nov 21, 2020
1 parent 2ef3100 commit 7e3d03a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"clipboard": "^2.0.6",
"element-ui": "^2.14.1",
"file-loader": "^6.2.0",
"lodash": "^4.17.20",
"vue": "^2.6.12"
}
}
27 changes: 27 additions & 0 deletions src/apis/request.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
import Axios from 'axios'
import _ from 'lodash'

function handleErrorResponse(error) {
let errorMsg = '未知错误!'

const isMessageValid =
_.isObject(error) &&
typeof error.message === 'string' &&
error.message.trim() !== ''

if (isMessageValid) {
errorMsg = error.message
}
// eslint-disable-next-line no-alert
alert(errorMsg)
return Promise.reject(error)
}

const instance = Axios.create({
withCredentials: true,
Expand All @@ -7,4 +24,14 @@ const instance = Axios.create({
},
})

// 添加请求拦截器
instance.interceptors.request.use(function (config) {
return config
}, handleErrorResponse)

// 添加响应拦截器
instance.interceptors.response.use(function (response) {
return response
}, handleErrorResponse)

export default instance

0 comments on commit 7e3d03a

Please sign in to comment.