GameLogic.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. $auth_game_list = request()->header('auth_game_list');
  33. $query = $this->search($where);
  34. if(!empty($auth_game_list)) {
  35. $authGameList = explode(',', $auth_game_list);
  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)
  51. {
  52. $has_package = $where['has_package']??false;
  53. unset($where['has_package']);
  54. $auth_game_list = request()->header('auth_game_list');
  55. $query = $this->search($where);
  56. if(!empty($auth_game_list)) {
  57. $authGameList = explode(',', $auth_game_list);
  58. $query->where('id', 'in', $authGameList);
  59. }
  60. $query->where('status', 1);
  61. $list = $query->select()->toArray();
  62. // 携带分组信息
  63. $gameGroupList = Db::connect('db_center')->table('game_group')->where('1=1')->select()->toArray();
  64. $gameListMap = array_column($list, null, 'id');
  65. // 按照gameGroupList分组输出,组内携带game_list(如2,3,4),并将gameListMap中的游戏信息塞入分组,树形结构输出
  66. $result = [];
  67. foreach ($gameGroupList as $group) {
  68. $groupGames = [];
  69. if (!empty($group['game_list'])) {
  70. $gameIds = explode(',', $group['game_list']);
  71. foreach ($gameIds as $gid) {
  72. $gid = trim($gid);
  73. if (isset($gameListMap[$gid])) {
  74. $gameInfo = $gameListMap[$gid];
  75. // 兼容输出格式
  76. $groupGames[] = [
  77. 'id' => $gameInfo['id'],
  78. 'name' => $has_package ? $gameInfo['id'].':'.$gameInfo['name'].':'.$gameInfo['package_name'] : '['.$gameInfo['id'].']'.$gameInfo['name'],
  79. ];
  80. }
  81. }
  82. }
  83. $result[] = [
  84. 'id' =>'group_id_'.$group['id'],
  85. 'name' => $group['name'],
  86. 'children' => $groupGames
  87. ];
  88. }
  89. return $result;
  90. }
  91. /**
  92. * 获取游戏options列表(无权限)
  93. * @return array
  94. */
  95. public function getGameOptionsNoAuth($where)
  96. {
  97. $query = $this->search($where);
  98. $query->where('status', 1);
  99. $list = $query->select()->toArray();
  100. // 携带分组信息
  101. $gameGroupList = Db::connect('db_center')->table('game_group')->where('1=1')->select()->toArray();
  102. $gameListMap = array_column($list, null, 'id');
  103. // 按照gameGroupList分组输出,组内携带game_list(如2,3,4),并将gameListMap中的游戏信息塞入分组,树形结构输出
  104. $result = [];
  105. foreach ($gameGroupList as $group) {
  106. $groupGames = [];
  107. if (!empty($group['game_list'])) {
  108. $gameIds = explode(',', $group['game_list']);
  109. foreach ($gameIds as $gid) {
  110. $gid = trim($gid);
  111. if (isset($gameListMap[$gid])) {
  112. $gameInfo = $gameListMap[$gid];
  113. // 兼容输出格式
  114. $groupGames[] = [
  115. 'id' => $gameInfo['id'],
  116. 'name' =>'['.$gameInfo['id'].']'.$gameInfo['name'],
  117. ];
  118. }
  119. }
  120. }
  121. $result[] = [
  122. 'id' =>'group_id_'.$group['id'],
  123. 'name' => $group['name'],
  124. 'children' => $groupGames
  125. ];
  126. }
  127. return $result;
  128. }
  129. public function gameMainTree($gameList): array
  130. {
  131. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  132. $groupedGames = [];
  133. foreach ($gameList as $game) {
  134. $mainId = $game['main_id'];
  135. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  136. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  137. $groupedGames[$mainId]['children'][] = [
  138. 'id' => $game['id'],
  139. 'name' => '['.$game['id'].']'.$game['name']
  140. ];
  141. }
  142. return array_values($groupedGames);
  143. }
  144. /**
  145. * 根据权限获取游戏列表
  146. */
  147. public function getGameListByPermission($where)
  148. {
  149. $auth_game_list = request()->header('auth_game_list');
  150. $query = $this->search($where);
  151. if(!empty($auth_game_list)) {
  152. $authGameList = explode(',', $auth_game_list);
  153. $query->where('id', 'in', $authGameList);
  154. }
  155. $query->where('status', 1);
  156. $query->order('sort', 'desc');
  157. return $query->select()->toArray();
  158. }
  159. /**
  160. * 根据权限获取游戏列表树形(无权限)
  161. */
  162. public function getGameListTreeNoAuth($where)
  163. {
  164. $pfGame = $this->model->where('status', 1)->order('sort', 'desc')->select()->toArray();
  165. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  166. $groupedGames = [];
  167. foreach ($pfGame as $game) {
  168. $mainId = $game['main_id'];
  169. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  170. $groupedGames[$mainId]['disabled'] = ($where['single'] ?? false);
  171. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  172. $groupedGames[$mainId]['children'][] = [
  173. 'id' => $game['id'],
  174. 'name' => '['.$game['id'].']'.$game['name']
  175. ];
  176. }
  177. return array_values($groupedGames);
  178. }
  179. /**
  180. * 根据权限获取游戏列表树形(有权限)
  181. */
  182. public function getGameListTree($where)
  183. {
  184. $auth_game_list = request()->header('auth_game_list');
  185. $query = $this->search();
  186. if(!empty($auth_game_list)) {
  187. $authGameList = explode(',', $auth_game_list);
  188. $query->where('id', 'in', $authGameList);
  189. }
  190. $query->where('status', 1);
  191. $query->order('sort', 'desc');
  192. $gameList = $query->select()->toArray();
  193. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  194. $groupedGames = [];
  195. foreach ($gameList as $game) {
  196. $mainId = $game['main_id'];
  197. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  198. $groupedGames[$mainId]['disabled'] = ($where['single'] ?? false);
  199. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  200. $groupedGames[$mainId]['children'][] = [
  201. 'id' => $game['id'],
  202. 'name' => '['.$game['id'].']'.$game['name']
  203. ];
  204. }
  205. return array_values($groupedGames);
  206. }
  207. /**
  208. * 设置部门游戏权限
  209. * @param array $where
  210. * @param array $gameList
  211. * @return void
  212. */
  213. public function setGameListByDeptId($dept_id, $gameList)
  214. {
  215. $this->systemDeptLogic->setGameListByDeptId($dept_id, $gameList);
  216. }
  217. }