From bfa7d3b23e526809b04299d9c2719c3449aba3be Mon Sep 17 00:00:00 2001
From: longz <longzhaoz@qq.com>
Date: 星期二, 28 四月 2026 10:32:42 +0800
Subject: [PATCH] 小程序图片压缩

---
 yuyueMiniProgram/src/pages/index/index.vue |  102 +++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 83 insertions(+), 19 deletions(-)

diff --git a/yuyueMiniProgram/src/pages/index/index.vue b/yuyueMiniProgram/src/pages/index/index.vue
index 1c21593..b3be601 100644
--- a/yuyueMiniProgram/src/pages/index/index.vue
+++ b/yuyueMiniProgram/src/pages/index/index.vue
@@ -3,6 +3,7 @@
 import { fetchMyApprovalTodos, fetchMyVisitorCodes, fetchVisitRecords } from '@/api/appointment'
 import { useMiniAuthStore } from '@/store/auth'
 import type { MiniMyVisitorCodeOrderItem, MiniMyVisitorCodeTicketItem } from '@/types/appointment'
+import { getApiErrorCode } from '@/utils/mini-login'
 
 interface VisitorCodeDisplayTicket extends MiniMyVisitorCodeTicketItem {
   qrCode: string
@@ -27,9 +28,12 @@
 const visitRecordCount = ref(0)
 
 const profile = computed(() => authStore.profile)
+const isLoggedIn = computed(() => authStore.isLoggedIn)
 const isEmployee = computed(() => profile.value?.userType === 'EMPLOYEE')
+const showEmployeeDashboard = computed(() => !isLoggedIn.value || isEmployee.value)
+const welcomeName = computed(() => profile.value?.displayName || '欢迎使用,请登录')
 const userRoleText = computed(() => {
-  if (!profile.value) {
+  if (!profile.value || !isLoggedIn.value) {
     return ''
   }
   if (profile.value.userType === 'EMPLOYEE') {
@@ -38,7 +42,7 @@
   return '普通访客'
 })
 const secondaryText = computed(() => {
-  if (!profile.value) {
+  if (!profile.value || !isLoggedIn.value) {
     return ''
   }
   if (profile.value.userType === 'EMPLOYEE') {
@@ -46,6 +50,8 @@
   }
   return profile.value.companyName || ''
 })
+const approvalTodoDisplay = computed(() => isLoggedIn.value ? String(approvalTodoCount.value) : '-')
+const visitRecordDisplay = computed(() => isLoggedIn.value ? String(visitRecordCount.value) : '-')
 const currentOrder = computed(() => visitorOrders.value[currentOrderIndex.value] || null)
 const currentTicket = computed(() => {
   if (!currentOrder.value) {
@@ -102,6 +108,10 @@
   return order.visitTimeText || '暂无有效期'
 }
 
+function isUnauthorizedError(error: unknown) {
+  return getApiErrorCode(error) === 401 || getApiErrorCode(error) === 403
+}
+
 function formatDateTime(value?: string | null) {
   if (!value) {
     return ''
@@ -121,6 +131,13 @@
 }
 
 async function loadVisitorCodes() {
+  if (!isLoggedIn.value) {
+    visitorOrders.value = []
+    ticketIndexMap.value = {}
+    currentOrderIndex.value = 0
+    loadingVisitorCodes.value = false
+    return
+  }
   loadingVisitorCodes.value = true
   const currentToken = qrRenderToken.value + 1
   qrRenderToken.value = currentToken
@@ -145,6 +162,9 @@
   catch (error) {
     visitorOrders.value = []
     ticketIndexMap.value = {}
+    if (isUnauthorizedError(error)) {
+      return
+    }
     uni.showToast({
       title: error instanceof Error ? error.message : '加载访客码失败',
       icon: 'none',
@@ -158,7 +178,7 @@
 }
 
 async function loadEmployeeDashboardCounts() {
-  if (!isEmployee.value) {
+  if (!isLoggedIn.value || !isEmployee.value) {
     approvalTodoCount.value = 0
     visitRecordCount.value = 0
     return
@@ -335,14 +355,50 @@
   })
 }
 
+function showLoginPrompt(message?: string) {
+  authStore.showLoginPrompt(message)
+}
+
+function hideLoginPrompt() {
+  authStore.hideLoginPrompt()
+}
+
+function ensureLoggedIn(action?: () => void, message?: string) {
+  if (isLoggedIn.value) {
+    action?.()
+    return true
+  }
+  showLoginPrompt(message)
+  return false
+}
+
+async function reloadHomeData() {
+  if (!isLoggedIn.value) {
+    visitorOrders.value = []
+    ticketIndexMap.value = {}
+    currentOrderIndex.value = 0
+    approvalTodoCount.value = 0
+    visitRecordCount.value = 0
+    return
+  }
+  await Promise.all([
+    loadVisitorCodes(),
+    loadEmployeeDashboardCounts(),
+  ])
+}
+
 function handleOpenApprovalTodo() {
-  uni.navigateTo({ url: '/pages/reviewList/index' })
+  ensureLoggedIn(() => {
+    uni.navigateTo({ url: '/pages/reviewList/index' })
+  }, '您还未登录,请先登录')
 }
 
 function handleOpenVisitRecords() {
-  uni.navigateTo({
-    url: '/pages/reception/index',
-  })
+  ensureLoggedIn(() => {
+    uni.navigateTo({
+      url: '/pages/reception/index',
+    })
+  }, '您还未登录,请先登录')
 }
 
 function buildVisitorCodeSharePath(orderUuid?: string | null) {
@@ -361,8 +417,15 @@
 
 onShow(() => {
   authStore.restoreSession()
-  void loadVisitorCodes()
-  void loadEmployeeDashboardCounts()
+  void reloadHomeData()
+})
+
+onMounted(() => {
+  uni.$on('mini-login-success', reloadHomeData)
+})
+
+onUnmounted(() => {
+  uni.$off('mini-login-success', reloadHomeData)
 })
 
 definePage({
@@ -385,19 +448,19 @@
         </text>
       </view>
 
-      <view class="mb-[36rpx] flex items-center pl-[26rpx] pr-[22rpx]">
+      <view class="mb-[36rpx] flex items-center pl-[26rpx] pr-[22rpx]" @click="!isLoggedIn && showLoginPrompt('您还未登录,请先登录')">
         <image src="/static/home/hi.png" class="mr-[24rpx] h-[96rpx] w-[212rpx]" mode="aspectFit" />
         <view class="flex flex-col">
           <view class="welcome-title flex items-center text-[56rpx] text-white font-[600]">
-            <text>{{ profile?.displayName || '用户' }}</text>
+            <text>{{ welcomeName }}</text>
           </view>
-          <text class="mt-[8rpx] text-[24rpx] text-white/90">
+          <text v-if="isLoggedIn && userRoleText" class="mt-[8rpx] text-[24rpx] text-white/90">
             {{ userRoleText }}<template v-if="secondaryText"> · {{ secondaryText }}</template>
           </text>
         </view>
       </view>
 
-      <view v-if="isEmployee" class="mb-[32rpx] w-full pl-[26rpx] pr-[22rpx]" style="box-sizing: border-box;">
+      <view v-if="showEmployeeDashboard" class="mb-[32rpx] w-full pl-[26rpx] pr-[22rpx]" style="box-sizing: border-box;">
         <view
           class="rounded-[16rpx] p-[24rpx] pb-[26rpx]"
           style="background: linear-gradient(180deg, #CCE4FE 0%, #FAFCFD 40%, #FDFDFD 100%); box-shadow: 0 4rpx 8rpx 0 rgba(0, 104, 255, 0.2);border: 1rpx solid #fff;"
@@ -416,7 +479,7 @@
                   待办
                 </text>
                 <text class="text-[32rpx] text-[#056DFF] font-bold">
-                  {{ approvalTodoCount }}
+                  {{ approvalTodoDisplay }}
                 </text>
               </view>
             </view>
@@ -433,7 +496,7 @@
                   待接待
                 </text>
                 <text class="text-[32rpx] text-[#F86911] font-bold">
-                  {{ visitRecordCount }}
+                  {{ visitRecordDisplay }}
                 </text>
               </view>
             </view>
@@ -445,7 +508,7 @@
         <view
           class="h-[240rpx] w-[344rpx] flex flex-1 flex-col items-center justify-center rounded-[20rpx]"
           style="background: linear-gradient(180deg, #DEF4FF 0%, #FFFFFF 100%);border: 1rpx solid #fff;box-shadow: 0 4rpx 10rpx 0 rgba(0, 104, 255, 0.16)"
-          @click="() => { router.push({ name: 'appointment' }) }"
+          @click="ensureLoggedIn(() => { router.push({ name: 'appointment' }) })"
         >
           <image src="/static/home/appointment.png" class="mb-[12rpx] h-[96rpx] w-[96rpx]" />
           <text class="text-[28rpx] text-[#1a1a1a]">
@@ -456,7 +519,7 @@
           <view
             class="flex flex-1 items-center rounded-[20rpx] px-[40rpx]"
             style="background: linear-gradient(180deg, #FFF7E0 0%, #FFFFFF 100%);box-shadow: 0 4rpx 10rpx 0 rgba(0, 104, 255, 0.16)"
-            @click="() => { router.push({ name: 'record' }) }"
+            @click="ensureLoggedIn(() => { router.push({ name: 'record' }) })"
           >
             <image src="/static/home/record.png" class="mr-[16rpx] h-[64rpx] w-[64rpx]" />
             <text class="text-[28rpx] text-[#000000]">
@@ -466,7 +529,7 @@
           <view
             class="flex flex-1 items-center rounded-[20rpx] px-[40rpx]"
             style="background: linear-gradient(180deg, #E0FAEE 0%, #FFFFFF 100%);border: 1rpx solid #fff;box-shadow: 0 4rpx 10rpx 0 rgba(0, 104, 255, 0.16)"
-            @click="uni.navigateTo({ url: '/pages/choosePerson/index?mode=manage' })"
+            @click="ensureLoggedIn(() => { uni.navigateTo({ url: '/pages/choosePerson/index?mode=manage' }) })"
           >
             <image src="/static/home/visitor.png" class="mr-[16rpx] h-[64rpx] w-[64rpx]" />
             <text class="text-[28rpx] text-[#000000]">
@@ -476,7 +539,7 @@
         </view>
       </view>
 
-      <view class="mt-[32rpx] px-[24rpx]">
+      <view v-if="isLoggedIn" class="mt-[32rpx] px-[24rpx]">
         <view class="mb-[20rpx] flex items-center justify-between">
           <text class="text-[34rpx] text-[#1a1a1a]">
             我的访客码
@@ -610,6 +673,7 @@
     </view>
 
     <image
+      v-if="isLoggedIn"
       src="/static/home/process.png"
       class="fixed right-[-42rpx] z-5 h-[128rpx] w-[128rpx]"
       :style="{ bottom: `${parkFlowBottomPx}px` }"

--
Gitblit v1.7.1