GameLogic.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. return $result;
  106. }
  107. public function gameMainTree($gameList): array
  108. {
  109. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  110. $groupedGames = [];
  111. foreach ($gameList as $game) {
  112. $mainId = $game['main_id'];
  113. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  114. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  115. $groupedGames[$mainId]['children'][] = [
  116. 'id' => $game['id'],
  117. 'name' => '['.$game['id'].']'.$game['name']
  118. ];
  119. }
  120. return array_values($groupedGames);
  121. }
  122. /**
  123. * 根据权限获取游戏列表
  124. */
  125. public function getGameListByPermission($where)
  126. {
  127. $authGameList = request()->header('auth_game_list');
  128. $query = $this->search($where);
  129. if(!empty($authGameList)) {
  130. $authGameList = explode(',', $authGameList);
  131. $query->where('id', 'in', $authGameList);
  132. }
  133. $query->where('status', 1);
  134. $query->order('sort', 'desc');
  135. return $query->select()->toArray();
  136. }
  137. /**
  138. * 根据权限获取游戏列表树形(无权限)
  139. */
  140. public function getGameListTreeNoAuth($where)
  141. {
  142. $pfGame = $this->model->where('status', 1)->order('sort', 'desc')->select()->toArray();
  143. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  144. $groupedGames = [];
  145. foreach ($pfGame as $game) {
  146. $mainId = $game['main_id'];
  147. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  148. $groupedGames[$mainId]['disabled'] = ($where['single'] ?? false);
  149. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  150. $groupedGames[$mainId]['children'][] = [
  151. 'id' => $game['id'],
  152. 'name' => '['.$game['id'].']'.$game['name']
  153. ];
  154. }
  155. return array_values($groupedGames);
  156. }
  157. /**
  158. * 根据权限获取游戏列表树形(有权限)
  159. */
  160. public function getGameListTree($where)
  161. {
  162. $authGameList = request()->header('auth_game_list');
  163. $query = $this->search();
  164. if(!empty($authGameList)) {
  165. $authGameList = explode(',', $authGameList);
  166. $query->where('id', 'in', $authGameList);
  167. }
  168. $query->where('status', 1);
  169. $query->order('sort', 'desc');
  170. $gameList = $query->select()->toArray();
  171. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  172. $groupedGames = [];
  173. foreach ($gameList as $game) {
  174. $mainId = $game['main_id'];
  175. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  176. $groupedGames[$mainId]['disabled'] = ($where['single'] ?? false);
  177. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  178. $groupedGames[$mainId]['children'][] = [
  179. 'id' => $game['id'],
  180. 'name' => '['.$game['id'].']'.$game['name']
  181. ];
  182. }
  183. return array_values($groupedGames);
  184. }
  185. /**
  186. * 设置部门游戏权限
  187. * @param array $where
  188. * @param array $gameList
  189. * @return void
  190. */
  191. public function setGameListByDeptId($dept_id, $gameList)
  192. {
  193. $this->systemDeptLogic->setGameListByDeptId($dept_id, $gameList);
  194. }
  195. }