front/src/views/site/SiteFeaturedRoutes.vue
zzh 40fe09b62f
All checks were successful
Build and Deploy Front / deploy (push) Successful in 3m25s
feat: 销售产品官网封面上传、精选线路 Coverflow 与封面卡文案布局
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-26 14:10:06 +08:00

489 lines
15 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<section
v-if="visible"
class="ai-featured-routes"
:aria-label="copy.featuredRoutesAria"
>
<div class="ai-featured-routes__head">
<div class="ai-featured-routes__heading">
<h3 class="ai-featured-routes__title">{{ copy.featuredRoutesTitle }}</h3>
</div>
<div class="ai-featured-routes__actions">
<div v-if="!loading && featured.length > 1" class="ai-featured-routes__nav">
<button
type="button"
class="ai-featured-routes__nav-btn"
:aria-label="copy.featuredRoutesPrev"
:disabled="busy || !canPrev"
@click="step(-1)"
>
<span aria-hidden="true">‹</span>
</button>
<button
type="button"
class="ai-featured-routes__nav-btn"
:aria-label="copy.featuredRoutesNext"
:disabled="busy || !canNext"
@click="step(1)"
>
<span aria-hidden="true">›</span>
</button>
</div>
<button
type="button"
class="ai-featured-routes__all"
:disabled="busy"
@click="emit('open-routes')"
>
{{ copy.featuredRoutesAll }}
<span aria-hidden="true">→</span>
</button>
</div>
</div>
<div
class="ai-featured-routes__rail"
@keydown="onKeydown"
@mouseenter="paused = true"
@mouseleave="paused = false"
@focusin="paused = true"
@focusout="onRailFocusOut"
>
<div
ref="stageRef"
class="ai-featured-routes__stage"
role="list"
tabindex="0"
:aria-label="copy.featuredRoutesTitle"
@pointerdown="onPointerDown"
@pointermove="onPointerMove"
@pointerup="onPointerUp"
@pointercancel="onPointerUp"
>
<template v-if="loading">
<div
v-for="n in 7"
:key="'skel-' + n"
class="ai-featured-routes__item is-skel"
:style="coverStyle(n - 4)"
aria-hidden="true"
>
<div class="ai-featured-routes__skel" />
</div>
</template>
<template v-else>
<div
v-for="(item, index) in featured"
:key="String(item.id)"
class="ai-featured-routes__item"
:class="{ 'is-active': index === activeIndex }"
role="listitem"
:style="coverStyle(circularOffset(index, activeIndex))"
>
<button
type="button"
class="ai-featured-routes__card"
:class="{ 'has-cover': routeHasRealCover(item) }"
:disabled="busy"
:tabindex="index === activeIndex ? 0 : -1"
:aria-current="index === activeIndex ? 'true' : undefined"
:aria-label="cardAria(item)"
:style="cardBgStyle(item)"
@click="onCard(item, index)"
>
<span class="ai-featured-routes__card-top">
<span
v-if="!routeHasRealCover(item)"
class="ai-featured-routes__dest"
>{{ routeCoverageLabel(item, lang) }}</span>
<span
v-else
class="ai-featured-routes__top-spacer"
aria-hidden="true"
/>
<span v-if="formatRouteTimeliness(item)" class="ai-featured-routes__eta">
{{ formatRouteTimeliness(item) }}
<em>{{ routesCopy.days }}</em>
</span>
</span>
<span class="ai-featured-routes__card-mid">
<strong class="ai-featured-routes__name">{{ item.productName }}</strong>
</span>
<span class="ai-featured-routes__card-foot">
<!-- 有封面:只留 1 个分组 chip;无封面:条款 + 分组 + 简述 -->
<span
v-if="routeHasRealCover(item) && (item.productGroupName || item.productGroup)"
class="ai-featured-routes__meta"
>
<span class="ai-featured-routes__chip">{{
item.productGroupName || item.productGroup
}}</span>
</span>
<span
v-else-if="!routeHasRealCover(item) && (item.tradeTerms || item.productGroupName || item.productGroup)"
class="ai-featured-routes__meta"
>
<template v-if="item.tradeTerms">
<span class="ai-featured-routes__chip">{{ item.tradeTerms }}</span>
</template>
<template v-if="item.productGroupName || item.productGroup">
<span class="ai-featured-routes__chip">{{
item.productGroupName || item.productGroup
}}</span>
</template>
</span>
<p
v-if="item.productDescription && !routeHasRealCover(item)"
class="ai-featured-routes__desc"
>
{{ item.productDescription }}
</p>
</span>
</button>
</div>
</template>
</div>
<div
v-if="!loading && featured.length > 1"
class="ai-featured-routes__dots"
role="tablist"
:aria-label="copy.featuredRoutesTitle"
>
<button
v-for="(item, index) in featured"
:key="'dot-' + String(item.id)"
type="button"
class="ai-featured-routes__dot"
:class="{ 'is-active': index === activeIndex }"
role="tab"
:aria-selected="index === activeIndex"
:aria-label="item.productName"
:disabled="busy"
@click="goTo(index)"
/>
</div>
</div>
</section>
</template>
<script lang="ts" setup>
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue';
import { WELCOME_COPY } from './lib/site-welcome';
import {
ROUTES_COPY,
buildRouteAskText,
fetchRoutesCatalog,
formatRouteTimeliness,
inferRouteDestIds,
pickFeaturedRoutes,
routeCoverageLabel,
type RouteItem,
} from './lib/portal-catalog';
import { resolveRouteCoverUrl, routeHasRealCover } from './lib/route-cover';
const AUTOPLAY_MS = 4500;
const props = defineProps<{
isZh: boolean;
busy?: boolean;
}>();
const emit = defineEmits<{
'open-routes': [dest?: string];
ask: [payload: { text: string; intentId: string }];
}>();
const lang = computed(() => (props.isZh ? 'zh' : 'en') as const);
const copy = computed(() => (props.isZh ? WELCOME_COPY.zh : WELCOME_COPY.en));
const routesCopy = computed(() => ROUTES_COPY[lang.value]);
const loading = ref(true);
const featured = ref<RouteItem[]>([]);
const activeIndex = ref(0);
const stageRef = ref<HTMLElement | null>(null);
const reducedMotion = ref(false);
const paused = ref(false);
const stageWidth = ref(1100);
const cardWidth = ref(280);
const visible = computed(() => loading.value || featured.value.length > 0);
const canPrev = computed(() => featured.value.length > 1);
const canNext = computed(() => featured.value.length > 1);
const sideSlots = computed(() => {
const w = Math.max(stageWidth.value, 320);
if (w >= 1100) return 3;
if (w >= 720) return 2;
return 2;
});
const reachPx = computed(() => {
const half = stageWidth.value / 2;
return Math.max(half - cardWidth.value * 0.1, cardWidth.value * 1.1);
});
const pitchPx = computed(() => {
const slots = sideSlots.value;
const compress = 0.72;
const denom = 1 + compress * Math.max(0, slots - 1);
return Math.max(reachPx.value / denom, cardWidth.value * 0.4);
});
function cardBgStyle(item: RouteItem) {
const url = resolveRouteCoverUrl(item);
return url ? { backgroundImage: `url(${url})` } : undefined;
}
/** 环形最短偏移:末张右侧接首张,首张左侧接末张 */
function circularOffset(index: number, active: number) {
const len = featured.value.length;
if (len <= 0) return 0;
let d = ((index - active) % len + len) % len;
if (d > len / 2) d -= len;
return d;
}
function coverStyle(offset: number) {
const abs = Math.abs(offset);
const sign = Math.sign(offset) || 0;
const flat = reducedMotion.value;
const slots = sideSlots.value;
const pitch = pitchPx.value;
const cw = cardWidth.value;
const compress = 0.72;
// 位移取整,避免亚像素缩放把文字糊掉
let x = 0;
if (abs > 0) {
x = Math.round(sign * (pitch + Math.max(0, abs - 1) * pitch * compress));
}
if (flat) {
return {
transform: `translate(-50%, -50%) translate3d(${x}px, 0, 0)`,
transformOrigin: '50% 50%',
zIndex: String(100 - abs),
opacity: abs > slots ? '0' : abs === 0 ? '1' : String(Math.max(0.55, 1 - abs * 0.16)),
pointerEvents: abs > slots ? ('none' as const) : ('auto' as const),
['--cover-dim' as string]: String(Math.min(0.4, abs * 0.14)),
};
}
const rotateY = abs === 0 ? 0 : sign * -58;
const xClear = abs === 0 ? 0 : Math.round(sign * cw * 0.16);
// 中心卡不做 scale / 少用 translateZ,否则字体会发糊
const z = abs === 0 ? 0 : -Math.round(abs * (cw * 0.55 + 20));
const opacity = abs > slots ? 0 : abs === 0 ? 1 : Math.max(0.5, 1 - abs * 0.1);
return {
transform:
abs === 0
? `translate(-50%, -50%) translate3d(0, 0, 0)`
: `translate(-50%, -50%) translate3d(${x + xClear}px, 0, ${z}px) rotateY(${rotateY}deg)`,
transformOrigin: '50% 50%',
zIndex: String(100 - abs),
opacity: String(opacity),
pointerEvents: abs > slots ? ('none' as const) : ('auto' as const),
['--cover-dim' as string]: String(abs === 0 ? 0 : Math.min(0.36, 0.1 + abs * 0.08)),
};
}
function measureStage() {
const el = stageRef.value;
if (!el) return;
stageWidth.value = Math.max(el.clientWidth, 320);
const card = el.querySelector('.ai-featured-routes__item') as HTMLElement | null;
if (card && card.offsetWidth > 40) cardWidth.value = card.offsetWidth;
}
function cardAria(item: RouteItem) {
const eta = formatRouteTimeliness(item);
return [
item.productName,
routeCoverageLabel(item, lang.value),
eta ? eta + ' ' + routesCopy.value.days : '',
copy.value.featuredRoutesAsk,
]
.filter(Boolean)
.join(',');
}
function goTo(index: number) {
const len = featured.value.length;
if (len <= 0) return;
activeIndex.value = ((index % len) + len) % len;
bumpAutoplay();
}
function step(dir: -1 | 1) {
goTo(activeIndex.value + dir);
}
function onCard(item: RouteItem, index: number) {
if (suppressClick) {
suppressClick = false;
return;
}
if (index !== activeIndex.value) {
goTo(index);
return;
}
const destId = inferRouteDestIds(item).find((id) => id !== 'other') || 'all';
emit('ask', {
text: buildRouteAskText(item, destId, lang.value),
intentId: 'quote',
});
}
function onKeydown(event: KeyboardEvent) {
if (event.key === 'ArrowLeft') {
event.preventDefault();
step(-1);
} else if (event.key === 'ArrowRight') {
event.preventDefault();
step(1);
} else if (event.key === 'Enter' || event.key === ' ') {
const item = featured.value[activeIndex.value];
if (!item || props.busy) return;
event.preventDefault();
onCard(item, activeIndex.value);
}
}
function onRailFocusOut(event: FocusEvent) {
const root = event.currentTarget as HTMLElement | null;
const next = event.relatedTarget as Node | null;
if (root && next && root.contains(next)) return;
paused.value = false;
}
/* —— 拖拽 / 轻扫 —— */
let dragStartX = 0;
let dragDelta = 0;
let dragging = false;
let suppressClick = false;
let dragPointerId: number | null = null;
function onPointerDown(event: PointerEvent) {
if (loading.value || featured.value.length <= 1) return;
if (event.button !== 0 && event.pointerType === 'mouse') return;
dragging = true;
paused.value = true;
suppressClick = false;
dragDelta = 0;
dragStartX = event.clientX;
dragPointerId = event.pointerId;
stageRef.value?.setPointerCapture?.(event.pointerId);
}
function onPointerMove(event: PointerEvent) {
if (!dragging || dragPointerId !== event.pointerId) return;
dragDelta = event.clientX - dragStartX;
}
function onPointerUp(event: PointerEvent) {
if (!dragging || (dragPointerId !== null && dragPointerId !== event.pointerId)) return;
dragging = false;
dragPointerId = null;
const threshold = 48;
if (Math.abs(dragDelta) >= threshold) {
suppressClick = true;
if (dragDelta < 0) step(1);
else step(-1);
}
dragDelta = 0;
paused.value = false;
bumpAutoplay();
}
/* —— 自动轮播 —— */
let autoplayTimer: ReturnType<typeof setInterval> | null = null;
function stopAutoplay() {
if (autoplayTimer) {
clearInterval(autoplayTimer);
autoplayTimer = null;
}
}
function startAutoplay() {
stopAutoplay();
if (reducedMotion.value || loading.value || featured.value.length <= 1) return;
autoplayTimer = setInterval(() => {
if (paused.value || props.busy || dragging) return;
step(1);
}, AUTOPLAY_MS);
}
function bumpAutoplay() {
startAutoplay();
}
async function load() {
loading.value = true;
activeIndex.value = 0;
stopAutoplay();
try {
const catalog = await fetchRoutesCatalog(lang.value);
featured.value = pickFeaturedRoutes(catalog, 10);
} catch {
featured.value = [];
} finally {
loading.value = false;
await nextTick();
const len = featured.value.length;
activeIndex.value = len > 0 ? Math.floor((len - 1) / 2) : 0;
measureStage();
startAutoplay();
}
}
let mq: MediaQueryList | null = null;
let ro: ResizeObserver | null = null;
function syncReducedMotion() {
reducedMotion.value = !!mq?.matches;
if (reducedMotion.value) stopAutoplay();
else startAutoplay();
}
onMounted(() => {
if (typeof window !== 'undefined' && window.matchMedia) {
mq = window.matchMedia('(prefers-reduced-motion: reduce)');
syncReducedMotion();
mq.addEventListener?.('change', syncReducedMotion);
}
void load();
});
onBeforeUnmount(() => {
mq?.removeEventListener?.('change', syncReducedMotion);
ro?.disconnect();
ro = null;
stopAutoplay();
});
watch(stageRef, (el) => {
ro?.disconnect();
ro = null;
if (!el || typeof ResizeObserver === 'undefined') return;
ro = new ResizeObserver(() => measureStage());
ro.observe(el);
measureStage();
});
watch(
() => props.isZh,
() => {
void load();
},
);
watch(featured, async () => {
await nextTick();
measureStage();
});
</script>