diff --git a/Frontend/src/api/product.ts b/Frontend/src/api/product.ts index 8a2239df..96c3817c 100644 --- a/Frontend/src/api/product.ts +++ b/Frontend/src/api/product.ts @@ -79,5 +79,13 @@ export const productApi = { unlikeProduct: (productId: number) => { return api.post(`/product/unlike/${productId}`) + }, + + searchProducts: (keyword: string) => { + return api.get('/product/list/search', { + params: { + keyword: keyword + } + }) } } diff --git a/Frontend/src/views/ProductListView.vue b/Frontend/src/views/ProductListView.vue index 52ed3c93..d51e5b95 100644 --- a/Frontend/src/views/ProductListView.vue +++ b/Frontend/src/views/ProductListView.vue @@ -6,8 +6,17 @@ -

商品列表

- +

商品列表

+
+ +
+ @@ -195,6 +204,11 @@ const hasMore = ref(true) // 加载状态 const loading = ref(false) +// 搜索相关状态 +const showSearch = ref(false) +const searchKeyword = ref('') +const isSearching = ref(false) + // 图片ID缓存 const imageIdCache = ref>({}) const imageUrls = ref>({}) @@ -280,7 +294,9 @@ const handleBackClick = () => { // 处理分类点击 const handleCategoryClick = (category: string) => { selectedCategory.value = category - // 这里可以添加筛选逻辑 + showSearch.value = false + searchKeyword.value = '' + isSearching.value = false fetchProducts() } @@ -305,6 +321,37 @@ const handleLoadMore = () => { hasMore.value = false } +// 切换搜索框显示/隐藏 +const toggleSearch = () => { + showSearch.value = !showSearch.value + if (!showSearch.value) { + searchKeyword.value = '' + isSearching.value = false + fetchProducts() // 重置为默认商品列表 + } +} + +// 处理搜索操作 +const handleSearch = async () => { + if (!searchKeyword.value.trim()) { + message.info('请输入搜索关键词') + return + } + + loading.value = true + try { + const response = await productApi.searchProducts(searchKeyword.value.trim()) as any + products.value = response || [] + await loadImageUrls(products.value) + hasMore.value = false + isSearching.value = true + } catch (error) { + message.error('搜索失败,请重试') + } finally { + loading.value = false + } +} + // 页面挂载时获取标签和商品数据 onMounted(async () => { await fetchTags() @@ -349,6 +396,15 @@ onMounted(async () => { justify-content: center; } +.search-input-container { + flex: 1; + margin: 0 10px; +} + +.search-input { + width: 100%; +} + /* 主内容区域 */ .main-content { display: flex; diff --git a/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/config/WebMvcConfig.java b/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/config/WebMvcConfig.java index 23693315..dd288b65 100644 --- a/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/config/WebMvcConfig.java +++ b/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/config/WebMvcConfig.java @@ -23,6 +23,9 @@ public class WebMvcConfig implements WebMvcConfigurer { "/user/phone-login", "/user/get/account/**", "/shop/list/simple", + "/product/**", + "/tag/**", + "/file/**", "/admin/auth/login", "/admin/object-storage-file/upload", "/error", diff --git a/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/controller/ProductController.java b/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/controller/ProductController.java index c830a320..050e334d 100644 --- a/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/controller/ProductController.java +++ b/YumiNail-Core/src/main/java/com/xiaowang/yuminailcore/controller/ProductController.java @@ -4,6 +4,7 @@ import com.xiaowang.yuminailcore.annotation.RequirePermission; import com.xiaowang.yuminailcore.common.Result; import com.xiaowang.yuminailcore.dto.ProductDTO; import com.xiaowang.yuminailcore.service.ProductService; +import com.xiaowang.yuminailcore.vo.PageResultVO; import com.xiaowang.yuminailcore.vo.ProductVO; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; @@ -61,4 +62,11 @@ public class ProductController { List productVOs = productService.listProductsByTag(tag); return Result.success(productVOs); } + + @GetMapping("/list/search") + public Result> searchProducts(@RequestParam("keyword") String keyword) { + // 调用pageProducts方法,设置默认分页参数,只返回列表 + PageResultVO pageResult = productService.pageProducts(1, 100, keyword, null); + return Result.success(pageResult.getList()); + } }