GameLogic.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. $query = $this->search($where);
  56. if(!empty($authGameList)) {
  57. $authGameList = explode(',', $authGameList);
  58. $query->where('id', 'in', $authGameList);
  59. }
  60. $query->where('status', 1);
  61. $list = $query->select()->toArray();
  62. return $this->getGameGroupTree($list, $hasPackage);
  63. }
  64. /**
  65. * 获取游戏options列表(无权限限制)
  66. * @return array
  67. */
  68. public function getGameOptionsNoAuth($where): array
  69. {
  70. $query = $this->search($where);
  71. $query->where('status', 1);
  72. $list = $query->select()->toArray();
  73. return $this->getGameGroupTree($list);
  74. }
  75. // 公共方法抽离
  76. protected function getGameGroupTree($list, $hasPackage=null): array
  77. {
  78. $gameListMap = array_column($list, null, 'id');
  79. // 携带分组信息
  80. $gameGroupList = Db::connect('db_center')->table('game_group')->select()->toArray();
  81. // 按照gameGroupList分组输出,组内携带game_list(如2,3,4),并将gameListMap中的游戏信息塞入分组,树形结构输出
  82. $result = [];
  83. foreach ($gameGroupList as $group) {
  84. $groupGames = [];
  85. if (!empty($group['game_list'])) {
  86. $gameIds = explode(',', $group['game_list']);
  87. foreach ($gameIds as $gid) {
  88. $gid = trim($gid);
  89. if (isset($gameListMap[$gid])) {
  90. $gameInfo = $gameListMap[$gid];
  91. // 兼容输出格式
  92. $groupGames[] = [
  93. 'id' => $gameInfo['id'],
  94. 'name' => $hasPackage ? $gameInfo['id'].':'.$gameInfo['name'].':'.$gameInfo['package_name'] : '['.$gameInfo['id'].']'.$gameInfo['name'],
  95. ];
  96. }
  97. }
  98. }
  99. $result[] = [
  100. 'id' =>'group_id_'.$group['id'],
  101. 'name' => $group['name'],
  102. 'children' => $groupGames
  103. ];
  104. }
  105. foreach($result as $key => $value){
  106. if(empty($value['children'])){
  107. unset($result[$key]);
  108. }
  109. }
  110. return $result;
  111. }
  112. public function gameMainTree($gameList): array
  113. {
  114. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  115. $groupedGames = [];
  116. foreach ($gameList as $game) {
  117. $mainId = $game['main_id'];
  118. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  119. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  120. $groupedGames[$mainId]['children'][] = [
  121. 'id' => $game['id'],
  122. 'name' => '['.$game['id'].']'.$game['name']
  123. ];
  124. }
  125. return array_values($groupedGames);
  126. }
  127. /**
  128. * 设置部门游戏权限
  129. * @param array $where
  130. * @param array $gameList
  131. * @return void
  132. */
  133. public function setGameListByDeptId($dept_id, $gameList)
  134. {
  135. $this->systemDeptLogic->setGameListByDeptId($dept_id, $gameList);
  136. }
  137. }