商品收藏功能
This commit is contained in:
@@ -79,5 +79,13 @@ export const productApi = {
|
||||
|
||||
unlikeProduct: (productId: number) => {
|
||||
return api.post<boolean>(`/product/unlike/${productId}`)
|
||||
},
|
||||
|
||||
searchProducts: (keyword: string) => {
|
||||
return api.get<ProductVO[]>('/product/list/search', {
|
||||
params: {
|
||||
keyword: keyword
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,17 @@
|
||||
<a-button class="back-btn" type="text" @click="handleBackClick">
|
||||
<LeftOutlined :style="{ fontSize: '20px' }" />
|
||||
</a-button>
|
||||
<h1>商品列表</h1>
|
||||
<a-button class="search-btn" type="text">
|
||||
<h1 v-if="!showSearch">商品列表</h1>
|
||||
<div v-else class="search-input-container">
|
||||
<a-input-search
|
||||
v-model:value="searchKeyword"
|
||||
placeholder="搜索商品"
|
||||
class="search-input"
|
||||
@search="handleSearch"
|
||||
enter-button
|
||||
/>
|
||||
</div>
|
||||
<a-button class="search-btn" type="text" @click="toggleSearch">
|
||||
<SearchOutlined :style="{ fontSize: '20px' }" />
|
||||
</a-button>
|
||||
</header>
|
||||
@@ -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<Record<string, string>>({})
|
||||
const imageUrls = ref<Record<number, string[]>>({})
|
||||
@@ -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;
|
||||
|
||||
@@ -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",
|
||||
|
||||
+8
@@ -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<ProductVO> productVOs = productService.listProductsByTag(tag);
|
||||
return Result.success(productVOs);
|
||||
}
|
||||
|
||||
@GetMapping("/list/search")
|
||||
public Result<List<ProductVO>> searchProducts(@RequestParam("keyword") String keyword) {
|
||||
// 调用pageProducts方法,设置默认分页参数,只返回列表
|
||||
PageResultVO<ProductVO> pageResult = productService.pageProducts(1, 100, keyword, null);
|
||||
return Result.success(pageResult.getList());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user