Skip to content

Commit d927d41

Browse files
committed
refine code
1 parent fd4b5c4 commit d927d41

File tree

9 files changed

+81
-55
lines changed

9 files changed

+81
-55
lines changed

mock/article.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ export default [
4848
const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
4949

5050
return {
51-
total: mockList.length,
52-
items: pageList
51+
code: '20000',
52+
data: {
53+
total: mockList.length,
54+
items: pageList
55+
}
5356
}
5457
}
5558
},
@@ -61,7 +64,10 @@ export default [
6164
const { id } = config.query
6265
for (const article of List) {
6366
if (article.id === +id) {
64-
return article
67+
return {
68+
code: '20000',
69+
data: article
70+
}
6571
}
6672
}
6773
}
@@ -71,12 +77,16 @@ export default [
7177
url: '/article/pv',
7278
type: 'get',
7379
response: _ => {
74-
return { pvData: [
75-
{ key: 'PC', pv: 1024 },
76-
{ key: 'mobile', pv: 1024 },
77-
{ key: 'ios', pv: 1024 },
78-
{ key: 'android', pv: 1024 }
79-
]
80+
return {
81+
code: '20000',
82+
data: {
83+
pvData: [
84+
{ key: 'PC', pv: 1024 },
85+
{ key: 'mobile', pv: 1024 },
86+
{ key: 'ios', pv: 1024 },
87+
{ key: 'android', pv: 1024 }
88+
]
89+
}
8090
}
8191
}
8292
},
@@ -86,6 +96,7 @@ export default [
8696
type: 'post',
8797
response: _ => {
8898
return {
99+
code: '20000',
89100
data: 'success'
90101
}
91102
}
@@ -96,6 +107,7 @@ export default [
96107
type: 'post',
97108
response: _ => {
98109
return {
110+
code: '20000',
99111
data: 'success'
100112
}
101113
}

mock/remoteSearch.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ export default [
2121
const lowerCaseName = item.name.toLowerCase()
2222
return !(name && lowerCaseName.indexOf(name.toLowerCase()) < 0)
2323
})
24-
return { items: mockNameList }
24+
return {
25+
code: '20000',
26+
data: { items: mockNameList }
27+
}
2528
}
2629
},
2730

@@ -31,14 +34,17 @@ export default [
3134
type: 'get',
3235
response: _ => {
3336
return {
34-
total: 20,
35-
'items|20': [{
36-
order_no: '@guid()',
37-
timestamp: +Mock.Random.date('T'),
38-
username: '@name()',
39-
price: '@float(1000, 15000, 0, 2)',
40-
'status|1': ['success', 'pending']
41-
}]
37+
code: '20000',
38+
data: {
39+
total: 20,
40+
'items|20': [{
41+
order_no: '@guid()',
42+
timestamp: +Mock.Random.date('T'),
43+
username: '@name()',
44+
price: '@float(1000, 15000, 0, 2)',
45+
'status|1': ['success', 'pending']
46+
}]
47+
}
4248
}
4349
}
4450
}

mock/user.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ export default [
3030
type: 'post',
3131
response: config => {
3232
const { username } = config.body
33-
return tokens[username]
33+
return {
34+
code: '20000',
35+
data: tokens[username]
36+
}
3437
}
3538
},
3639

@@ -40,7 +43,10 @@ export default [
4043
type: 'get',
4144
response: config => {
4245
const { token } = config.query
43-
return users[token]
46+
return {
47+
code: '20000',
48+
data: users[token]
49+
}
4450
}
4551
},
4652

@@ -50,6 +56,7 @@ export default [
5056
type: 'post',
5157
response: _ => {
5258
return {
59+
code: '20000',
5360
data: 'success'
5461
}
5562
}

src/layout/components/Navbar.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ export default {
7777
toggleSideBar() {
7878
this.$store.dispatch('toggleSideBar')
7979
},
80-
logout() {
81-
this.$store.dispatch('Logout').then(() => {
82-
location.reload()// In order to re-instantiate the vue-router object to avoid bugs
83-
})
80+
async logout() {
81+
await this.$store.dispatch('Logout')
82+
// In order to re-instantiate the vue-router object to avoid bugs
83+
location.reload()
8484
}
8585
}
8686
}

src/store/modules/user.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const user = {
3636
login({ username: username.trim(), password: password }).then(response => {
3737
const { data } = response
3838
commit('SET_TOKEN', data.token)
39-
setToken(response.data.token)
39+
setToken(data.token)
4040
resolve()
4141
}).catch(error => {
4242
reject(error)
@@ -54,17 +54,17 @@ const user = {
5454
reject('Verification failed, please Login again.')
5555
}
5656

57-
const { roles } = data
57+
const { roles, name, avatar, introduction } = data
5858

5959
// roles must be a non-empty array
6060
if (!roles || roles.length <= 0) {
6161
reject('getInfo: roles must be a non-null array!')
6262
}
6363

64-
commit('SET_ROLES', data.roles)
65-
commit('SET_NAME', data.name)
66-
commit('SET_AVATAR', data.avatar)
67-
commit('SET_INTRODUCTION', data.introduction)
64+
commit('SET_ROLES', roles)
65+
commit('SET_NAME', name)
66+
commit('SET_AVATAR', avatar)
67+
commit('SET_INTRODUCTION', introduction)
6868
resolve(data)
6969
}).catch(error => {
7070
reject(error)

src/utils/request.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ service.interceptors.request.use(
2929

3030
// response interceptor
3131
service.interceptors.response.use(
32-
response => response,
32+
/**
33+
* If you want to get information such as headers or status
34+
* Please return response => response
35+
*/
36+
response => response.data,
3337
/**
3438
* 下面的注释为通过在response里,自定义code来标示请求状态
3539
* 当code返回如下情况则说明权限有问题,登出并返回到登录页

src/views/table/dragTable.vue

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,16 @@ export default {
9696
this.getList()
9797
},
9898
methods: {
99-
getList() {
99+
async getList() {
100100
this.listLoading = true
101-
fetchList(this.listQuery).then(response => {
102-
this.list = response.data.items
103-
this.total = response.data.total
104-
this.listLoading = false
105-
this.oldList = this.list.map(v => v.id)
106-
this.newList = this.oldList.slice()
107-
this.$nextTick(() => {
108-
this.setSort()
109-
})
101+
const { data } = await fetchList(this.listQuery)
102+
this.list = data.items
103+
this.total = data.total
104+
this.listLoading = false
105+
this.oldList = this.list.map(v => v.id)
106+
this.newList = this.oldList.slice()
107+
this.$nextTick(() => {
108+
this.setSort()
110109
})
111110
},
112111
setSort() {

src/views/table/inlineEditTable.vue

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,16 @@ export default {
8888
this.getList()
8989
},
9090
methods: {
91-
getList() {
91+
async getList() {
9292
this.listLoading = true
93-
fetchList(this.listQuery).then(response => {
94-
const items = response.data.items
95-
this.list = items.map(v => {
96-
this.$set(v, 'edit', false) // https://vuejs.org/v2/guide/reactivity.html
97-
v.originalTitle = v.title // will be used when user click the cancel botton
98-
return v
99-
})
100-
this.listLoading = false
93+
const { data } = await fetchList(this.listQuery)
94+
const items = data.items
95+
this.list = items.map(v => {
96+
this.$set(v, 'edit', false) // https://vuejs.org/v2/guide/reactivity.html
97+
v.originalTitle = v.title // will be used when user click the cancel botton
98+
return v
10199
})
100+
this.listLoading = false
102101
},
103102
cancelEdit(row) {
104103
row.title = row.originalTitle

src/views/zip/index.vue

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,11 @@ export default {
5353
this.fetchData()
5454
},
5555
methods: {
56-
fetchData() {
56+
async fetchData() {
5757
this.listLoading = true
58-
fetchList().then(response => {
59-
this.list = response.data.items
60-
this.listLoading = false
61-
})
58+
const { data } = await fetchList()
59+
this.list = data.items
60+
this.listLoading = false
6261
},
6362
handleDownload() {
6463
this.downloadLoading = true

0 commit comments

Comments
 (0)