|
1 | 1 | import axios from 'axios'
|
2 |
| -import { Message } from 'element-ui' |
| 2 | +import { MessageBox, Message } from 'element-ui' |
3 | 3 | import store from '@/store'
|
4 | 4 | import { getToken } from '@/utils/auth'
|
5 | 5 |
|
@@ -33,40 +33,39 @@ service.interceptors.response.use(
|
33 | 33 | * If you want to get information such as headers or status
|
34 | 34 | * Please return response => response
|
35 | 35 | */
|
36 |
| - response => response.data, |
37 | 36 | /**
|
38 | 37 | * 下面的注释为通过在response里,自定义code来标示请求状态
|
39 | 38 | * 当code返回如下情况则说明权限有问题,登出并返回到登录页
|
40 |
| - * 如想通过 xmlhttprequest 来状态码标识 逻辑可写在下面error中 |
| 39 | + * 如想通过 XMLHttpRequest 来状态码标识 逻辑可写在下面error中 |
41 | 40 | * 以下代码均为样例,请结合自生需求加以修改,若不需要,则可删除
|
42 | 41 | */
|
43 |
| - // response => { |
44 |
| - // const res = response.data |
45 |
| - // if (res.code !== 20000) { |
46 |
| - // Message({ |
47 |
| - // message: res.message, |
48 |
| - // type: 'error', |
49 |
| - // duration: 5 * 1000 |
50 |
| - // }) |
51 |
| - // // 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了; |
52 |
| - // if (res.code === 50008 || res.code === 50012 || res.code === 50014) { |
53 |
| - // // 请自行在引入 MessageBox |
54 |
| - // // import { Message, MessageBox } from 'element-ui' |
55 |
| - // MessageBox.confirm('你已被登出,可以取消继续留在该页面,或者重新登录', '确定登出', { |
56 |
| - // confirmButtonText: '重新登录', |
57 |
| - // cancelButtonText: '取消', |
58 |
| - // type: 'warning' |
59 |
| - // }).then(() => { |
60 |
| - // store.dispatch('user/resetToken').then(() => { |
61 |
| - // location.reload() // 为了重新实例化vue-router对象 避免bug |
62 |
| - // }) |
63 |
| - // }) |
64 |
| - // } |
65 |
| - // return Promise.reject('error') |
66 |
| - // } else { |
67 |
| - // return response.data |
68 |
| - // } |
69 |
| - // }, |
| 42 | + response => { |
| 43 | + const res = response.data |
| 44 | + if (res.code !== 20000) { |
| 45 | + Message({ |
| 46 | + message: res.message, |
| 47 | + type: 'error', |
| 48 | + duration: 5 * 1000 |
| 49 | + }) |
| 50 | + // 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了; |
| 51 | + if (res.code === 50008 || res.code === 50012 || res.code === 50014) { |
| 52 | + // 请自行在引入 MessageBox |
| 53 | + // import { Message, MessageBox } from 'element-ui' |
| 54 | + MessageBox.confirm('你已被登出,可以取消继续留在该页面,或者重新登录', '确定登出', { |
| 55 | + confirmButtonText: '重新登录', |
| 56 | + cancelButtonText: '取消', |
| 57 | + type: 'warning' |
| 58 | + }).then(() => { |
| 59 | + store.dispatch('user/resetToken').then(() => { |
| 60 | + location.reload() // 为了重新实例化vue-router对象 避免bug |
| 61 | + }) |
| 62 | + }) |
| 63 | + } |
| 64 | + return Promise.reject('error') |
| 65 | + } else { |
| 66 | + return res |
| 67 | + } |
| 68 | + }, |
70 | 69 | error => {
|
71 | 70 | console.log('err' + error) // for debug
|
72 | 71 | Message({
|
|
0 commit comments