PC-202304251453\Administrator пре 5 месеци
родитељ
комит
3e5ca654d3
2 измењених фајлова са 32 додато и 58 уклоњено
  1. 30 56
      app/v1/logic/center/GameLogic.php
  2. 2 2
      app/v1/middleware/FilterPermission.php

+ 30 - 56
app/v1/logic/center/GameLogic.php

@@ -35,10 +35,10 @@ class GameLogic extends BaseLogic
      */
     public function getIndex($where)
     {
-        $auth_game_list = request()->header('auth_game_list');
+        $authGameList = request()->header('auth_game_list');
         $query = $this->search($where);
-        if(!empty($auth_game_list)) {
-            $authGameList = explode(',', $auth_game_list);
+        if(!empty($authGameList)) {
+            $authGameList = explode(',', $authGameList);
             $query->where('id', 'in', $authGameList);
         }
         $query->order('sort', 'desc');
@@ -53,72 +53,46 @@ class GameLogic extends BaseLogic
     }
 
     /**
-     * 获取游戏options列表(有权限)
+     * 获取游戏options列表(有权限限制
      * @return array
      */
-    public function getGameOptions($where)
+    public function getGameOptions($where): array
     {
-        $has_package = $where['has_package']??false;
+        $hasPackage = $where['has_package']??false;
         unset($where['has_package']);
-        $auth_game_list = request()->header('auth_game_list');
+        $authGameList = request()->header('auth_game_list');
         $query = $this->search($where);
-        if(!empty($auth_game_list)) {
-            $authGameList = explode(',', $auth_game_list);
+        if(!empty($authGameList)) {
+            $authGameList = explode(',', $authGameList);
             $query->where('id', 'in', $authGameList);
         }
+
         $query->where('status', 1);
         $list = $query->select()->toArray();
 
-        // 携带分组信息
-        $gameGroupList = Db::connect('db_center')->table('game_group')->where('1=1')->select()->toArray();
-
-        $gameListMap = array_column($list, null, 'id'); 
-
-        // 按照gameGroupList分组输出,组内携带game_list(如2,3,4),并将gameListMap中的游戏信息塞入分组,树形结构输出
-        $result = [];
-        foreach ($gameGroupList as $group) {
-            $groupGames = [];
-            if (!empty($group['game_list'])) {
-                $gameIds = explode(',', $group['game_list']);
-                
-                foreach ($gameIds as $gid) {
-                    $gid = trim($gid);
-                    if (isset($gameListMap[$gid])) {
-                        $gameInfo = $gameListMap[$gid];
-                        // 兼容输出格式
-                        $groupGames[] = [
-                            'id' => $gameInfo['id'],
-                            'name' => $has_package ? $gameInfo['id'].':'.$gameInfo['name'].':'.$gameInfo['package_name'] : '['.$gameInfo['id'].']'.$gameInfo['name'],
-                        ];
-                    }
-                }
-            }
-            $result[] = [
-                'id' =>'group_id_'.$group['id'],
-                'name' => $group['name'],
-                'children' => $groupGames
-            ];
-        }
-
-        return $result;
-
-        
+        return $this->getGameGroupTree($list, $hasPackage);
     }
 
     /**
-     * 获取游戏options列表(无权限)
+     * 获取游戏options列表(无权限限制)
      * @return array
      */
-    public function getGameOptionsNoAuth($where)
+    public function getGameOptionsNoAuth($where): array
     {
         $query = $this->search($where);
         $query->where('status', 1);
         $list = $query->select()->toArray();
 
-        // 携带分组信息
-        $gameGroupList = Db::connect('db_center')->table('game_group')->where('1=1')->select()->toArray();
+        return $this->getGameGroupTree($list);
+    }
 
-        $gameListMap = array_column($list, null, 'id'); 
+    // 公共方法抽离
+    protected function getGameGroupTree($list, $hasPackage=null): array
+    {
+        $gameListMap = array_column($list, null, 'id');
+
+        // 携带分组信息
+        $gameGroupList = Db::connect('db_center')->table('game_group')->select()->toArray();
 
         // 按照gameGroupList分组输出,组内携带game_list(如2,3,4),并将gameListMap中的游戏信息塞入分组,树形结构输出
         $result = [];
@@ -126,6 +100,7 @@ class GameLogic extends BaseLogic
             $groupGames = [];
             if (!empty($group['game_list'])) {
                 $gameIds = explode(',', $group['game_list']);
+
                 foreach ($gameIds as $gid) {
                     $gid = trim($gid);
                     if (isset($gameListMap[$gid])) {
@@ -133,7 +108,7 @@ class GameLogic extends BaseLogic
                         // 兼容输出格式
                         $groupGames[] = [
                             'id' => $gameInfo['id'],
-                            'name' =>'['.$gameInfo['id'].']'.$gameInfo['name'],
+                            'name' => $hasPackage ? $gameInfo['id'].':'.$gameInfo['name'].':'.$gameInfo['package_name'] : '['.$gameInfo['id'].']'.$gameInfo['name'],
                         ];
                     }
                 }
@@ -141,7 +116,6 @@ class GameLogic extends BaseLogic
             $result[] = [
                 'id' =>'group_id_'.$group['id'],
                 'name' => $group['name'],
-                
                 'children' => $groupGames
             ];
         }
@@ -169,10 +143,10 @@ class GameLogic extends BaseLogic
      */
     public function getGameListByPermission($where)
     {
-        $auth_game_list = request()->header('auth_game_list');
+        $authGameList = request()->header('auth_game_list');
         $query = $this->search($where);
-        if(!empty($auth_game_list)) {
-            $authGameList = explode(',', $auth_game_list);
+        if(!empty($authGameList)) {
+            $authGameList = explode(',', $authGameList);
             $query->where('id', 'in', $authGameList);
         }
         $query->where('status', 1);
@@ -206,10 +180,10 @@ class GameLogic extends BaseLogic
      */
     public function getGameListTree($where)
     {
-        $auth_game_list = request()->header('auth_game_list');
+        $authGameList = request()->header('auth_game_list');
         $query = $this->search();
-        if(!empty($auth_game_list)) {
-            $authGameList = explode(',', $auth_game_list);
+        if(!empty($authGameList)) {
+            $authGameList = explode(',', $authGameList);
             $query->where('id', 'in', $authGameList);
         }
         $query->where('status', 1);

+ 2 - 2
app/v1/middleware/FilterPermission.php

@@ -22,7 +22,7 @@ class FilterPermission implements MiddlewareInterface
         // $user_info = $this->systemUserLogic->read($token['id']);
 
         // 获取游戏数据权限
-        $auth_game_list = $user_info['game_list'] ?? '';
+        $authGameList = $user_info['game_list'] ?? '';
 
         // 获取游戏自然量数据权限
         $auth_normal_game_list = $user_info['normal_game_list'] ?? '';
@@ -33,7 +33,7 @@ class FilterPermission implements MiddlewareInterface
         // 追加参数逻辑
         $extraParams = [
             'auth_normal_game_list' => $auth_normal_game_list === '*' ? '' : $auth_normal_game_list,
-            'auth_game_list' => $auth_game_list === '*' ? '' : $auth_game_list,
+            'auth_game_list' => $authGameList === '*' ? '' : $authGameList,
             'auth_ad_permission' => $auth_ad_permission === '*' ? '' : $auth_ad_permission,
         ];