skyline 4 месяцев назад
Родитель
Сommit
c8251f5597

+ 10 - 0
haha-miniapp/src/main/java/com/haha/miniapp/config/SaTokenConfig.java

@@ -2,10 +2,12 @@ package com.haha.miniapp.config;
 
 import cn.dev33.satoken.interceptor.SaInterceptor;
 import cn.dev33.satoken.stp.StpUtil;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
+@Slf4j
 @Configuration
 public class SaTokenConfig implements WebMvcConfigurer {
     
@@ -13,6 +15,14 @@ public class SaTokenConfig implements WebMvcConfigurer {
     public void addInterceptors(InterceptorRegistry registry) {
         // 注册Sa-Token拦截器,拦截所有路径
         registry.addInterceptor(new SaInterceptor(handle -> {
+            // 记录token信息(用于调试)
+            String tokenValue = StpUtil.getTokenValue();
+            if (tokenValue != null && !tokenValue.isEmpty()) {
+                log.debug("当前请求token: {}, 是否登录: {}", tokenValue, StpUtil.isLogin());
+            } else {
+                log.debug("当前请求未携带token");
+            }
+            
             // 登录认证:除了指定的接口,其他都需要登录
             StpUtil.checkLogin();
         }))

+ 11 - 5
haha-miniapp/src/main/java/com/haha/miniapp/controller/DeviceController.java

@@ -40,17 +40,23 @@ public class DeviceController {
             return Result.error(400, "参数错误:deviceId 不能为空");
         }
 
-        // 2. 获取用户信息
-        Long userId = StpUtil.getLoginIdAsLong();
-        log.info("用户 {} 扫码请求打开设备 {}", userId, deviceId);
-
         try {
+            // 2. 获取用户信息
+            Long userId = StpUtil.getLoginIdAsLong();
+            log.info("用户 {} 扫码请求打开设备 {}", userId, deviceId);
+
             // 3. 调用Service层处理业务逻辑
             OpenDoorVO result = deviceService.scanOpenDoor(deviceId, userId);
             return Result.success("开门成功,请取货", result);
             
+        } catch (cn.dev33.satoken.exception.NotLoginException e) {
+            // 捕获 Sa-Token 未登录异常
+            log.warn("用户未登录或token无效,设备: {}, 错误类型: {}, 错误信息: {}", 
+                deviceId, e.getType(), e.getMessage());
+            return Result.error(401, "登录已失效,请重新登录");
+            
         } catch (Exception e) {
-            // 处理异常
+            // 处理其他异常
             log.error("开门异常", e);
             
             // 判断是否为SDK异常