GameLogic.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace app\v1\logic\center;
  3. use plugin\saiadmin\basic\BaseLogic;
  4. use app\v1\model\center\Game;
  5. use plugin\saiadmin\app\logic\system\SystemDeptLogic;
  6. use support\think\Db;
  7. /**
  8. * 游戏列表逻辑层
  9. */
  10. class GameLogic extends BaseLogic
  11. {
  12. protected $gameMainLogic;
  13. protected $systemDeptLogic;
  14. protected $gameGroupLogic;
  15. /**
  16. * 构造函数
  17. */
  18. public function __construct()
  19. {
  20. $this->model = new Game();
  21. $this->gameMainLogic = new GameMainLogic();
  22. $this->systemDeptLogic = new SystemDeptLogic();
  23. $this->gameGroupLogic = new GameGroupLogic();
  24. }
  25. /**
  26. * 获取游戏列表
  27. * @param array $where
  28. * @return array
  29. */
  30. public function getIndex($where)
  31. {
  32. $authGameList = request()->header('auth_game_list');
  33. $query = $this->search($where);
  34. if(!empty($authGameList)) {
  35. $authGameList = explode(',', $authGameList);
  36. $query->where('id', 'in', $authGameList);
  37. }
  38. $query->order('sort', 'desc');
  39. $data = $this->getList($query);
  40. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  41. foreach ($data['data'] as $key => $value) {
  42. $data['data'][$key]['main_game_name'] = $mainGameMap[$value['main_id']] ?? '';
  43. }
  44. return $data;
  45. }
  46. /**
  47. * 获取游戏options列表(有权限限制)
  48. * @return array
  49. */
  50. public function getGameOptions($where): array
  51. {
  52. $hasPackage = $where['has_package']??false;
  53. unset($where['has_package']);
  54. $authGameList = request()->header('auth_game_list');
  55. echo 'authGameList:'.$authGameList;
  56. $query = $this->search($where);
  57. if(!empty($authGameList)) {
  58. $authGameList = explode(',', $authGameList);
  59. $query->where('id', 'in', $authGameList);
  60. }
  61. $query->where('status', 1);
  62. $list = $query->select()->toArray();
  63. return $this->getGameGroupTree($list, $hasPackage);
  64. }
  65. /**
  66. * 获取游戏options列表(无权限限制)
  67. * @return array
  68. */
  69. public function getGameOptionsNoAuth($where): array
  70. {
  71. $query = $this->search($where);
  72. $query->where('status', 1);
  73. $list = $query->select()->toArray();
  74. return $this->getGameGroupTree($list);
  75. }
  76. // 公共方法抽离
  77. protected function getGameGroupTree($list, $hasPackage=null): array
  78. {
  79. $gameListMap = array_column($list, null, 'id');
  80. // 携带分组信息
  81. $gameGroupList = Db::connect('db_center')->table('game_group')->select()->toArray();
  82. // 按照gameGroupList分组输出,组内携带game_list(如2,3,4),并将gameListMap中的游戏信息塞入分组,树形结构输出
  83. $result = [];
  84. foreach ($gameGroupList as $group) {
  85. $groupGames = [];
  86. if (!empty($group['game_list'])) {
  87. $gameIds = explode(',', $group['game_list']);
  88. foreach ($gameIds as $gid) {
  89. $gid = trim($gid);
  90. if (isset($gameListMap[$gid])) {
  91. $gameInfo = $gameListMap[$gid];
  92. // 兼容输出格式
  93. $groupGames[] = [
  94. 'id' => $gameInfo['id'],
  95. 'name' => $hasPackage ? $gameInfo['id'].':'.$gameInfo['name'].':'.$gameInfo['package_name'] : '['.$gameInfo['id'].']'.$gameInfo['name'],
  96. ];
  97. }
  98. }
  99. }
  100. $result[] = [
  101. 'id' =>'group_id_'.$group['id'],
  102. 'name' => $group['name'],
  103. 'children' => $groupGames
  104. ];
  105. }
  106. foreach($result as $key => $value){
  107. if(empty($value['children'])){
  108. unset($result[$key]);
  109. }
  110. }
  111. return $result;
  112. }
  113. public function gameMainTree($gameList): array
  114. {
  115. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  116. $groupedGames = [];
  117. foreach ($gameList as $game) {
  118. $mainId = $game['main_id'];
  119. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  120. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  121. $groupedGames[$mainId]['children'][] = [
  122. 'id' => $game['id'],
  123. 'name' => '['.$game['id'].']'.$game['name']
  124. ];
  125. }
  126. return array_values($groupedGames);
  127. }
  128. /**
  129. * 设置部门游戏权限
  130. * @param array $where
  131. * @param array $gameList
  132. * @return void
  133. */
  134. public function setGameListByDeptId($dept_id, $gameList)
  135. {
  136. $this->systemDeptLogic->setGameListByDeptId($dept_id, $gameList);
  137. }
  138. }