Selaa lähdekoodia

Merge branch 'dev' of http://git.range8.cn/youyou/dms-api into dev

PC-202304251453\Administrator 6 kuukautta sitten
vanhempi
commit
2ea556a027

+ 1 - 1
app/v1/controller/center/CommonController.php

@@ -51,7 +51,7 @@ class CommonController extends BaseController
      */
     public function getTreeGameOptions(Request $request): Response
     {
-        $data = $this->gameMainLogic->getTreeGameOptions();
+        $data = $this->gameLogic->getGameListByPermission();
         return $this->success($data);
     }
 }

+ 1 - 0
app/v1/controller/center/GameController.php

@@ -47,6 +47,7 @@ class GameController extends BaseController
             ['os', ''],
             ['status', ''],
         ]);
+        
         $list = $this->logic->getIndex($where);
        
         return $this->success($list);

+ 36 - 20
app/v1/logic/center/GameLogic.php

@@ -41,8 +41,9 @@ class GameLogic extends BaseLogic
 
         $data = $this->getList($query);
 
+        $mainGameMap = $this->gameMainLogic->getMainGameMap();
         foreach ($data['data'] as $key => $value) {
-            $data['data'][$key]['main_game_name'] = $this->gameMainLogic->getMainGameNameById($value['main_id']);
+            $data['data'][$key]['main_game_name'] = $mainGameMap[$value['main_id']] ?? '';
         }
         return $data;
     }
@@ -62,14 +63,15 @@ class GameLogic extends BaseLogic
         }, $list);
         return $list;
     }
-
+    
     /**
-     * 处理游戏tree数据
-     * @return array
+     * 获取所有的游戏数据
      */
-    public function getGameTree($game_list)
+    public function getAllGameData()
     {
-         $mainGameMap = $this->gameMainLogic->getMainGameMap();
+        $game_list = $this->model->where('status', 1)->order('sort', 'desc')->select()->toArray();
+
+        $mainGameMap = $this->gameMainLogic->getMainGameMap();
         
          $groupedGames = [];
          foreach ($game_list as $game) {
@@ -81,17 +83,16 @@ class GameLogic extends BaseLogic
                 'name' => $game['name']
             ];
          }
-        $game_list = array_values($groupedGames);
-        return $game_list;
+        return array_values($groupedGames);
     }
 
     /**
-     * 获取所有的游戏数据
+     * 根据权限获取游戏列表
      */
-    public function getAllGameData()
+    public function getGameListByPermission()
     {
-        $game_list = $this->model->where('status', 1)->order('sort', 'desc')->select()->toArray();
-        return $this->getGameTree($game_list);
+        $game_list = $this->getAllGameData();
+        return $game_list;
     }
 
     /**
@@ -100,15 +101,28 @@ class GameLogic extends BaseLogic
      * @return array
      */
     public function getGameListByDeptId($dept_id)
+    
     {
-        $game_ids = $this->systemDeptLogic->getGameListByDeptId($dept_id);
-        // if($game_ids==='*'){
-        //     $game_list = $this->model->where('status', 1)->order('sort', 'desc')->select()->toArray();
-        // }else{
-        //     $game_list = $this->model->where('id', 'in', $game_ids)->where('status', 1)->order('sort', 'desc')->select()->toArray();
-        // }
-        return $game_ids;
-
+        $game_ids = $this->systemDeptLogic->getGameIdsByDeptId($dept_id);
+        $game_list = [];
+        if($game_ids==='*'){
+            $game_list = $this->model->where('status', 1)->order('sort', 'desc')->select()->toArray();
+        }else{
+            $game_list = $this->model->where('id', 'in', $game_ids)->where('status', 1)->order('sort', 'desc')->select()->toArray();
+        }
+        $mainGameMap = $this->gameMainLogic->getMainGameMap();
+        
+         $groupedGames = [];
+         foreach ($game_list as $game) {
+            $mainId = $game['main_id'];
+            $groupedGames[$mainId]['id'] = $mainId;
+            $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
+            $groupedGames[$mainId]['children'][] = [
+                'id' => $game['id'],
+                'name' => $game['name']
+            ];
+         }
+        return array_values($groupedGames);
     }
 
     /**
@@ -121,4 +135,6 @@ class GameLogic extends BaseLogic
     {
         $this->systemDeptLogic->setGameListByDeptId($dept_id, $game_list);
     }
+
+   
 }

+ 51 - 0
app/v1/middleware/FilterPermission.php

@@ -0,0 +1,51 @@
+<?php
+namespace app\v1\middleware;
+
+use plugin\saiadmin\app\cache\UserInfoCache;
+use Webman\Http\Request;
+use Webman\Http\Response;
+use Webman\MiddlewareInterface;
+
+/**
+ * 权限检查中间件
+ */
+class FilterPermission implements MiddlewareInterface
+{
+    public function process(Request $request, callable $handler) : Response
+    {
+
+        // 获取请求方法
+        $method = strtoupper($request->method());
+
+        // 根据用户id获取用户权限
+        $token = getCurrentInfo();
+        // 用户权限缓存
+        $userAuthCache = new UserInfoCache($token['id']);
+        $user_info = $userAuthCache->getUserInfo();
+
+        // 获取游戏数据权限
+        $auth_game_list = $user_info['game_list'] ?? -1;
+
+        // 获取游戏自然量数据权限
+        $auth_normal_game_list = $user_info['normal_game_list'] ?? -1;
+
+        // 获取游戏自然量数据权限
+        $auth_ad_permission = $user_info['ad_permission'] ?? -1;
+
+        // 追加参数逻辑
+        $extraParams = [
+            'auth_normal_game_list' => $auth_normal_game_list === '*' ? '' : $auth_normal_game_list,
+            'auth_game_list' => $auth_game_list === '*' ? '' : $auth_game_list,
+            'auth_ad_permission' => $auth_ad_permission === '*' ? '' : $auth_ad_permission,
+        ];
+       
+       // 合并并设置回 request
+        if ($method === 'GET') {
+            $request->setGet(array_merge($request->get(), $extraParams));
+        } elseif ($method === 'POST') {
+            $request->setPost(array_merge($request->post(), $extraParams));
+        }
+
+        return $handler($request);
+    }
+}

+ 5 - 1
config/middleware.php

@@ -1,5 +1,6 @@
 <?php
 
+use app\v1\middleware\FilterPermission;
 use plugin\saiadmin\app\middleware\SystemLog;
 use plugin\saiadmin\app\middleware\CheckLogin;
 use plugin\saiadmin\app\middleware\CheckAuth;
@@ -9,5 +10,8 @@ return [
         CheckLogin::class,
         CheckAuth::class,
         SystemLog::class,
-    ]
+    ],
+    'v1' => [
+        FilterPermission::class,
+    ],
 ];

+ 28 - 0
plugin/saiadmin/app/controller/system/SystemUserController.php

@@ -167,4 +167,32 @@ class SystemUserController extends BaseController
         $userAuthCache->clearUserCache();
         return $this->success('操作成功');
     }
+
+    /**
+     * 设置用户权限
+     * @param Request $request
+     * @return Response
+     */
+    public function setUserPermission(Request $request) : Response
+    {
+        $id = $request->post('id');
+        $game_list = $request->post('game_list');
+        $normal_game_list = $request->post('normal_game_list');
+        $ad_permission = $request->post('ad_permission');
+        if ($game_list) {
+            $data['game_list'] = $game_list;
+        }
+        if ($normal_game_list) {
+            $data['normal_game_list'] = $normal_game_list;
+        }
+        if ($ad_permission) {
+            $data['ad_permission'] = $ad_permission;
+        }
+        $this->logic->authEdit($id, $data);
+        $userInfoCache = new UserInfoCache($id);
+        $userInfoCache->clearUserInfo();
+        $userAuthCache = new UserAuthCache($id);
+        $userAuthCache->clearUserCache();
+        return $this->success('操作成功');
+    }
 }

+ 13 - 6
plugin/saiadmin/app/logic/system/SystemDeptLogic.php

@@ -19,12 +19,16 @@ use plugin\saiadmin\utils\Arr;
  */
 class SystemDeptLogic extends BaseLogic
 {
+
+    // protected $gameLogic;
+
     /**
      * 构造函数
      */
     public function __construct()
     {
         $this->model = new SystemDept();
+        // $this->gameLogic = new GameLogic();
     }
 
     /**
@@ -33,7 +37,8 @@ class SystemDeptLogic extends BaseLogic
     public function add($data): mixed
     {
         $data = $this->handleData($data);
-        $data['game_list'] = $this->getGameListByDeptId(['dept_id' => $data['parent_id']]);
+        // 继承父部门的权限
+        // $data['game_list'] = $this->getGameListByDeptId(['dept_id' => $data['parent_id']]);
         $this->model->save($data);
         return $this->model->getKey();
     }
@@ -168,16 +173,16 @@ class SystemDeptLogic extends BaseLogic
         $model->leader()->detach($ids);
     }
 
+
     /**
-     * 根据部门ID获取游戏列表
+     * 根据部门ID获取游戏ID
      */
-    public function getGameListByDeptId($where)
+    public function getGameIdsByDeptId($dept_id)
     {
-        $dept_id = $where['dept_id'];
         $query = $this->model->field('game_list')->where('id', $dept_id);
         $data = $query->find();
-        
-        return $data->game_list;
+        $game_ids = $data->game_list;
+        return $game_ids;
     }
 
     /**
@@ -191,4 +196,6 @@ class SystemDeptLogic extends BaseLogic
         }
         return $result;
     }
+
+
 }

+ 6 - 0
plugin/saiadmin/app/logic/system/SystemUserLogic.php

@@ -127,6 +127,12 @@ class SystemUserLogic extends BaseLogic
                     throw new ApiException('没有权限操作该角色数据');
                 }
             }
+            // 如果修改了部门,则清空权限
+            if ($user->dept_id != $data['dept_id']) {
+                $data['game_list'] = null;
+                $data['normal_game_list'] = null;
+                $data['ad_permission'] = null;
+            }
             $result = parent::edit($id, $data);
             if ($result) {
                 $user->roles()->detach();

+ 1 - 0
plugin/saiadmin/config/route.php

@@ -35,6 +35,7 @@ Route::group('/core', function () {
     Route::post("/user/clearCache",[\plugin\saiadmin\app\controller\system\SystemUserController::class, 'clearCache']);
     Route::post("/user/initUserPassword",[\plugin\saiadmin\app\controller\system\SystemUserController::class, 'initUserPassword']);
     Route::post("/user/setHomePage",[\plugin\saiadmin\app\controller\system\SystemUserController::class, 'setHomePage']);
+    Route::post("/user/setUserPermission",[\plugin\saiadmin\app\controller\system\SystemUserController::class, 'setUserPermission']);
 
     // 角色管理
     fastRoute('role',\plugin\saiadmin\app\controller\system\SystemRoleController::class);