Files
JayuCloud/YumiNail-Frontend/src/App.vue
T

299 lines
7.9 KiB
Vue
Raw Normal View History

<template>
<el-config-provider>
<ConfigProvider :locale="antdLocale">
<div class="app-container">
<BottomNav v-if="!isHiddenNav" />
<div class="page-content" :class="{ 'no-nav-padding': isHiddenNav }">
<router-view v-slot="{ Component }">
<component :is="Component" :key="route.path" />
</router-view>
</div>
</div>
</ConfigProvider>
</el-config-provider>
</template>
<script setup lang="ts">
/**
* App page script.
* Call guide: the template binds the reactive state below, and the methods are triggered by button clicks, lifecycle hooks, or router events.
*/
import { computed, h, onMounted, provide, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ConfigProvider, Modal } from 'ant-design-vue'
import BottomNav from './components/BottomNav.vue'
2026-02-28 17:09:45 +08:00
import { cartApi } from './api/cart'
import { userApi, type SystemMessageVO } from './api/user'
import antdLocale from './config/antdv-locale'
const route = useRoute()
const router = useRouter()
/** routerReady is a reactive state variable used by the template and event handlers. */
const routerReady = ref(false)
/** isHiddenNav is a reactive state variable used by the template and event handlers. */
const isHiddenNav = computed(() => {
if (!routerReady.value) {
return true
}
if (route.path === '/' || route.path.startsWith('/admin')) {
return true
}
return route.matched.some(r => r.meta.hideNav) || route.matched.some(r => r.meta.isAdmin)
})
2026-02-28 17:09:45 +08:00
// 全局购物车数量状态
/** cartCount is a reactive state variable used by the template and event handlers. */
2026-02-28 17:09:45 +08:00
const cartCount = ref(0)
/** checkingPopupMessages is a reactive state variable used by the template and event handlers. */
const checkingPopupMessages = ref(false)
2026-02-28 17:09:45 +08:00
// 加载购物车数量
/** loadCartCount is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
2026-02-28 17:09:45 +08:00
const loadCartCount = async () => {
try {
// 检查用户是否已登录
const token = localStorage.getItem('accessToken') || sessionStorage.getItem('accessToken')
if (!token) {
cartCount.value = 0
return
}
2026-03-02 10:13:20 +08:00
const response = await cartApi.getCartList() as any
const data = response || []
const cartList = Array.isArray(data) ? data : []
const totalQuantity = cartList.reduce((sum: number, item: any) => sum + (item.quantity || 0), 0)
2026-02-28 17:09:45 +08:00
cartCount.value = totalQuantity
} catch (error) {
cartCount.value = 0
2026-02-28 17:09:45 +08:00
}
}
// 提供更新购物车数量的方法
/** updateCartCount is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
2026-02-28 17:09:45 +08:00
const updateCartCount = async () => {
await loadCartCount()
}
/** showPopupMessage is a local helper used by this page script. */
const showPopupMessage = (item: SystemMessageVO) => {
return new Promise<void>((resolve) => {
let settled = false
/** markShownAndResolve is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
const markShownAndResolve = async () => {
if (settled) {
return
}
settled = true
await userApi.markMessagePopupShown(item.id)
resolve()
}
Modal.info({
title: item.title,
content: h('div', { class: 'popup-message-body' }, [
item.content ? h('div', { class: 'popup-message-content' }, item.content) : null,
item.imageUrl ? h('img', { class: 'popup-message-image', src: item.imageUrl, alt: '消息图片' }) : null
]),
okText: '我知道了',
async onOk() {
await markShownAndResolve()
},
async afterClose() {
await markShownAndResolve()
}
})
})
}
/** checkPopupMessages is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
const checkPopupMessages = async () => {
const token = localStorage.getItem('accessToken') || sessionStorage.getItem('accessToken')
const isAdminPage = route.matched.some(r => r.meta.isAdmin)
if (!token || isAdminPage || checkingPopupMessages.value) {
return
}
checkingPopupMessages.value = true
try {
const messages = await userApi.listMyPopupMessages()
for (const item of messages || []) {
await showPopupMessage(item)
}
} catch (error) {
console.error('加载待弹窗消息失败:', error)
} finally {
checkingPopupMessages.value = false
}
}
2026-02-28 17:09:45 +08:00
provide('updateCartCount', updateCartCount)
provide('cartCount', cartCount)
// 监听路由变化,更新购物车数量
watch(() => route.path, async () => {
// 检查当前是否是后台页面,如果是则不加载购物车数据
const isAdminPage = route.matched.some(r => r.meta.isAdmin)
if (!isAdminPage) {
await loadCartCount()
await checkPopupMessages()
2026-02-28 17:09:45 +08:00
}
})
// 页面加载时获取购物车数量
loadCartCount()
onMounted(async () => {
await router.isReady()
routerReady.value = true
await checkPopupMessages()
})
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
color: #2c3e50;
}
.app-container {
min-height: 100vh;
display: flex;
flex-direction: column;
position: relative;
}
.page-content {
flex: 1;
padding-bottom: 60px;
position: relative;
z-index: 1;
}
/* 当导航栏隐藏时,移除底部 padding */
.page-content.no-nav-padding {
padding-bottom: 0;
}
.yumi-date-picker-panel {
padding: 6px;
}
.yumi-date-picker-panel .ant-picker-panel-container {
overflow: hidden;
border-radius: 12px;
box-shadow: 0 18px 44px rgba(34, 40, 49, 0.18);
}
.yumi-date-picker-panel .ant-picker-panel {
border: 0;
}
.yumi-date-picker-panel .ant-picker-header {
padding: 8px 12px;
border-bottom-color: #eef1f5;
}
.yumi-date-picker-panel .ant-picker-header button {
min-width: 34px;
height: 34px;
border-radius: 8px;
}
.yumi-date-picker-panel .ant-picker-header-view {
line-height: 34px;
font-weight: 700;
}
.yumi-date-picker-panel .ant-picker-content th {
height: 34px;
color: #7b8494;
font-weight: 600;
}
.yumi-date-picker-panel .ant-picker-cell {
padding: 4px 0;
}
.yumi-date-picker-panel .ant-picker-cell-inner {
min-width: 34px;
height: 34px !important;
line-height: 34px !important;
border-radius: 8px !important;
transition: background .18s ease, color .18s ease, transform .18s ease;
}
.yumi-date-picker-panel .ant-picker-cell:hover .ant-picker-cell-inner {
background: #eef6ff;
transform: translateY(-1px);
}
.yumi-date-picker-panel .ant-picker-cell-selected .ant-picker-cell-inner,
.yumi-date-picker-panel .ant-picker-cell-range-start .ant-picker-cell-inner,
.yumi-date-picker-panel .ant-picker-cell-range-end .ant-picker-cell-inner {
background: #1677ff;
color: #fff;
font-weight: 700;
}
.yumi-date-picker-panel .ant-picker-footer {
border-top-color: #eef1f5;
}
.yumi-date-time-panel .ant-picker-time-panel,
.yumi-time-panel .ant-picker-time-panel {
min-width: 150px;
}
.yumi-date-time-panel .ant-picker-time-panel-column,
.yumi-time-panel .ant-picker-time-panel-column {
width: 76px;
padding: 8px 6px;
}
.yumi-date-time-panel .ant-picker-time-panel-cell-inner,
.yumi-time-panel .ant-picker-time-panel-cell-inner {
height: 34px !important;
line-height: 34px !important;
border-radius: 8px !important;
text-align: center;
}
.yumi-date-time-panel .ant-picker-time-panel-cell-selected .ant-picker-time-panel-cell-inner,
.yumi-time-panel .ant-picker-time-panel-cell-selected .ant-picker-time-panel-cell-inner {
background: #eef6ff;
color: #1677ff;
font-weight: 700;
}
.ant-picker {
border-radius: 8px;
}
.ant-picker-focused,
.ant-picker:hover {
border-color: #1677ff;
box-shadow: 0 0 0 3px rgba(22, 119, 255, 0.12);
}
.popup-message-body {
display: grid;
gap: 12px;
}
.popup-message-content {
white-space: pre-wrap;
line-height: 1.8;
}
.popup-message-image {
width: 100%;
max-height: 360px;
object-fit: contain;
border-radius: 8px;
background: #fff7f8;
}
</style>