front/src/components/tab-bar/index.vue
zzh 95c71f4c44
All checks were successful
Build and Deploy Front / deploy (push) Successful in 11m56s
feat: 客户门户多页签与顶栏/侧栏体验,下单成功带单号跳转查询
客户端启用与管理端同款页签并修标题 i18n;顶栏主题色菜单坞与独立 Logo;侧栏底栏布局与选中态调整;下单成功携带 keyword 打开订单列表。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-27 10:01:06 +08:00

684 lines
17 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>
<div
class="tab-bar"
:class="{
'is-overflow-left': overflowLeft,
'is-overflow-right': overflowRight,
'has-fold': foldedTags.length > 0,
}"
>
<button
v-show="overflowLeft"
type="button"
class="tab-bar-nav tab-bar-nav--left"
title="向左滚动"
@click="scrollByDir(-1)"
>
<icon-left />
</button>
<div
ref="scrollRef"
class="tab-bar-scroll"
@wheel="onWheel"
@scroll="updateOverflow"
>
<div ref="tagsWrapRef" class="tags-wrap">
<tab-item
v-for="entry in visibleTags"
:key="entry.tag.name || entry.tag.fullPath"
:index="entry.index"
:item-data="entry.tag"
/>
</div>
</div>
<button
v-show="overflowRight"
type="button"
class="tab-bar-nav tab-bar-nav--right"
title="向右滚动"
@click="scrollByDir(1)"
>
<icon-right />
</button>
<div class="tab-bar-actions">
<a-dropdown
v-if="foldedTags.length"
trigger="click"
position="br"
:popup-max-height="320"
>
<button type="button" class="tab-bar-action" :title="`已折叠 ${foldedTags.length} 个标签`">
<icon-down />
<span class="tab-bar-action__label">更多</span>
<span class="tab-bar-action__badge">{{ foldedTags.length }}</span>
</button>
<template #content>
<div class="fold-menu">
<button
v-for="entry in foldedTags"
:key="entry.tag.name || entry.tag.fullPath"
type="button"
class="fold-menu-item"
:class="{ 'is-active': isTagActive(entry.tag) }"
@click="gotoTag(entry.tag)"
>
<span class="fold-menu-item__title" :title="entry.tag.title">
{{ entry.tag.title }}
</span>
<span
class="fold-menu-item__close"
title="关闭"
@click.stop="closeTag(entry.tag, entry.index)"
>
<icon-close />
</span>
</button>
<div class="fold-menu-divider" />
<button
type="button"
class="fold-menu-item fold-menu-item--action"
:disabled="!canCloseOthers"
@click="closeOthers"
>
关闭其他
</button>
<button
type="button"
class="fold-menu-item fold-menu-item--action"
:disabled="tagList.length <= 1"
@click="closeAll"
>
关闭全部
</button>
</div>
</template>
</a-dropdown>
<a-dropdown trigger="hover" position="br" :disabled="tagList.length <= 1">
<button
type="button"
class="tab-bar-action tab-bar-action--icon"
:disabled="!canCloseOthers"
title="关闭其他"
>
<icon-close-circle :size="18" />
</button>
<template #content>
<a-doption :disabled="!canCloseOthers" @click="closeOthers">
关闭其他
</a-doption>
<a-doption :disabled="tagList.length <= 1" @click="closeAll">
关闭全部
</a-doption>
</template>
</a-dropdown>
<button
type="button"
class="tab-bar-action tab-bar-action--icon"
:disabled="tagList.length <= 1"
title="关闭全部标签"
@click="closeAll"
>
<icon-folder-delete :size="18" />
</button>
</div>
</div>
</template>
<script lang="ts" setup>
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from 'vue';
import type { RouteLocationNormalized } from 'vue-router';
import { useRoute, useRouter } from 'vue-router';
import Sortable from 'sortablejs';
import {
IconLeft,
IconRight,
IconDown,
IconClose,
IconCloseCircle,
IconFolderDelete,
} from '@arco-design/web-vue/es/icon';
import {
listenerRouteChange,
removeRouteListener,
} from '@/utils/route-listener';
import { useTabBarStore } from '@/store';
import type { TagProps } from '@/store/modules/tab-bar/types';
import { DEFAULT_ROUTE_NAME } from '@/router/constants';
import tabItem from './tab-item.vue';
/** 顶栏最多直接展示的标签数,超出收入「更多」 */
const MAX_VISIBLE_TABS = 15;
const route = useRoute();
const router = useRouter();
const tabBarStore = useTabBarStore();
const scrollRef = ref<HTMLElement | null>(null);
const tagsWrapRef = ref<HTMLElement | null>(null);
const overflowLeft = ref(false);
const overflowRight = ref(false);
let sortableInstance: Sortable | null = null;
const tagList = computed(() => tabBarStore.getTabList);
type TabEntry = { tag: TagProps; index: number };
function stripEphemeralQuery(fullPath: string) {
return fullPath
.replace(/([?&])_r=\d+/g, '$1')
.replace(/([?&])_refresh=[^&]*/g, '$1')
.replace(/\?&/, '?')
.replace(/[?&]$/, '');
}
function isTagActive(tag: TagProps) {
const previewPath = tabBarStore.previewingPath;
if (previewPath || tabBarStore.previewingName) {
if (
previewPath &&
(tag.fullPath === previewPath ||
stripEphemeralQuery(tag.fullPath) ===
stripEphemeralQuery(previewPath))
) {
return true;
}
return (
!!tabBarStore.previewingName && tag.name === tabBarStore.previewingName
);
}
if (route.name && tag.name === String(route.name)) return true;
if (route.fullPath === tag.fullPath) return true;
return (
stripEphemeralQuery(route.fullPath) === stripEphemeralQuery(tag.fullPath)
);
}
function findActiveIndex(list: TagProps[]) {
const idx = list.findIndex((tag) => isTagActive(tag));
return idx >= 0 ? idx : 0;
}
/** 首页固定 + 当前页始终可见,其余按顺序填满 15 个 */
const splitTags = computed(() => {
const list = tagList.value;
if (list.length <= MAX_VISIBLE_TABS) {
return {
visible: list.map((tag, index) => ({ tag, index })),
folded: [] as TabEntry[],
};
}
const activeIdx = findActiveIndex(list);
const reserved = new Set<number>([0]);
if (activeIdx > 0) reserved.add(activeIdx);
const indices: number[] = [...reserved];
for (let i = 1; i < list.length && indices.length < MAX_VISIBLE_TABS; i += 1) {
if (!reserved.has(i)) indices.push(i);
}
indices.sort((a, b) => a - b);
const visibleSet = new Set(indices);
return {
visible: indices.map((index) => ({ tag: list[index], index })),
folded: list
.map((tag, index) => ({ tag, index }))
.filter((entry) => !visibleSet.has(entry.index)),
};
});
const visibleTags = computed(() => splitTags.value.visible);
const foldedTags = computed(() => splitTags.value.folded);
const canCloseOthers = computed(() => {
if (tagList.value.length <= 1) return false;
const activeIdx = findActiveIndex(tagList.value);
return tagList.value.some((_, i) => i !== 0 && i !== activeIdx);
});
function updateOverflow() {
const el = scrollRef.value;
if (!el) {
overflowLeft.value = false;
overflowRight.value = false;
return;
}
const max = el.scrollWidth - el.clientWidth;
overflowLeft.value = el.scrollLeft > 2;
overflowRight.value = max > 2 && el.scrollLeft < max - 2;
}
function scrollByDir(dir: -1 | 1) {
const el = scrollRef.value;
if (!el) return;
el.scrollBy({
left: dir * Math.max(160, el.clientWidth * 0.55),
behavior: 'smooth',
});
}
function onWheel(e: WheelEvent) {
const el = scrollRef.value;
if (!el) return;
if (Math.abs(e.deltaY) <= Math.abs(e.deltaX)) return;
const max = el.scrollWidth - el.clientWidth;
if (max <= 0) return;
e.preventDefault();
el.scrollLeft += e.deltaY;
updateOverflow();
}
function scrollActiveTagIntoView() {
const wrap = scrollRef.value;
if (!wrap) return;
const active = wrap.querySelector('.link-activated') as HTMLElement | null;
if (!active) return;
const wrapRect = wrap.getBoundingClientRect();
const tagRect = active.getBoundingClientRect();
if (tagRect.left < wrapRect.left + 8) {
wrap.scrollBy({
left: tagRect.left - wrapRect.left - 24,
behavior: 'smooth',
});
} else if (tagRect.right > wrapRect.right - 8) {
wrap.scrollBy({
left: tagRect.right - wrapRect.right + 24,
behavior: 'smooth',
});
}
updateOverflow();
}
function gotoTag(tag: TagProps) {
if (isTagActive(tag)) return;
tabBarStore.previewNavigate(tag);
if (tag.name) {
router.push({ name: tag.name, query: tag.query });
return;
}
router.push(tag.fullPath);
}
function closeTag(tag: TagProps, idx: number) {
const wasActive = isTagActive(tag);
tabBarStore.deleteTag(idx, tag);
if (wasActive) {
const latest = tagList.value[Math.max(0, idx - 1)];
if (latest?.name) {
router.push({ name: latest.name, query: latest.query });
}
}
}
function closeAll() {
if (tagList.value.length <= 1) return;
tabBarStore.resetTabList();
router.push({ name: DEFAULT_ROUTE_NAME });
}
function closeOthers() {
if (!canCloseOthers.value) return;
const list = tagList.value;
const activeIdx = findActiveIndex(list);
const keep = list.filter((_, i) => i === 0 || i === activeIdx);
tabBarStore.freshTabList(keep);
const current = keep[keep.length - 1];
if (current?.name && !isTagActive(current)) {
router.push({ name: current.name, query: current.query });
}
}
watch(
() => route.fullPath,
async () => {
tabBarStore.clearPreview();
await nextTick();
scrollActiveTagIntoView();
},
);
watch(
() => [tagList.value.length, visibleTags.value.length] as const,
async () => {
await nextTick();
updateOverflow();
},
);
listenerRouteChange((nextRoute: RouteLocationNormalized) => {
if (nextRoute.query?._refresh || nextRoute.query?._r) {
return;
}
// 登录/官网等独立壳不进页签;具体过滤在 updateTabList
tabBarStore.updateTabList(nextRoute);
nextTick(() => scrollActiveTagIntoView());
}, true);
function onWindowResize() {
updateOverflow();
}
function initSortable() {
sortableInstance?.destroy();
sortableInstance = null;
if (!tagsWrapRef.value) return;
sortableInstance = new Sortable(tagsWrapRef.value, {
animation: 160,
filter: '.arco-tag-close-btn, .arco-tag-refresh-btn',
preventOnFilter: true,
onEnd(evt) {
const { oldIndex, newIndex } = evt;
if (oldIndex == null || newIndex == null || oldIndex === newIndex) {
return;
}
const fromReal = visibleTags.value[oldIndex]?.index;
const toReal = visibleTags.value[newIndex]?.index;
if (fromReal == null || toReal == null || fromReal === toReal) return;
tabBarStore.reorderTabs(fromReal, toReal);
},
});
}
onMounted(() => {
// 客户门户无 TopNav 时也要翻一次标题(DEFAULT_ROUTE 初始存的是 locale key)
tabBarStore.refreshTagTitles();
initSortable();
window.addEventListener('resize', onWindowResize, { passive: true });
nextTick(() => {
scrollActiveTagIntoView();
updateOverflow();
});
});
watch(
() => visibleTags.value.map((e) => e.tag.name || e.tag.fullPath).join('|'),
async () => {
await nextTick();
initSortable();
},
);
onUnmounted(() => {
sortableInstance?.destroy();
sortableInstance = null;
window.removeEventListener('resize', onWindowResize);
removeRouteListener();
});
</script>
<style lang="less" scoped>
.tab-bar {
position: relative;
height: 100%;
display: flex;
align-items: center;
gap: 2px;
/* 与菜单坞内文字起点对齐:12 + 4 + 8(logo pad) + 32(logo) + 6 ≈ 62 */
padding: 0 12px 0 62px;
background: transparent;
&.is-overflow-left::before,
&.is-overflow-right::after {
content: '';
position: absolute;
top: 0;
bottom: 0;
width: 20px;
pointer-events: none;
z-index: 1;
}
&.is-overflow-left::before {
left: 34px;
background: linear-gradient(
90deg,
var(--app-page-bg, #f0f3f4) 10%,
transparent
);
}
&.is-overflow-right::after {
right: 72px;
background: linear-gradient(
270deg,
var(--app-page-bg, #f0f3f4) 10%,
transparent
);
}
&.has-fold.is-overflow-right::after {
right: 140px;
}
.tab-bar-nav {
flex-shrink: 0;
width: 22px;
height: 22px;
padding: 0;
border: none;
border-radius: 6px;
background: transparent;
color: var(--color-text-4);
display: inline-flex;
align-items: center;
justify-content: center;
cursor: pointer;
z-index: 2;
transition: background-color 0.15s ease, color 0.15s ease;
&:hover {
color: var(--color-text-2);
background: rgba(0, 0, 0, 0.04);
}
}
.tab-bar-scroll {
flex: 0 1 auto;
min-width: 0;
width: max-content;
max-width: 100%;
height: 100%;
overflow-x: auto;
overflow-y: hidden;
display: flex;
align-items: center;
scroll-behavior: auto;
&::-webkit-scrollbar {
height: 0;
}
.tags-wrap {
height: 100%;
display: flex;
align-items: center;
width: max-content;
padding: 0;
white-space: nowrap;
gap: 4px;
:deep(.arco-tag) {
display: inline-flex;
align-items: center;
margin-right: 0;
cursor: pointer;
border-radius: 8px;
height: 22px;
line-height: 22px;
padding: 0 8px;
font-size: 8px; /* 未选中:小 */
background: transparent;
border: 1px solid transparent;
color: var(--color-text-3);
box-shadow: none;
transition:
background-color 0.15s ease,
color 0.15s ease,
border-color 0.15s ease;
.tag-link {
cursor: grab;
}
&.sortable-ghost {
opacity: 0.4;
}
&.sortable-drag {
cursor: grabbing;
}
&:first-child {
.arco-tag-close-btn {
display: none;
}
}
&:not(.link-activated):hover {
background-color: rgba(0, 0, 0, 0.04);
border-color: transparent;
color: var(--color-text-1);
}
}
}
}
.tab-bar-actions {
flex-shrink: 0;
display: inline-flex;
align-items: center;
gap: 4px;
margin-left: auto;
padding-left: 6px;
border-left: none;
}
.tab-bar-action {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 4px;
height: 24px;
min-width: 24px;
padding: 0 6px;
border: none;
border-radius: 8px;
background: transparent;
color: var(--color-text-3);
font-size: 12px;
cursor: pointer;
transition: background-color 0.15s ease, color 0.15s ease;
&:hover:not(:disabled) {
color: var(--color-text-1);
background: rgba(0, 0, 0, 0.04);
}
&:disabled {
opacity: 0.35;
cursor: not-allowed;
}
&--icon {
padding: 0;
width: 24px;
:deep(svg) {
width: 16px;
height: 16px;
}
}
&__label {
line-height: 1;
}
&__badge {
min-width: 16px;
height: 16px;
padding: 0 4px;
border-radius: 8px;
background: var(--color-fill-3);
color: var(--color-text-2);
font-size: 11px;
line-height: 16px;
text-align: center;
}
}
}
</style>
<style lang="less">
.fold-menu {
min-width: 160px;
max-width: 240px;
padding: 4px 0;
}
.fold-menu-item {
display: flex;
align-items: center;
width: 100%;
gap: 8px;
padding: 6px 10px;
border: none;
background: transparent;
color: var(--color-text-2);
font-size: 12px;
text-align: left;
cursor: pointer;
&:hover:not(:disabled) {
background: var(--color-fill-2);
}
&:disabled {
opacity: 0.4;
cursor: not-allowed;
}
&.is-active {
color: var(--app-primary-color);
background: var(--app-primary-color-light);
font-weight: 600;
}
&--action {
color: var(--color-text-3);
}
&__title {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
&__close {
flex-shrink: 0;
width: 18px;
height: 18px;
border-radius: 4px;
display: inline-flex;
align-items: center;
justify-content: center;
color: var(--color-text-3);
&:hover {
color: var(--color-text-1);
background: var(--color-fill-3);
}
}
}
.fold-menu-divider {
height: 1px;
margin: 4px 8px;
background: var(--color-border-2);
}
</style>