Revert "Revert "V0.4.0 imporve error response""

This reverts commit acccdb65bd.
This commit is contained in:
yaziming
2019-09-04 20:41:37 +08:00
parent a5449a674a
commit 5e2b3ca228
51 changed files with 1373 additions and 416 deletions

View File

@@ -3,28 +3,51 @@ import router from '../router'
let baseUrl = process.env.VUE_APP_BASE_URL ? process.env.VUE_APP_BASE_URL : 'http://localhost:8000'
const request = (method, path, params, data) => {
return new Promise((resolve, reject) => {
const request = async (method, path, params, data, others = {}) => {
try {
const url = baseUrl + path
const headers = {
'Authorization': window.localStorage.getItem('token')
}
axios({
const response = await axios({
method,
url,
params,
data,
headers
headers,
...others
})
.then(resolve)
.catch(error => {
console.log(error)
if (error.response.status === 401) {
router.push('/login')
}
reject(error)
})
})
// console.log(response)
return response
} catch (e) {
if (e.response.status === 401 && router.currentRoute.path !== '/login') {
router.push('/login')
}
await Promise.reject(e)
}
// return new Promise((resolve, reject) => {
// const url = baseUrl + path
// const headers = {
// 'Authorization': window.localStorage.getItem('token')
// }
// axios({
// method,
// url,
// params,
// data,
// headers,
// ...others
// })
// .then(resolve)
// .catch(error => {
// console.log(error)
// if (error.response.status === 401) {
// router.push('/login')
// }
// reject(error)
// })
// })
}
const get = (path, params) => {