Skip to content

Commit

Permalink
fix(sys-client): add 403 permission tip;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Sep 8, 2021
1 parent b0c8634 commit d845356
Showing 1 changed file with 22 additions and 32 deletions.
54 changes: 22 additions & 32 deletions packages/system-client/src/utils/request.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import { MessageBox } from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'
import { showError, showInfo } from './show'

// create an axios instance
const service = axios.create({
Expand Down Expand Up @@ -43,40 +44,29 @@ service.interceptors.response.use(
* You can also judge the status by HTTP Status Code
*/
response => {
const res = response
// const res = response.data
return response.data
},
error => {
const status = error.response.status

// if the custom code is not 20000, it is judged as an error.
if (res.status !== 200) {
Message({
message: res.error || 'Error',
type: 'error',
duration: 5 * 1000
if (status === 401) {
// to re-login
MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
confirmButtonText: 'Re-Login',
cancelButtonText: 'Cancel',
type: 'warning'
}).then(() => {
store.dispatch('user/resetToken')
.then(() => { location.reload() })
})

if (res.status === 401) {
// to re-login
MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
confirmButtonText: 'Re-Login',
cancelButtonText: 'Cancel',
type: 'warning'
}).then(() => {
store.dispatch('user/resetToken').then(() => {
location.reload()
})
})
}
return Promise.reject(new Error(res.data.error || 'Error'))
} else {
return res.data
return Promise.reject(error)
}
},
error => {
Message({
message: error.message,
type: 'info',
duration: 5 * 1000
})
if (status === 403) {
showInfo('无此操作权限')
return Promise.reject(error)
}

showError(error.message)
return Promise.reject(error)
}
)
Expand Down

0 comments on commit d845356

Please sign in to comment.