Files
cz-app/src/pages/home-index/index.vue
2026-11-05 15:08:55 +08:00

304 lines
8.2 KiB
Vue

<script setup lang="ts">
import { onShow } from '@dcloudio/uni-app'
import { computed, ref } from 'vue'
import { getBannerList } from '@/api/banner'
import { getProjectSubmissionList } from '@/api/project-submission'
import { getRequirementOngoing } from '@/api/requirement'
import AppTabBar from '@/components/AppTabBar.vue'
import { useGreeting } from '@/hooks/use-greeting'
import { useQuickEntries } from '@/hooks/use-quick-entries'
import type { BannerItem } from '@/types/banner'
import type { ProjectItem, QuickEntryItem, ServiceItem } from '@/types/home'
import { ensureLoggedIn } from '@/utils/auth-guard'
import { mapOngoingOverviewToHome } from '@/utils/map-ongoing-overview'
import { mapProjectSubmissionsToServices } from '@/utils/map-project-submission'
import { navigateQuickEntry } from '@/utils/quick-entry-navigate'
import headerBg from '@/static/icons/1.png'
import brandLogoIcon from '@/static/icons/brand-logo.png'
import messageIcon from '@/static/icons/message.png'
import hiIcon from '@/static/icons/hi.png'
import HomeBanner from './components/HomeBanner.vue'
import HomeFloatingActions from './components/HomeFloatingActions.vue'
import ProjectCard from './components/ProjectCard.vue'
import QuickEntryEditor from './components/QuickEntryEditor.vue'
import ServiceCard from './components/ServiceCard.vue'
const greeting = useGreeting()
const {
pinnedEntries,
otherEntries,
editorVisible,
openEditor,
closeEditor,
removeFromPinned,
addToPinned,
} = useQuickEntries()
const banners = ref<BannerItem[]>([])
const services = ref<ServiceItem[]>([])
const ongoingCount = ref(0)
const todoCount = ref(0)
const ongoingProjects = ref<ProjectItem[]>([])
async function loadBanners() {
try {
const list = await getBannerList()
banners.value = (list ?? [])
.filter(item => item.enabled === 1 || item.enabled === undefined)
.sort((a, b) => (a.sort ?? 0) - (b.sort ?? 0))
}
catch {
banners.value = []
}
}
async function loadProjectSubmissions() {
try {
const list = await getProjectSubmissionList()
services.value = await mapProjectSubmissionsToServices(list ?? [])
}
catch {
services.value = []
}
}
async function loadOngoingProjects() {
try {
const overview = await getRequirementOngoing({ limit: 5 })
const mapped = mapOngoingOverviewToHome(overview)
ongoingCount.value = mapped.ongoingCount
todoCount.value = mapped.todoCount
ongoingProjects.value = mapped.projects
}
catch {
ongoingCount.value = 0
todoCount.value = 0
ongoingProjects.value = []
}
}
onShow(() => {
if (!ensureLoggedIn())
return
loadBanners()
loadProjectSubmissions()
loadOngoingProjects()
})
function handleQuickEntryTap(entry: QuickEntryItem) {
navigateQuickEntry(entry.id, entry.label)
}
function handleViewAllProjects() {
navigateQuickEntry('project', '项目中心')
}
function handleOngoingProjectDetail(id: string) {
uni.navigateTo({
url: `/pages/project-center/detail/index?id=${encodeURIComponent(id)}`,
})
}
function handleOpenMessageCenter() {
uni.redirectTo({ url: '/pages/message-center/index' })
}
const ongoingSubtitle = computed(() => {
if (ongoingCount.value <= 0)
return '暂无进行中的项目'
return `今天有${ongoingCount.value}个项目正在进行`
})
</script>
<template>
<view class="home">
<view class="home__header">
<image
class="home__header-bg"
:src="headerBg"
mode="aspectFill"
/>
<view class="home__header-inner">
<view class="home__header-top">
<view class="home__brand">
<image
class="home__logo"
:src="brandLogoIcon"
mode="aspectFit"
/>
<text class="home__brand-name">
虫子智造研发一体化平台
</text>
</view>
<view
class="home__message-btn"
@tap="handleOpenMessageCenter"
>
<image
class="home__message-icon"
:src="messageIcon"
mode="aspectFit"
/>
</view>
</view>
<view class="home__greeting-row">
<text class="home__greeting">
{{ greeting }}
</text>
<view class="home__hi-badge">
<image
class="home__hi-icon"
:src="hiIcon"
mode="aspectFit"
/>
</view>
</view>
<text class="home__subtitle">
{{ ongoingSubtitle }}
</text>
</view>
</view>
<scroll-view
class="home__body"
scroll-y
:show-scrollbar="false"
>
<view class="home__content">
<HomeBanner :list="banners" />
<view class="home__section">
<text class="home__section-title">
业务服务
</text>
<view
v-if="services.length"
class="home__service-grid"
>
<ServiceCard
v-for="item in services"
:key="item.id"
:item="item"
/>
</view>
<text
v-else
class="home__service-empty"
>
暂无业务服务
</text>
</view>
<view class="home__sections">
<view class="home__section-header">
<text class="home__section-title">
快捷入口
</text>
<text
class="home__section-action"
@tap="openEditor"
>
编辑
</text>
</view>
<view class="home__quick-row">
<view
v-for="entry in pinnedEntries"
:key="entry.id"
class="home__quick-item"
@tap="handleQuickEntryTap(entry)"
>
<image
class="home__quick-icon"
:src="entry.icon"
mode="aspectFit"
/>
<text class="home__quick-label">
{{ entry.label }}
</text>
</view>
</view>
</view>
<view class="home__sectionss home__section--last">
<view class="home__section-header">
<text class="home__section-title">
在办项目
</text>
<text
class="home__section-action"
@tap="handleViewAllProjects"
>
查看全部
</text>
</view>
<view class="home__stats-row">
<view class="home__stat-card home__stat-card--blue">
<view class="home__stat-value">
<text class="home__stat-num">
{{ ongoingCount }}
</text>
<text class="home__stat-unit">
个项目
</text>
</view>
<text class="home__stat-label">
进行中
</text>
</view>
<view class="home__stat-card home__stat-card--orange">
<view class="home__stat-value">
<text class="home__stat-num">
{{ todoCount }}
</text>
<text class="home__stat-unit">
项待办
</text>
</view>
<text class="home__stat-label">
待您处理
</text>
</view>
</view>
<text class="home__recent-title">
最近更新
</text>
<ProjectCard
v-for="item in ongoingProjects"
:key="item.id"
:item="item"
@detail="handleOngoingProjectDetail"
/>
<text
v-if="!ongoingProjects.length"
class="home__service-empty"
>
暂无最近更新项目
</text>
</view>
</view>
</scroll-view>
<AppTabBar active="home" />
<HomeFloatingActions />
<QuickEntryEditor
:visible="editorVisible"
:pinned-entries="pinnedEntries"
:other-entries="otherEntries"
@close="closeEditor"
@remove="removeFromPinned"
@add="addToPinned"
/>
</view>
</template>
<style scoped lang="scss">
@import './index.scss';
</style>