Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复token过期或错误后 弹框拦截不下来 控制台报很多错的问题 #3702

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 21 additions & 17 deletions src/utils/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ import { MessageBox, Message } from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'

function confirmReLogin() {
return new Promise((resolve, reject) => {
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()
})
})
})
}

// create an axios instance
const service = axios.create({
baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
Expand Down Expand Up @@ -42,30 +56,20 @@ service.interceptors.response.use(
* Here is just an example
* You can also judge the status by HTTP Status Code
*/
response => {
async response => {
const res = response.data

// if the custom code is not 20000, it is judged as an error.
if (res.code !== 20000) {
// 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
ToshiFourteen marked this conversation as resolved.
Show resolved Hide resolved
// to re-login
await confirmReLogin()
} else if (res.code !== 20000) {
// if the custom code is not 20000, it is judged as an error.
Message({
message: res.message || 'Error',
type: 'error',
duration: 5 * 1000
})

// 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
// 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.message || 'Error'))
} else {
return res
Expand Down