From c43ab6a1848fd1f6f53fa98f6d3bd9ab309f819e Mon Sep 17 00:00:00 2001 From: dzq Date: Thu, 26 Jun 2025 17:47:58 +0800 Subject: [PATCH] =?UTF-8?q?refactor(product):=20=E5=B0=86=E5=95=86?= =?UTF-8?q?=E5=93=81=E5=88=97=E8=A1=A8=E7=BB=84=E4=BB=B6=E6=8B=86=E5=88=86?= =?UTF-8?q?=E4=B8=BA=E7=8B=AC=E7=AB=8B=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将ProductList.vue中的商品展示和购物车逻辑提取到新的ProductContainer组件中,提升代码可维护性 --- src/pages/product/ProductList.vue | 173 +------- .../product/components/ProductContainer.vue | 397 ++++++++++++++++++ 2 files changed, 406 insertions(+), 164 deletions(-) create mode 100644 src/pages/product/components/ProductContainer.vue diff --git a/src/pages/product/ProductList.vue b/src/pages/product/ProductList.vue index baf9339..952371b 100644 --- a/src/pages/product/ProductList.vue +++ b/src/pages/product/ProductList.vue @@ -13,6 +13,7 @@ import { useWxStore } from "@/pinia/stores/wx" import { bindQyUserApi } from "@/common/apis/ab98" import { getShopListApi } from "@/common/apis/shop" import { ShopEntity } from "@/common/apis/shop/type" +import ProductContainer from "./components/ProductContainer.vue" const router = useRouter() const route = useRoute() @@ -20,13 +21,10 @@ const route = useRoute() const productStore = useProductStore(); const cartStore = useCartStore(); const wxStore = useWxStore(); -const { cartItems, totalPrice, totalQuantity } = storeToRefs(cartStore); // 新增购物车状态 -// 从 store 解构分类标签和商品数据 -const { labels, categories } = storeToRefs(productStore); + const { openid, corpidLogin, ab98User, qyUserId } = storeToRefs(wxStore); -// 当前选中的分类索引 -const activeCategory = ref(0) + // 滚动容器引用 const scrollContainer = ref() // 顶部头图高度(随滚动变化) @@ -40,19 +38,6 @@ const showShopList = ref(true); const shopList = ref([]); const shopId = ref(0); -// 商品详情弹窗控制 -const showDetailPopup = ref(false) -// 当前查看的商品ID -const currentProductId = ref() -// 当前商品详情计算属性 -const currentProduct = computed(() => - categories.value.find(p => p.id === currentProductId.value) -) -// 购物车弹窗控制 -const showCartPopup = ref(false) - -const searchQuery = ref(''); - const name = ref(''); const idNum = ref(''); const showAb98BindPopup = ref(false); @@ -64,77 +49,8 @@ function handleShopSelect(selectedShopId: number) { productStore.setSelectedShop(shopList.value.find(shop => shop.shopId === selectedShopId)!); productStore.getGoods(selectedShopId); cartStore.clearCart(); - activeCategory.value = 0; -} -function handleBackToShopList() { - showShopList.value = true; - shopId.value = 0; - cartStore.clearCart(); } -function handleCategoryClick(index: number) { - activeCategory.value = index -} - -// 节流的滚动位置计算(用于切换左侧导航激活状态) -/* const throttledUpdate = throttle(() => { - if (!scrollContainer.value || !categoryRefs.value.length) return - - // 添加数组元素存在性检查 - const validRefs = categoryRefs.value.filter(Boolean) - if (!validRefs.length) return - - const containerTop = scrollContainer.value.getBoundingClientRect().top - const offsets = validRefs.map((el) => { - return el.getBoundingClientRect().top - containerTop - }) - let activeIndex = 0 - for (let i = offsets.length - 1; i >= 0; i--) { - if (offsets[i] < 100) { - activeIndex = i - break - } - } - // activeCategory.value = activeIndex -}, 100) */ - -// 节流的头部高度计算(实现顶部图片缩放效果) -const throttledScroll = throttle(() => { - if (!scrollContainer.value) return - const scrollTop = scrollContainer.value.scrollTop - headerHeight.value = Math.max(150 - scrollTop * 0.5, 60) -}, 100) - -// 打开商品详情弹窗 -function showProductDetail(productId: number) { - currentProductId.value = productId - showDetailPopup.value = true -} - -function handleAddToCart(product: Product) { - cartStore.addToCart(product) -} - -function handleRemoveFromCart(product: Product) { - cartStore.removeFromCart(product.cellId) -} - -function getCartItemCount(cellId: number) { - const item = cartItems.value.find(item => item.product.cellId === cellId) - return item ? item.quantity : 0 -} - -function filterProductsByName(products: Product[], query: string) { - if (!query) return products - return products.filter(p => - p.name.toLowerCase().includes(query.toLowerCase()) - ) -} - -const currentProducts = computed(() => { - const filteredByCategory = categories.value.filter(c => c.label === labels.value[activeCategory.value].id) - return filterProductsByName(filteredByCategory, searchQuery.value) -}) // 组件挂载时添加滚动监听 onMounted(() => { @@ -153,7 +69,7 @@ onMounted(() => { } else { productStore.getGoods(shopId.value); } - scrollListener.push(scrollContainer.value?.addEventListener("scroll", throttledScroll)) + // scrollListener.push(scrollContainer.value?.addEventListener("scroll", throttledScroll)) // scrollListener.push(scrollContainer.value?.addEventListener("scroll", throttledUpdate)) }) @@ -241,83 +157,11 @@ async function handleAb98Bind() {
-
- -
- 重选地址 - - - -
- - -
- -
- - -
-
- {{ product.name }} -
-
- ¥{{ product.price.toFixed(2) }} -
-
- - 还剩{{ product.stock }}份 - -
- - {{ getCartItemCount(product.cellId) - }} - -
-
-
-
-
-
- -
-
- - - -
- 合计:¥{{ totalPrice.toFixed(2) }} -
-
- - 去结算 - -
-
+ - - - - - -
请绑定汇邦云账号
@@ -601,6 +445,7 @@ async function handleAb98Bind() { justify-content: start; padding: 12px; } + .shop-cover-img { width: 100%; height: 80px; diff --git a/src/pages/product/components/ProductContainer.vue b/src/pages/product/components/ProductContainer.vue new file mode 100644 index 0000000..d2e1546 --- /dev/null +++ b/src/pages/product/components/ProductContainer.vue @@ -0,0 +1,397 @@ + + + + + +