Skip to content

Commit 5aa2273

Browse files
committed
rewrite axios && mock data
1 parent 1d05d66 commit 5aa2273

File tree

17 files changed

+137
-204
lines changed

17 files changed

+137
-204
lines changed

src/api/article.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fetch } from 'utils/fetch';
1+
import fetch from 'utils/fetch';
22

33
export function getList() {
44
return fetch({

src/api/article_table.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fetch } from 'utils/fetch';
1+
import fetch from 'utils/fetch';
22

33
export function fetchList(query) {
44
return fetch({

src/api/login.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fetch } from 'utils/fetch';
1+
import fetch from 'utils/fetch';
22

33
export function loginByEmail(email, password) {
44
const data = {
@@ -19,7 +19,6 @@ export function logout() {
1919
});
2020
}
2121

22-
2322
export function getInfo(token) {
2423
return fetch({
2524
url: '/user/info',

src/api/qiniu.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
import fetch, { tpFetch } from 'utils/fetch';
1+
// import fetch, { tpFetch } from 'utils/fetch';
22

3-
export function getToken() {
4-
return fetch({
5-
url: '/qiniu/upload/token',
6-
method: 'get'
7-
});
8-
}
9-
export function upload(data) {
10-
return tpFetch({
11-
url: 'https://upload.qbox.me',
12-
method: 'post',
13-
data
14-
});
15-
}
3+
// export function getToken() {
4+
// return fetch({
5+
// url: '/qiniu/upload/token',
6+
// method: 'get'
7+
// });
8+
// }
9+
// export function upload(data) {
10+
// return tpFetch({
11+
// url: 'https://upload.qbox.me',
12+
// method: 'post',
13+
// data
14+
// });
15+
// }
1616

1717

18-
/* 外部uri转七牛uri*/
19-
export function netUpload(token, net_url) {
20-
const imgData = {
21-
net_url
22-
};
23-
return fetch({
24-
url: '/qiniu/upload/net/async',
25-
method: 'post',
26-
data: imgData
27-
});
28-
}
18+
// /* 外部uri转七牛uri*/
19+
// export function netUpload(token, net_url) {
20+
// const imgData = {
21+
// net_url
22+
// };
23+
// return fetch({
24+
// url: '/qiniu/upload/net/async',
25+
// method: 'post',
26+
// data: imgData
27+
// });
28+
// }

src/api/remoteSearch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fetch } from 'utils/fetch';
1+
import fetch from 'utils/fetch';
22

33
export function userSearch(name) {
44
return fetch({

src/mock/article.js

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,21 @@ for (let i = 0; i < count; i++) {
1717
}
1818

1919
export default {
20-
getList: () => new Promise(resolve => {
21-
setTimeout(() => {
22-
resolve([200, {
23-
data: List
24-
}]);
25-
}, 100);
26-
}),
27-
getArticle: () => new Promise(resolve => {
28-
setTimeout(() => {
29-
resolve([200, {
30-
data: {
31-
id: 120000000001,
32-
author: { key: 'mockPan' },
33-
source_name: '原创作者',
34-
category_item: [{ key: 'global', name: '全球' }],
35-
comment_disabled: false,
36-
content: '<p>我是测试数据我是测试数据</p><p><img class="wscnph" src="https://wpimg.wallstcn.com/4c69009c-0fd4-4153-b112-6cb53d1cf943" data-wscntype="image" data-wscnh="300" data-wscnw="400" data-mce-src="https://wpimg.wallstcn.com/4c69009c-0fd4-4153-b112-6cb53d1cf943"></p>"',
37-
content_short: '我是测试数据',
38-
display_time: +new Date(),
39-
image_uri: 'https://wpimg.wallstcn.com/e4558086-631c-425c-9430-56ffb46e70b3',
40-
platforms: ['a-platform'],
41-
source_uri: 'https://github.com/PanJiaChen/vue-element-admin',
42-
status: 'published',
43-
tags: [],
44-
title: ''
45-
}
46-
}]);
47-
}, 100);
20+
getList: () => List,
21+
getArticle: () => ({
22+
id: 120000000001,
23+
author: { key: 'mockPan' },
24+
source_name: '原创作者',
25+
category_item: [{ key: 'global', name: '全球' }],
26+
comment_disabled: false,
27+
content: '<p>我是测试数据我是测试数据</p><p><img class="wscnph" src="https://wpimg.wallstcn.com/4c69009c-0fd4-4153-b112-6cb53d1cf943" data-wscntype="image" data-wscnh="300" data-wscnw="400" data-mce-src="https://wpimg.wallstcn.com/4c69009c-0fd4-4153-b112-6cb53d1cf943"></p>"',
28+
content_short: '我是测试数据',
29+
display_time: +new Date(),
30+
image_uri: 'https://wpimg.wallstcn.com/e4558086-631c-425c-9430-56ffb46e70b3',
31+
platforms: ['a-platform'],
32+
source_uri: 'https://github.com/PanJiaChen/vue-element-admin',
33+
status: 'published',
34+
tags: [],
35+
title: ''
4836
})
4937
};

src/mock/article_table.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Mock from 'mockjs';
2-
2+
import { param2Obj } from 'utils';
33

44
const List = [];
55
const count = 100;
@@ -21,9 +21,9 @@ for (let i = 0; i < count; i++) {
2121

2222
export default {
2323
getList: config => {
24-
const { importance, type, title, page, limit, sort } = config.params;
24+
const { importance, type, title, page, limit, sort } = param2Obj(config.url);
2525
let mockList = List.filter(item => {
26-
if (importance && item.importance !== importance) return false;
26+
if (importance && item.importance !== +importance) return false;
2727
if (type && item.type !== type) return false;
2828
if (title && item.title.indexOf(title) < 0) return false;
2929
return true;
@@ -33,21 +33,12 @@ export default {
3333
}
3434

3535
const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1));
36-
37-
return new Promise(resolve => {
38-
setTimeout(() => {
39-
resolve([200, {
40-
total: mockList.length,
41-
items: pageList
42-
}]);
43-
}, 100);
44-
})
36+
return {
37+
total: mockList.length,
38+
items: pageList
39+
}
4540
},
46-
getPv: () => new Promise(resolve => {
47-
setTimeout(() => {
48-
resolve([200, {
49-
pvData: [{ key: 'PC网站', pv: 1024 }, { key: 'mobile网站', pv: 1024 }, { key: 'ios', pv: 1024 }, { key: 'android', pv: 1024 }]
50-
}]);
51-
}, 100);
41+
getPv: () => ({
42+
pvData: [{ key: 'PC网站', pv: 1024 }, { key: 'mobile网站', pv: 1024 }, { key: 'ios', pv: 1024 }, { key: 'android', pv: 1024 }]
5243
})
5344
};

src/mock/index.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
1-
import axios from 'axios';
2-
import MockAdapter from 'axios-mock-adapter';
1+
import Mock from 'mockjs';
32
import loginAPI from './login';
43
import articleAPI from './article';
54
import article_tableAPI from './article_table';
65
import remoteSearchAPI from './remoteSearch';
7-
const mock = new MockAdapter(axios);
6+
87

98
// 登录相关
10-
mock.onPost('/login/loginbyemail').reply(loginAPI.loginByEmail);
11-
mock.onPost('/login/logout').reply(loginAPI.logout);
12-
mock.onGet('/user/info').reply(loginAPI.getInfo);
9+
Mock.mock(/\/login\/loginbyemail/, 'post', loginAPI.loginByEmail);
10+
Mock.mock(/\/login\/logout/, 'post', loginAPI.logout);
11+
Mock.mock(/\/user\/info\.*/, 'get', loginAPI.getInfo)
1312

14-
// 文章相关
15-
mock.onGet('/article/list').reply(articleAPI.getList);
16-
mock.onGet('/article/detail').reply(articleAPI.getArticle);
13+
// // 文章相关
14+
Mock.mock(/\/article\/list/, 'get', articleAPI.getList);
15+
Mock.mock(/\/article\/detail/, 'get', articleAPI.getArticle);
1716

18-
// table example相关
19-
mock.onGet('/article_table/list').reply(article_tableAPI.getList);
20-
mock.onGet('/article_table/pv').reply(article_tableAPI.getPv);
17+
// // table example相关
18+
Mock.mock(/\/article_table\/list/, 'get', article_tableAPI.getList);
19+
Mock.mock(/\/article_table\/p/, 'get', article_tableAPI.getPv);
2120

22-
// 搜索相关
23-
mock.onGet('/search/user').reply(remoteSearchAPI.searchUser);
21+
// // 搜索相关
22+
Mock.mock(/\/search\/user/, 'get', remoteSearchAPI.searchUser);
2423

25-
mock.onAny().passThrough();
2624

27-
export default mock;
25+
export default Mock;

src/mock/login.js

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { param2Obj } from 'utils';
2+
13
const userMap = {
24
admin: {
35
role: ['admin'],
@@ -28,36 +30,17 @@ const userMap = {
2830

2931
export default {
3032
loginByEmail: config => {
31-
const { email } = JSON.parse(config.data);
32-
return new Promise((resolve, reject) => {
33-
if (userMap[email.split('@')[0]]) {
34-
setTimeout(() => {
35-
resolve([200, {
36-
data: userMap[email.split('@')[0]]
37-
}]);
38-
}, 500);
39-
} else {
40-
reject('账号不正确')
41-
}
42-
})
33+
console.log(config)
34+
const { email } = JSON.parse(config.body);
35+
return userMap[email.split('@')[0]];
4336
},
4437
getInfo: config => {
45-
const { token } = config.params;
46-
return new Promise((resolve, reject) => {
47-
if (userMap[token]) {
48-
setTimeout(() => {
49-
resolve([200, {
50-
data: userMap[token]
51-
}]);
52-
}, 100);
53-
} else {
54-
reject('获取失败')
55-
}
56-
})
38+
const { token } = param2Obj(config.url);
39+
if (userMap[token]) {
40+
return userMap[token];
41+
} else {
42+
return Promise.reject('a');
43+
}
5744
},
58-
logout: () => new Promise(resolve => {
59-
setTimeout(() => {
60-
resolve([200, { data: 'success' }]);
61-
}, 100);
62-
})
45+
logout: () => 'success'
6346
};

src/mock/remoteSearch.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Mock from 'mockjs';
2+
import { param2Obj } from 'utils';
23

34
const NameList = [];
45
const count = 100;
@@ -12,18 +13,12 @@ NameList.push({ name: 'mockPan' })
1213

1314
export default {
1415
searchUser: config => {
15-
const { name } = config.params;
16+
const { name } = param2Obj(config.url);
1617
const mockNameList = NameList.filter(item => {
1718
const lowerCaseName = item.name.toLowerCase()
1819
if (name && lowerCaseName.indexOf(name.toLowerCase()) < 0) return false;
1920
return true;
2021
});
21-
return new Promise(resolve => {
22-
setTimeout(() => {
23-
resolve([200, {
24-
items: mockNameList
25-
}]);
26-
}, 100);
27-
})
22+
return { items: mockNameList }
2823
}
2924
};

0 commit comments

Comments
 (0)