GameLogic.php 5.2 KB

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