Files
cz-web/src/views/home/cpns/HomeAppDialog.vue
wangzzpeng 6eb2bfcbfd first1
2026-09-28 09:27:51 +08:00

318 lines
8.5 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.
<script setup lang="ts">
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { useHomeStore } from '@/stores/modules/home'
import type { PortalEntryCard, PortalEntryGroup } from '@/types/business/home'
import { toPortalEntryCards } from '@/utils/entryIcon'
/**
* 「更多应用」弹窗:快捷入口 / 其他入口 两组,图标角标可把入口在两组之间互转。
*
* 数据来自 store.quickEntries / store.otherEntries(← /vermins/portalEntryUser/list)。
* 接口不可用(游客、无权限)时回退到内置默认入口,保证弹窗不空。
*/
const visible = defineModel<boolean>('visible', { default: false })
const homeStore = useHomeStore()
const router = useRouter()
/** 接口不可用时的默认快捷入口(与首页卡片一致的固定模块) */
const FALLBACK_QUICK: Array<Pick<PortalEntryCard, 'label' | 'route' | 'icon' | 'tone'>> = [
{ label: '财务台账', route: '/finance/bills', icon: '💰', tone: 'amber' },
{ label: '合同台账', route: '/contract', icon: '📄', tone: 'blue' },
{ label: '客服工作台', route: '/service', icon: '💬', tone: 'green' },
{ label: '案例库管理', route: '/cases', icon: '📁', tone: 'purple' },
{ label: '工单需求池', route: '/demand', icon: '📋', tone: 'cyan' },
]
/** 前端已注册的静态路由集合(用于判断入口 route 是否可跳转) */
const knownPaths = computed(() => new Set(router.getRoutes().map((item) => item.path)))
/** 快捷入口组 */
const quickCards = computed<PortalEntryCard[]>(() => {
const list = toPortalEntryCards(homeStore.quickEntries)
if (list.length > 0) return list
return FALLBACK_QUICK.map((item) => ({
...item,
id: 0,
groupType: '1' as PortalEntryGroup,
isImage: false,
}))
})
/** 其他入口组 */
const otherCards = computed<PortalEntryCard[]>(() => toPortalEntryCards(homeStore.otherEntries))
/** 入口是否可点击跳转(route 为空或未注册时不跳,避免落到 404) */
function canOpen(card: PortalEntryCard): boolean {
return card.route.length > 0 && knownPaths.value.has(card.route)
}
/** 是否是后端下发的入口(默认兜底入口 id 为 0,不可切换分组) */
function switchable(card: PortalEntryCard): boolean {
return card.id > 0
}
/** 点击图标:跳转对应路由 */
function onOpen(card: PortalEntryCard): void {
if (!canOpen(card)) return
visible.value = false
void router.push(card.route)
}
/** 点击角标:快捷 ⇄ 其他 互转 */
async function onToggle(card: PortalEntryCard): Promise<void> {
if (!switchable(card)) return
const target: PortalEntryGroup = card.groupType === '1' ? '0' : '1'
try {
await homeStore.switchEntryGroup(card.id, target)
} catch {
// 失败提示已由请求层统一弹出,这里只保证不产生未捕获的 rejection
}
}
function onClose(): void {
visible.value = false
}
</script>
<template>
<Teleport to="body">
<div v-if="visible" class="home-app-dialog" @click.self="onClose">
<div class="home-app-dialog__panel">
<div class="home-app-dialog__body">
<section class="home-app-dialog__group">
<h4 class="home-app-dialog__group-title">
快捷入口
</h4>
<div class="home-app-dialog__grid">
<div
v-for="card in quickCards"
:key="`quick-${card.label}-${card.id}`"
class="home-app-dialog__item"
:class="{ 'is-link': canOpen(card) }"
>
<button
class="home-app-dialog__icon"
:class="`home-app-dialog__icon--${card.tone}`"
type="button"
@click="onOpen(card)"
>
<img v-if="card.isImage" :src="card.icon" :alt="card.label" class="home-app-dialog__img">
<span v-else>{{ card.icon }}</span>
</button>
<button
v-if="switchable(card)"
class="home-app-dialog__badge home-app-dialog__badge--remove"
type="button"
:aria-label="`将${card.label}移出快捷入口`"
@click.stop="onToggle(card)"
>
−
</button>
<span class="home-app-dialog__label">{{ card.label }}</span>
</div>
</div>
</section>
<section v-if="otherCards.length > 0" class="home-app-dialog__group">
<h4 class="home-app-dialog__group-title">
其他入口
</h4>
<div class="home-app-dialog__grid">
<div
v-for="card in otherCards"
:key="`other-${card.label}-${card.id}`"
class="home-app-dialog__item"
:class="{ 'is-link': canOpen(card) }"
>
<button
class="home-app-dialog__icon"
:class="`home-app-dialog__icon--${card.tone}`"
type="button"
@click="onOpen(card)"
>
<img v-if="card.isImage" :src="card.icon" :alt="card.label" class="home-app-dialog__img">
<span v-else>{{ card.icon }}</span>
</button>
<button
class="home-app-dialog__badge home-app-dialog__badge--add"
type="button"
:aria-label="`将${card.label}加入快捷入口`"
@click.stop="onToggle(card)"
>
+
</button>
<span class="home-app-dialog__label">{{ card.label }}</span>
</div>
</div>
</section>
</div>
</div>
</div>
</Teleport>
</template>
<style lang="less" scoped>
.home-app-dialog {
position: fixed;
inset: 0;
z-index: 1000;
.flex(row, center);
padding: @spacing-base;
background: rgba(0, 0, 0, 0.45);
&__panel {
.flex(column, flex-start, stretch);
width: 560px;
max-width: calc(100vw - 32px);
max-height: calc(100vh - 64px);
padding: @spacing-lg;
border-radius: @radius-lg;
background: @white;
box-shadow: @shadow-lg;
}
&__head {
.flex(row, space-between, center);
margin-bottom: @spacing-base;
}
&__title {
font-size: @font-size-lg;
font-weight: 600;
color: @heading-color;
}
&__close {
.flex(row, center);
width: 24px;
height: 24px;
border: 0;
border-radius: @radius-sm;
background: transparent;
color: @text-color-secondary;
font-size: @font-size-lg;
line-height: 1;
cursor: pointer;
transition: all 0.2s;
&:hover {
background: @bg-color;
color: @heading-color;
}
}
&__body {
overflow-y: auto;
}
&__group {
& + & {
margin-top: @spacing-lg;
}
}
&__group-title {
margin-bottom: @spacing-base;
font-size: @font-size-base;
font-weight: 600;
color: @heading-color;
}
&__grid {
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: @spacing-base @spacing-sm;
}
&__item {
position: relative;
.flex(column, flex-start, center);
gap: @spacing-sm;
}
&__icon {
.flex(row, center);
width: 44px;
height: 44px;
border: 0;
border-radius: @radius-base;
font-size: @font-size-xl;
cursor: pointer;
transition: transform 0.2s, box-shadow 0.2s;
&:hover {
transform: translateY(-2px);
box-shadow: @shadow-base;
}
&--purple {
background: #f0eaff;
}
&--green {
background: #e7f8ec;
}
&--blue {
background: @primary-light;
}
&--amber {
background: #fff3e0;
}
&--red {
background: #ffeceb;
}
&--cyan {
background: #e4f6fa;
}
}
&__img {
width: 24px;
height: 24px;
object-fit: contain;
}
&__badge {
position: absolute;
top: -6px;
right: 50%;
margin-right: -28px;
.flex(row, center);
width: 16px;
height: 16px;
border: 1px solid @border-color;
border-radius: @radius-round;
background: @white;
color: @text-color-secondary;
font-size: @font-size-sm;
line-height: 1;
cursor: pointer;
transition: all 0.2s;
&:hover {
border-color: @primary-color;
color: @primary-color;
}
&--remove:hover {
border-color: @danger-color;
color: @danger-color;
}
}
&__label {
font-size: @font-size-sm;
color: @text-color;
text-align: center;
line-height: 1.4;
}
}
</style>