Skip to content

Commit 76ec176

Browse files
committed
mock error
1 parent 6b9588e commit 76ec176

File tree

3 files changed

+52
-33
lines changed

3 files changed

+52
-33
lines changed

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
build/*.js
2-
config/*.js
32
src/assets
43
public
4+
dist

mock/user.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,19 @@ export default [
3030
type: 'post',
3131
response: config => {
3232
const { username } = config.body
33+
const token = tokens[username]
34+
35+
// mock error
36+
if (!token) {
37+
return {
38+
code: 60204,
39+
message: 'Account and password are incorrect.'
40+
}
41+
}
42+
3343
return {
3444
code: 20000,
35-
data: tokens[username]
45+
data: token
3646
}
3747
}
3848
},
@@ -43,9 +53,19 @@ export default [
4353
type: 'get',
4454
response: config => {
4555
const { token } = config.query
56+
const info = users[token]
57+
58+
// mock error
59+
if (!info) {
60+
return {
61+
code: 50008,
62+
message: 'Login failed, unable to get user details.'
63+
}
64+
}
65+
4666
return {
4767
code: 20000,
48-
data: users[token]
68+
data: info
4969
}
5070
}
5171
},

src/utils/request.js

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios from 'axios'
2-
import { Message } from 'element-ui'
2+
import { MessageBox, Message } from 'element-ui'
33
import store from '@/store'
44
import { getToken } from '@/utils/auth'
55

@@ -33,40 +33,39 @@ service.interceptors.response.use(
3333
* If you want to get information such as headers or status
3434
* Please return response => response
3535
*/
36-
response => response.data,
3736
/**
3837
* 下面的注释为通过在response里,自定义code来标示请求状态
3938
* 当code返回如下情况则说明权限有问题,登出并返回到登录页
40-
* 如想通过 xmlhttprequest 来状态码标识 逻辑可写在下面error中
39+
* 如想通过 XMLHttpRequest 来状态码标识 逻辑可写在下面error中
4140
* 以下代码均为样例,请结合自生需求加以修改,若不需要,则可删除
4241
*/
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+
},
7069
error => {
7170
console.log('err' + error) // for debug
7271
Message({

0 commit comments

Comments
 (0)