Просмотр исходного кода

✨ feat(首页): 增强登录状态处理和调试日志

- 添加本地存储令牌检查,优化查询启用条件
- 增加详细的调试日志输出,便于追踪数据流和API状态
- 调整隐私政策弹框显示逻辑,仅对从未登录过的用户显示
yourname 2 недель назад
Родитель
Сommit
515f715d2c
1 измененных файлов с 15 добавлено и 9 удалено
  1. 15 9
      mini/src/pages/index/index.tsx

+ 15 - 9
mini/src/pages/index/index.tsx

@@ -87,6 +87,9 @@ const HomePage: React.FC = () => {
     staleTime: 5 * 60 * 1000, // 5分钟缓存
   })
 
+  const hasToken = Taro.getStorageSync('mini_token')
+  console.debug('[首页] 商品查询状态:', { isLoggedIn, hasToken })
+
   const {
     data,
     isLoading,
@@ -98,7 +101,7 @@ const HomePage: React.FC = () => {
   } = useInfiniteQuery({
     queryKey: ['home-goods-infinite'],
     queryFn: async ({ pageParam = 1 }) => {
-      // console.debug('请求商品数据,页码:', pageParam)
+      console.debug('[首页] 请求商品数据,页码:', pageParam)
       // 使用类型断言绕过 RPC 客户端类型推断问题
       const response = await (goodsClient.$get as any)({
         query: {
@@ -109,15 +112,16 @@ const HomePage: React.FC = () => {
           sortOrder: 'DESC' // 倒序排列
         }
       })
+      console.debug('[首页] API响应状态:', response.status)
       if (response.status !== 200) {
         throw new Error('获取商品失败')
       }
       const result = await response.json()
-      // console.debug('API响应数据:', {
-      //   page: pageParam,
-      //   dataCount: result.data?.length || 0,
-      //   pagination: result.pagination
-      // })
+      console.debug('[首页] API响应数据:', {
+        page: pageParam,
+        dataCount: result.data?.length || 0,
+        pagination: result.pagination
+      })
       return result
     },
     getNextPageParam: (lastPage, _allPages) => {
@@ -139,7 +143,7 @@ const HomePage: React.FC = () => {
     },
     staleTime: 5 * 60 * 1000,
     initialPageParam: 1,
-    enabled: isLoggedIn,
+    enabled: isLoggedIn || !!hasToken,
   })
 
   // 合并所有分页数据
@@ -243,9 +247,11 @@ const HomePage: React.FC = () => {
     }
   })
 
-  // 未登录时显示隐私政策弹框
+  // 未登录时显示隐私政策弹框(仅限从未登录过的用户)
   React.useEffect(() => {
-    if (!isLoggedIn) {
+    const hasToken = Taro.getStorageSync('mini_token')
+    // 只有完全没有登录过的用户才显示隐私弹框
+    if (!isLoggedIn && !hasToken) {
       setShowPrivacyModal(true)
     }
   }, [isLoggedIn])