San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 1 | import Mock from 'mockjs'; |
| 2 | import MockAdapter from 'axios-mock-adapter'; |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 3 | import {getHotPosts, getLikePosts, getPosts, getPostDetail} from '@/api/post' |
San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 4 | |
| 5 | /** |
| 6 | * 设置用户相关的 Mock 接口 |
| 7 | * @param {MockAdapter} mock |
| 8 | */ |
| 9 | export function setupPostMock(mock){ |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 10 | const hotPostsPattern = new RegExp(`^${getHotPosts}(\\?page=\\d+&size=\\d+)?$`); |
| 11 | const LikePostsPattern = new RegExp(`^${getLikePosts}(\\?page=\\d+&size=\\d+)?$`); |
| 12 | const searchPostsPattern = new RegExp(`^${getPosts}\\?((keyword=[^&]+&?)|(tags=[^&]+&?)|(author=[^&]+&?)|)+(page=\\d+)&(pageSize=\\d+)$`); |
| 13 | mock.onGet(hotPostsPattern).reply(config => { |
| 14 | const urlParams = new URLSearchParams(config.url.split('?')[1]); |
| 15 | const size = parseInt(urlParams.get('size')) || 10; |
| 16 | let data = Mock.mock({ |
| 17 | [`list|${size}`]: [ |
| 18 | { |
| 19 | 'postId|+1': 1, |
| 20 | 'postTitle': '@ctitle(5, 10)', |
| 21 | 'postContent': '@cparagraph(1, 3)', |
| 22 | 'author': '@cname()', |
| 23 | 'createdAt': '@date("yyyy-MM-dd")', |
| 24 | 'viewCount|1-100': 1, |
| 25 | }, |
| 26 | ], |
| 27 | }); |
| 28 | return [200, data.list]; |
| 29 | }); |
| 30 | mock.onGet(LikePostsPattern).reply(config => { |
| 31 | const urlParams = new URLSearchParams(config.url.split('?')[1]); |
| 32 | const size = parseInt(urlParams.get('size')) || 10; |
| 33 | let data = Mock.mock({ |
| 34 | [`list|${size}`]: [ |
| 35 | { |
| 36 | 'postId|+1': 1, |
| 37 | 'postTitle': '@ctitle(5, 10)', |
| 38 | 'postContent': '@cparagraph(1, 3)', |
| 39 | 'author': '@cname()', |
| 40 | 'createdAt': '@date("yyyy-MM-dd")', |
| 41 | 'viewCount|1-100': 1, |
| 42 | }, |
| 43 | ], |
| 44 | }); |
| 45 | return [200, data.list]; |
| 46 | }); |
| 47 | mock.onGet(searchPostsPattern).reply(config => { |
| 48 | const urlParams = new URLSearchParams(config.url.split('?')[1]); |
| 49 | const tags = urlParams.get('tags')?.split(',') || []; // 将 tags 参数解析为数组 |
| 50 | const page = parseInt(urlParams.get('page')) || 1; // 默认 page 为 1 |
| 51 | const size = parseInt(urlParams.get('size')) || 10; // 默认 size 为 10 |
| 52 | let data = Mock.mock({ |
| 53 | [`list|${size}`]: [ |
| 54 | { |
| 55 | 'postId|+1': 1, |
| 56 | 'postTitle': '@ctitle(5, 10)', |
| 57 | 'postContent': '@cparagraph(1, 3)', |
| 58 | 'author': '@cname()', |
| 59 | 'createdAt': '@date("yyyy-MM-dd")', |
| 60 | 'viewCount|1-100': 1, |
| 61 | }, |
| 62 | ], |
| 63 | }); |
| 64 | return [200, data.list]; |
| 65 | }); |
| 66 | |
| 67 | |
| 68 | const getPostDetailPattern = new RegExp(`^${getPostDetail}/[0-9]+$`); |
| 69 | mock.onGet(getPostDetailPattern).reply(config => { |
| 70 | const postId = config.url.split('/').pop(); |
| 71 | let data = Mock.mock({ |
| 72 | 'postId': postId, |
| 73 | 'postTitle': '@ctitle(5, 10)', |
| 74 | 'postContent': '@cparagraph(1, 3)', |
| 75 | 'author': '@cname()', |
| 76 | 'createdAt': '@date("yyyy-MM-dd")', |
| 77 | 'viewCount|1-100': 1, |
| 78 | }); |
San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 79 | return [200, data]; |
| 80 | }); |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 81 | |
| 82 | |
San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 83 | } |