GameLogic.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: your name
  6. // +----------------------------------------------------------------------
  7. namespace app\v1\logic\center;
  8. use plugin\saiadmin\basic\BaseLogic;
  9. use app\v1\model\center\Game;
  10. use plugin\saiadmin\app\logic\system\SystemDeptLogic;
  11. use support\think\Db;
  12. /**
  13. * 游戏列表逻辑层
  14. */
  15. class GameLogic extends BaseLogic
  16. {
  17. protected $gameMainLogic;
  18. protected $systemDeptLogic;
  19. protected $gameGroupLogic;
  20. /**
  21. * 构造函数
  22. */
  23. public function __construct()
  24. {
  25. $this->model = new Game();
  26. $this->gameMainLogic = new GameMainLogic();
  27. $this->systemDeptLogic = new SystemDeptLogic();
  28. $this->gameGroupLogic = new GameGroupLogic();
  29. }
  30. /**
  31. * 获取游戏列表
  32. * @param array $where
  33. * @return array
  34. */
  35. public function getIndex($where)
  36. {
  37. $auth_game_list = request()->header('auth_game_list');
  38. $query = $this->search($where);
  39. if(!empty($auth_game_list)) {
  40. $authGameList = explode(',', $auth_game_list);
  41. $query->where('id', 'in', $authGameList);
  42. }
  43. $query->order('sort', 'desc');
  44. $data = $this->getList($query);
  45. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  46. foreach ($data['data'] as $key => $value) {
  47. $data['data'][$key]['main_game_name'] = $mainGameMap[$value['main_id']] ?? '';
  48. }
  49. return $data;
  50. }
  51. /**
  52. * 获取游戏options列表(有权限)
  53. * @return array
  54. */
  55. public function getGameOptions($where)
  56. {
  57. $has_package = $where['has_package']??false;
  58. unset($where['has_package']);
  59. $auth_game_list = request()->header('auth_game_list');
  60. $query = $this->search($where);
  61. if(!empty($auth_game_list)) {
  62. $authGameList = explode(',', $auth_game_list);
  63. $query->where('id', 'in', $authGameList);
  64. }
  65. $query->where('status', 1);
  66. $list = $query->select()->toArray();
  67. // 携带分组信息
  68. $gameGroupList = Db::connect('db_center')->table('game_group')->where('1=1')->select()->toArray();
  69. $gameListMap = array_column($list, null, 'id');
  70. // 按照gameGroupList分组输出,组内携带game_list(如2,3,4),并将gameListMap中的游戏信息塞入分组,树形结构输出
  71. $result = [];
  72. foreach ($gameGroupList as $group) {
  73. $groupGames = [];
  74. if (!empty($group['game_list'])) {
  75. $gameIds = explode(',', $group['game_list']);
  76. foreach ($gameIds as $gid) {
  77. $gid = trim($gid);
  78. if (isset($gameListMap[$gid])) {
  79. $gameInfo = $gameListMap[$gid];
  80. // 兼容输出格式
  81. $groupGames[] = [
  82. 'id' => $gameInfo['id'],
  83. 'name' => $has_package ? $gameInfo['id'].':'.$gameInfo['name'].':'.$gameInfo['package_name'] : '['.$gameInfo['id'].']'.$gameInfo['name'],
  84. ];
  85. }
  86. }
  87. }
  88. $result[] = [
  89. 'id' =>'group_id_'.$group['id'],
  90. 'name' => $group['name'],
  91. 'children' => $groupGames
  92. ];
  93. }
  94. return $result;
  95. }
  96. /**
  97. * 获取游戏options列表(无权限)
  98. * @return array
  99. */
  100. public function getGameOptionsNoAuth($where)
  101. {
  102. $query = $this->search($where);
  103. $query->where('status', 1);
  104. $list = $query->select()->toArray();
  105. // 携带分组信息
  106. $gameGroupList = Db::connect('db_center')->table('game_group')->where('1=1')->select()->toArray();
  107. $gameListMap = array_column($list, null, 'id');
  108. // 按照gameGroupList分组输出,组内携带game_list(如2,3,4),并将gameListMap中的游戏信息塞入分组,树形结构输出
  109. $result = [];
  110. foreach ($gameGroupList as $group) {
  111. $groupGames = [];
  112. if (!empty($group['game_list'])) {
  113. $gameIds = explode(',', $group['game_list']);
  114. foreach ($gameIds as $gid) {
  115. $gid = trim($gid);
  116. if (isset($gameListMap[$gid])) {
  117. $gameInfo = $gameListMap[$gid];
  118. // 兼容输出格式
  119. $groupGames[] = [
  120. 'id' => $gameInfo['id'],
  121. 'name' =>'['.$gameInfo['id'].']'.$gameInfo['name'],
  122. ];
  123. }
  124. }
  125. }
  126. $result[] = [
  127. 'id' =>'group_id_'.$group['id'],
  128. 'name' => $group['name'],
  129. 'children' => $groupGames
  130. ];
  131. }
  132. return $result;
  133. }
  134. /**
  135. * 获取所有的游戏数据
  136. */
  137. public function getAllGameData($where)
  138. {
  139. $auth_game_list = request()->header('auth_game_list');
  140. $query = $this->search($where);
  141. if(!empty($auth_game_list)) {
  142. $authGameList = explode(',', $auth_game_list);
  143. $query->where('id', 'in', $authGameList);
  144. }
  145. $query->where('status', 1);
  146. $query->order('sort', 'desc');
  147. $gameList = $this->getList($query);
  148. return $this->gameMainTree($gameList);
  149. }
  150. public function gameMainTree($gameList): array
  151. {
  152. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  153. $groupedGames = [];
  154. foreach ($gameList as $game) {
  155. $mainId = $game['main_id'];
  156. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  157. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  158. $groupedGames[$mainId]['children'][] = [
  159. 'id' => $game['id'],
  160. 'name' => '['.$game['id'].']'.$game['name']
  161. ];
  162. }
  163. return array_values($groupedGames);
  164. }
  165. /**
  166. * 根据权限获取游戏列表
  167. */
  168. public function getGameListByPermission($where)
  169. {
  170. $auth_game_list = request()->header('auth_game_list');
  171. $query = $this->search($where);
  172. if(!empty($auth_game_list)) {
  173. $authGameList = explode(',', $auth_game_list);
  174. $query->where('id', 'in', $authGameList);
  175. }
  176. $query->where('status', 1);
  177. $query->order('sort', 'desc');
  178. return $query->select()->toArray();
  179. }
  180. /**
  181. * 根据权限获取游戏列表树形(无权限)
  182. */
  183. public function getGameListTreeNoAuth($where)
  184. {
  185. $pfGame = $this->model->where('status', 1)->order('sort', 'desc')->select()->toArray();
  186. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  187. $groupedGames = [];
  188. foreach ($pfGame as $game) {
  189. $mainId = $game['main_id'];
  190. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  191. $groupedGames[$mainId]['disabled'] = ($where['single'] ?? false);
  192. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  193. $groupedGames[$mainId]['children'][] = [
  194. 'id' => $game['id'],
  195. 'name' => '['.$game['id'].']'.$game['name']
  196. ];
  197. }
  198. return array_values($groupedGames);
  199. }
  200. /**
  201. * 根据权限获取游戏列表树形(有权限)
  202. */
  203. public function getGameListTree($where)
  204. {
  205. $auth_game_list = request()->header('auth_game_list');
  206. $query = $this->search();
  207. if(!empty($auth_game_list)) {
  208. $authGameList = explode(',', $auth_game_list);
  209. $query->where('id', 'in', $authGameList);
  210. }
  211. $query->where('status', 1);
  212. $query->order('sort', 'desc');
  213. $gameList = $query->select()->toArray();
  214. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  215. $groupedGames = [];
  216. foreach ($gameList as $game) {
  217. $mainId = $game['main_id'];
  218. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  219. $groupedGames[$mainId]['disabled'] = ($where['single'] ?? false);
  220. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  221. $groupedGames[$mainId]['children'][] = [
  222. 'id' => $game['id'],
  223. 'name' => '['.$game['id'].']'.$game['name']
  224. ];
  225. }
  226. return array_values($groupedGames);
  227. }
  228. /**
  229. * 根据部门ID获取游戏列表
  230. * @param array $where
  231. * @return array
  232. */
  233. public function getGameListByDeptId($dept_id)
  234. {
  235. $game_ids = $this->systemDeptLogic->getGameIdsByDeptId($dept_id);
  236. if($game_ids==='*'){
  237. $gameList = $this->model->where('status', 1)->order('sort', 'desc')->select()->toArray();
  238. }else{
  239. $gameList = $this->model->where('id', 'in', $game_ids)->where('status', 1)->order('sort', 'desc')->select()->toArray();
  240. }
  241. return $this->gameMainTree($gameList);
  242. }
  243. /**
  244. * 设置部门游戏权限
  245. * @param array $where
  246. * @param array $gameList
  247. * @return void
  248. */
  249. public function setGameListByDeptId($dept_id, $gameList)
  250. {
  251. $this->systemDeptLogic->setGameListByDeptId($dept_id, $gameList);
  252. }
  253. }