Explorar o código

refactor(app): 优化登录检查和数据处理

- 重构 LoginCheck 中间件,简化逻辑并添加日志记录
- 修正 CropInfoDto 和 LandInfoDto 中的时间处理
- 更新 FarmCrop模型注释
-增加对枚举类型的处理支持
- 优化 TokenSession 中的缓存键生成和日志记录
- 调整 docker-compose 配置,移除冗余的网络定义
notfff hai 7 meses
pai
achega
ca6551c125

+ 4 - 0
UCore/Commands/GenerateModelAnnotation.php

@@ -579,6 +579,10 @@ class GenerateModelAnnotation extends Command
                 // 检查是否是 Illuminate\Contracts\Database\Eloquent\CastsAttributes 的实现
                 $this->debug("{$castType} 实现了 Illuminate\Contracts\Database\Eloquent\CastsAttributes 接口");
                 return $castType;
+            } else if (is_subclass_of($castType, '\UnitEnum')){
+                // 枚举
+                $this->debug("{$castType} 实现了 \UnitEnum  枚举");
+                return $castType;
             }
         }
 

+ 6 - 2
UCore/TokenSession.php

@@ -4,6 +4,7 @@ namespace UCore;
 
 use App\Version;
 use UCore\Exception\LogicException;
+use UCore\Helper\Logger;
 
 /**
  * Token的session,使用cache作为储存介质,有效期10天
@@ -56,7 +57,10 @@ class TokenSession
      */
     static public function get($key = null, $defaultValue = null)
     {
-        $data = \Illuminate\Support\Facades\Cache::get(static::getKey(), []);
+        $keyFull =static::getKey();
+        $data = \Illuminate\Support\Facades\Cache::get($keyFull, []);
+        Logger::debug('cache.get: '.$keyFull,  $data);
+
         if ($key === null) {
             return $data;
         }
@@ -143,7 +147,7 @@ class TokenSession
 
     static protected function getKey()
     {
-        return static::$prefix . static::$session_id . '_' . Version::VERSION;
+        return static::$prefix . static::$session_id . '_' . \UCore\Version::VERSION;
     }
 
 

+ 31 - 31
app/Module/AppGame/Middleware/LoginCheck.php

@@ -7,6 +7,7 @@ use App\Module\AppGame\SessionApp;
 use App\Module\AppGame\Tools\Protobuf;
 use Closure;
 use Illuminate\Http\Request;
+use UCore\Helper\Logger;
 use Uraus\Kku\Common\RESPONSE_CODE;
 use Uraus\Kku\Response;
 
@@ -28,8 +29,7 @@ class LoginCheck
         /**
          * @var BaseHandler $handler
          */
-        $handler = $request->attributes->get('_handler');
-
+        $handler    = $request->attributes->get('_handler');
         $need_login = false;
         if ($handler && method_exists($handler, 'needLogin') && $handler->needLogin()) {
             // 检查是否已登录
@@ -39,6 +39,8 @@ class LoginCheck
 
         // 需要登陆,进行登陆判断
         $token = $request->header('token', '');
+        Logger::debug('LoginCheck:' . $token . $need_login);
+
         // dump($token);
         if (get_class($handler) == 'App\Module\AppGame\Handler\Public\TokenUsefulHandler') {
             SessionApp::$session_id = $token;
@@ -49,36 +51,34 @@ class LoginCheck
 
             SessionApp::$session_id = SessionApp::genSessionID();
 
+            return $next($request);
+        }
+        if (empty($token)) {
+            //  token 不合法
+            $response = new Response();
+            $response->setCode(RESPONSE_CODE::REQUEST_ERROR);
+            $response->setMsg('请求错误-Token is import!');
+
+            return \App\Module\AppGame\Tools\Protobuf::response($response);
+
         } else {
-            if (empty($token)) {
-                $token = SessionApp::getSessionId();
-                if (get_class($handler) != 'App\Module\AppGame\Handler\Public\TokenHandler') {
-                    //  token 不合法
-                    $response = new Response();
-                    $response->setCode(RESPONSE_CODE::REQUEST_ERROR);
-                    $response->setMsg('请求错误-Token is import!');
-
-                    return \App\Module\AppGame\Tools\Protobuf::response($response);
-                }
-            } else {
-                // token 不为空
-                if (!SessionApp::checktoken($token)) {
-                    //  token 不合法
-                    $response = new Response();
-                    $response->setCode(RESPONSE_CODE::REQUEST_ERROR);
-                    $response->setMsg('请求错误-Token');
-
-                    return Protobuf::response($response);
-                }
-                SessionApp::$session_id = $token;
-                // 登陆判断
-                $uid = \App\Module\AppGame\SessionApp::getUserId();
-                //            dump($uid);
-
-                if ($uid > 0) {
-                    $handler->user_id = $uid;
-                    $login_ok         = true;
-                }
+            // token 不为空
+            if (!SessionApp::checktoken($token)) {
+                //  token 不合法
+                $response = new Response();
+                $response->setCode(RESPONSE_CODE::REQUEST_ERROR);
+                $response->setMsg('请求错误-Token');
+
+                return Protobuf::response($response);
+            }
+            SessionApp::$session_id = $token;
+            // 登陆判断
+            $uid = \App\Module\AppGame\SessionApp::getUserId();
+            //            dump($uid);
+
+            if ($uid > 0) {
+                $handler->user_id = $uid;
+                $login_ok         = true;
             }
         }
 

+ 1 - 2
app/Module/AppGame/Proto/CropInfoDto.php

@@ -41,14 +41,13 @@ class CropInfoDto
         if (!empty($cropInfoDto->stageStartTime)) {
             // 如果 stageStartTime 是字符串格式的日期时间,转换为时间戳
             if (is_string($cropInfoDto->stageStartTime)) {
-                $$stageStartTime = strtotime($cropInfoDto->stageEndTime);
+                $stageStartTime = strtotime($cropInfoDto->stageEndTime);
                 $dataLand->setStageNextTimes($stageStartTime);
             }
             // 如果已经是时间戳,直接使用
             else if (is_numeric($cropInfoDto->stageStartTime)) {
                 $dataLand->setStageNextTimes($cropInfoDto->stageStartTime);
             }
-
         }
 
         // 下一个阶段的时间

+ 1 - 1
app/Module/AppGame/Proto/LandInfoDto.php

@@ -49,7 +49,7 @@ class LandInfoDto
 
             // 设置种子状态,这里需要将Farm模块的生长阶段映射到Proto的种子状态
             // 假设我们有一个映射关系,这里简单地直接使用生长阶段值
-            $dataLand->setSeedStatus($landInfoDto->crop->growthStage);
+            $dataLand->setSeedStatus((int)$landInfoDto->crop->growthStage);
 
             // 检查是否有灾害
             if (!empty($landInfoDto->crop->disasters)) {

+ 3 - 2
app/Module/Farm/Dtos/CropInfoDto.php

@@ -57,7 +57,7 @@ class CropInfoDto
      *
      * @var int
      */
-    public $growthStage;
+    public int $growthStage;
 
     /**
      * 生长阶段名称
@@ -110,7 +110,8 @@ class CropInfoDto
         $dto->seedName = $crop->seed->name ?? '';
 //        dd($crop);
         $dto->plantTime = $crop->plant_time;
-        $dto->growthStage = $crop->growth_stage;
+
+        $dto->growthStage = $crop->growth_stage->value();
 //        $dto->growthStageName = $crop->growth_stage->name;
         $dto->stageStartTime = $crop->stage_start_time ? $crop->stage_start_time : null;
         $dto->stageEndTime = $crop->stage_end_time ? $crop->stage_end_time : null;

+ 1 - 1
app/Module/Farm/Models/FarmCrop.php

@@ -9,7 +9,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
 /**
  * 作物信息模型
  *
- * field start
+ * field start 
  * @property  int  $id  主键ID
  * @property  int  $land_id  土地ID
  * @property  int  $user_id  用户ID

+ 0 - 4
docker-compose.yml

@@ -18,7 +18,3 @@ services:
        - 36014:80
     networks:
         - ggggg
-  redis:
-     image: redis
-     ports:
-       - 6379