feat: 相关商品问题修复

This commit is contained in:
duanshuwen
2025-11-06 21:10:26 +08:00
parent 2527cc5004
commit 0bbd5a912e
2 changed files with 39 additions and 21 deletions

View File

@@ -27,8 +27,13 @@ const startX = ref(0);
const startY = ref(0); const startY = ref(0);
const moveX = ref(0); const moveX = ref(0);
const moveY = ref(0); const moveY = ref(0);
// 原始位移用于判定手势,不参与频繁样式更新以减少卡顿
const rawMoveX = ref(0);
const rawMoveY = ref(0);
const currentIndex = ref(null); const currentIndex = ref(null);
const isHorizontal = ref(false); // 是否为水平滑动 const isHorizontal = ref(false); // 是否为水平滑动
// 简易节流:避免每次 touchmove 都触发样式更新
let dragTicking = false;
const visibleCards = computed(() => cards.value.slice(0, 3)); const visibleCards = computed(() => cards.value.slice(0, 3));
@@ -43,15 +48,15 @@ function touchStart(e, index) {
function touchMove(e, index) { function touchMove(e, index) {
if (currentIndex.value !== index) return; if (currentIndex.value !== index) return;
const touch = e.touches[0]; const touch = e.touches[0];
moveX.value = touch.clientX - startX.value; rawMoveX.value = touch.clientX - startX.value;
moveY.value = touch.clientY - startY.value; rawMoveY.value = touch.clientY - startY.value;
// 第一次移动时判断方向 // 第一次移动时判断方向
if ( if (
!isHorizontal.value && !isHorizontal.value &&
(Math.abs(moveX.value) > 10 || Math.abs(moveY.value) > 10) (Math.abs(rawMoveX.value) > 10 || Math.abs(rawMoveY.value) > 10)
) { ) {
if (Math.abs(moveX.value) > Math.abs(moveY.value)) { if (Math.abs(rawMoveX.value) > Math.abs(rawMoveY.value)) {
isHorizontal.value = true; isHorizontal.value = true;
} }
} }
@@ -59,19 +64,32 @@ function touchMove(e, index) {
// 如果是水平滑动,阻止页面滚动 // 如果是水平滑动,阻止页面滚动
if (isHorizontal.value) { if (isHorizontal.value) {
e.preventDefault?.(); e.preventDefault?.();
// 将样式更新按 16ms 节流,降低渲染压力
if (!dragTicking) {
dragTicking = true;
setTimeout(() => {
moveX.value = rawMoveX.value;
moveY.value = rawMoveY.value;
dragTicking = false;
}, 16);
}
} }
} }
function touchEnd(e, index) { function touchEnd(e, index) {
if (!isHorizontal.value) return; // 如果不是水平滑动,不处理 if (!isHorizontal.value) return; // 如果不是水平滑动,不处理
if (Math.abs(moveX.value) > props.threshold) { const deltaX = Math.abs(rawMoveX.value);
const direction = moveX.value > 0 ? 1 : -1; const deltaY = Math.abs(rawMoveY.value);
if (deltaX > props.threshold && deltaX >= deltaY) {
const direction = rawMoveX.value > 0 ? 1 : -1;
animateSwipe(index, direction); animateSwipe(index, direction);
} else { } else {
resetCard(index); resetCard(index);
} }
moveX.value = 0; moveX.value = 0;
moveY.value = 0; moveY.value = 0;
rawMoveX.value = 0;
rawMoveY.value = 0;
currentIndex.value = null; currentIndex.value = null;
} }
@@ -108,7 +126,7 @@ function cardStyle(index, card) {
: `translate(0, ${translateY}rpx)`; : `translate(0, ${translateY}rpx)`;
const rotate = const rotate =
currentIndex.value === index currentIndex.value === index
? `rotate(${moveX.value / 15}deg)` ? `rotate(${moveX.value / 24}deg)`
: "rotate(0)"; : "rotate(0)";
let opacity = 1; let opacity = 1;
@@ -129,7 +147,11 @@ function cardStyle(index, card) {
transform: ${transform}; transform: ${transform};
opacity: ${opacity}; opacity: ${opacity};
transition: ${ transition: ${
card.swiped ? "all 0.3s ease-out" : "transform 0.2s ease-out" card.swiped
? "all 0.3s ease-out"
: currentIndex.value === index && isHorizontal.value
? "none"
: "transform 0.2s ease-out"
}; };
`; `;
} }

View File

@@ -4,14 +4,17 @@
<SwipeCards :cardsData="commodityList"> <SwipeCards :cardsData="commodityList">
<template #default="{ card }"> <template #default="{ card }">
<view class="inner-card" @click="placeOrderHandle(card)"> <view
class="inner-card overflow-hidden"
@click="placeOrderHandle(card)"
>
<!-- 商品大图部分自适应剩余空间 --> <!-- 商品大图部分自适应剩余空间 -->
<view class="goods-image-wrapper"> <view class="goods-image-wrapper">
<image <image
class="goods-image" class="goods-image"
:src="card.commodityPhoto" :src="card.commodityPhoto"
mode="aspectFill" mode="aspectFill"
></image> />
<view class="goods-title"> <view class="goods-title">
<text class="goods-text">{{ card.commodityName }}</text> <text class="goods-text">{{ card.commodityName }}</text>
<view class="card-price-row"> <view class="card-price-row">
@@ -27,17 +30,17 @@
<!-- 底部相册部分固定比例或高度 --> <!-- 底部相册部分固定比例或高度 -->
<view class="album-row"> <view class="album-row">
<view <view
v-for="(item, index) in normalizedAlbums(commodityList)" v-for="(item, index) in card.commodityPhotoList"
:key="index" :key="index"
class="album-item" class="album-item"
> >
<view v-if="item" class="album-image-wrapper"> <view v-if="item" class="album-image-wrapper">
<image <image
class="album-image" class="album-image"
:src="item.commodityPhoto" :src="item.photoUrl"
mode="aspectFill" mode="aspectFill"
/> />
<view class="album-title">{{ item.commodityName }}</view> <view class="album-title">{{ item.photoName }}</view>
</view> </view>
</view> </view>
</view> </view>
@@ -48,7 +51,7 @@
</template> </template>
<script setup> <script setup>
import { defineProps } from "vue"; import { defineProps, computed } from "vue";
import { checkToken } from "@/hooks/useGoLogin"; import { checkToken } from "@/hooks/useGoLogin";
import ModuleTitle from "@/components/ModuleTitle/index.vue"; import ModuleTitle from "@/components/ModuleTitle/index.vue";
import SwipeCards from "@/components/SwipeCards/index.vue"; import SwipeCards from "@/components/SwipeCards/index.vue";
@@ -60,13 +63,6 @@ const props = defineProps({
}, },
}); });
// 补齐3个的
const normalizedAlbums = (list = []) => {
const arr = list.slice(0, 3);
while (arr.length < 3) arr.push(null);
return arr;
};
// 去下单 // 去下单
const placeOrderHandle = (item) => { const placeOrderHandle = (item) => {
checkToken().then(() => { checkToken().then(() => {