refactor(courses): 移除未使用的搜索和模糊处理函数,优化搜索逻辑
移除CoursesPage.vue中未使用的handleSearch和handleBlur函数,简化代码结构。在CourseListPage.vue中引入防抖函数优化搜索性能,并统一处理搜索操作,提升代码可维护性。
Showing
2 changed files
with
21 additions
and
31 deletions
| 1 | <!-- | 1 | <!-- |
| 2 | * @Date: 2025-03-21 14:31:21 | 2 | * @Date: 2025-03-21 14:31:21 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-04-15 14:24:16 | 4 | + * @LastEditTime: 2025-05-21 16:57:36 |
| 5 | * @FilePath: /mlaj/src/views/courses/CourseListPage.vue | 5 | * @FilePath: /mlaj/src/views/courses/CourseListPage.vue |
| 6 | * @Description: 文件描述 | 6 | * @Description: 文件描述 |
| 7 | --> | 7 | --> |
| ... | @@ -39,6 +39,7 @@ import { courses as mockCourses } from '@/utils/mockData'; | ... | @@ -39,6 +39,7 @@ import { courses as mockCourses } from '@/utils/mockData'; |
| 39 | // 导入接口 | 39 | // 导入接口 |
| 40 | import { getCourseListAPI } from "@/api/course"; | 40 | import { getCourseListAPI } from "@/api/course"; |
| 41 | import { List } from 'vant'; | 41 | import { List } from 'vant'; |
| 42 | +import { useDebounceFn } from '@vueuse/core'; | ||
| 42 | 43 | ||
| 43 | const $route = useRoute(); | 44 | const $route = useRoute(); |
| 44 | const courses = ref([]); | 45 | const courses = ref([]); |
| ... | @@ -48,35 +49,35 @@ const limit = ref(5); | ... | @@ -48,35 +49,35 @@ const limit = ref(5); |
| 48 | const page = ref(0); | 49 | const page = ref(0); |
| 49 | const keyword = ref(''); | 50 | const keyword = ref(''); |
| 50 | 51 | ||
| 51 | -// 搜索课程列表 | 52 | +// 防抖处理的搜索课程列表 |
| 52 | -const searchCourses = async () => { | 53 | +const debouncedSearch = useDebounceFn(async (searchKeyword) => { |
| 53 | - const res = await getCourseListAPI({ limit: limit.value, page: 0, keyword: keyword.value }); | 54 | + const res = await getCourseListAPI({ limit: limit.value, page: 0, keyword: searchKeyword }); |
| 54 | if (res.code) { | 55 | if (res.code) { |
| 55 | courses.value = res.data; | 56 | courses.value = res.data; |
| 56 | finished.value = res.data.length < limit.value; | 57 | finished.value = res.data.length < limit.value; |
| 57 | page.value = 1; | 58 | page.value = 1; |
| 58 | } | 59 | } |
| 60 | +}, 300); | ||
| 61 | + | ||
| 62 | +// 统一处理搜索操作 | ||
| 63 | +const handleSearch = (newKeyword) => { | ||
| 64 | + if (keyword.value !== newKeyword) { | ||
| 65 | + keyword.value = newKeyword; | ||
| 66 | + debouncedSearch(newKeyword); | ||
| 67 | + } | ||
| 59 | }; | 68 | }; |
| 60 | 69 | ||
| 61 | // 监听路由参数变化 | 70 | // 监听路由参数变化 |
| 62 | -// watchEffect(() => { | 71 | +watchEffect(() => { |
| 63 | -// const queryKeyword = $route.query.keyword; | 72 | + const queryKeyword = $route.query.keyword; |
| 64 | -// if (keyword.value !== queryKeyword) { | 73 | + if (queryKeyword !== undefined) { |
| 65 | -// keyword.value = queryKeyword || ''; | 74 | + handleSearch(queryKeyword || ''); |
| 66 | -// searchCourses(); | 75 | + } |
| 67 | -// } | 76 | +}); |
| 68 | -// }); | ||
| 69 | - | ||
| 70 | -// Search handler | ||
| 71 | -// const handleSearch = (query) => { | ||
| 72 | - // keyword.value = query; | ||
| 73 | - // searchCourses(); | ||
| 74 | -// }; | ||
| 75 | 77 | ||
| 76 | // Blur handler | 78 | // Blur handler |
| 77 | const handleBlur = (query) => { | 79 | const handleBlur = (query) => { |
| 78 | - keyword.value = query; | 80 | + handleSearch(query); |
| 79 | - searchCourses(); | ||
| 80 | }; | 81 | }; |
| 81 | 82 | ||
| 82 | // Load more courses | 83 | // Load more courses | ... | ... |
| ... | @@ -3,7 +3,7 @@ | ... | @@ -3,7 +3,7 @@ |
| 3 | <div class="pb-16"> | 3 | <div class="pb-16"> |
| 4 | <!-- Search Bar --> | 4 | <!-- Search Bar --> |
| 5 | <div class="pb-2"> | 5 | <div class="pb-2"> |
| 6 | - <SearchBar placeholder="搜索" @search="handleSearch" @blur="handleBlur" :isCoursePage="true" /> | 6 | + <SearchBar placeholder="搜索" :isCoursePage="true" /> |
| 7 | </div> | 7 | </div> |
| 8 | 8 | ||
| 9 | <!-- Featured Course Banner --> | 9 | <!-- Featured Course Banner --> |
| ... | @@ -107,17 +107,6 @@ const $route = useRoute(); | ... | @@ -107,17 +107,6 @@ const $route = useRoute(); |
| 107 | const $router = useRouter(); | 107 | const $router = useRouter(); |
| 108 | useTitle($route.meta.title); | 108 | useTitle($route.meta.title); |
| 109 | 109 | ||
| 110 | -// Search handler | ||
| 111 | -const handleSearch = (query) => { | ||
| 112 | - console.log("Searching for:", query); | ||
| 113 | - // Would implement actual search logic here | ||
| 114 | -}; | ||
| 115 | -// Blur handler | ||
| 116 | -const handleBlur = (query) => { | ||
| 117 | - // 实际项目中这里会调用搜索API | ||
| 118 | - console.log('Searching for:', query); | ||
| 119 | -}; | ||
| 120 | - | ||
| 121 | // Current time for the countdown timer | 110 | // Current time for the countdown timer |
| 122 | const todayDate = new Date(); | 111 | const todayDate = new Date(); |
| 123 | const formattedTime = featuredCourse.liveTime.split(":"); | 112 | const formattedTime = featuredCourse.liveTime.split(":"); | ... | ... |
-
Please register or login to post a comment