GameLogic.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. $game_list = $this->getList($query);
  148. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  149. $groupedGames = [];
  150. foreach ($game_list as $game) {
  151. $mainId = $game['main_id'];
  152. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  153. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  154. $groupedGames[$mainId]['children'][] = [
  155. 'id' => $game['id'],
  156. 'name' => '['.$game['id'].']'.$game['name']
  157. ];
  158. }
  159. return array_values($groupedGames);
  160. }
  161. /**
  162. * 根据权限获取游戏列表
  163. */
  164. public function getGameListByPermission($where)
  165. {
  166. $auth_game_list = request()->header('auth_game_list');
  167. $query = $this->search($where);
  168. if(!empty($auth_game_list)) {
  169. $authGameList = explode(',', $auth_game_list);
  170. $query->where('id', 'in', $authGameList);
  171. }
  172. $query->where('status', 1);
  173. $query->order('sort', 'desc');
  174. return $query->select()->toArray();
  175. }
  176. /**
  177. * 根据权限获取游戏列表树形(无权限)
  178. */
  179. public function getGameListTreeNoAuth($where)
  180. {
  181. $pfGame = $this->model->where('status', 1)->order('sort', 'desc')->select()->toArray();
  182. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  183. $groupedGames = [];
  184. foreach ($pfGame as $game) {
  185. $mainId = $game['main_id'];
  186. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  187. $groupedGames[$mainId]['disabled'] = ($where['single'] ?? false);
  188. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  189. $groupedGames[$mainId]['children'][] = [
  190. 'id' => $game['id'],
  191. 'name' => '['.$game['id'].']'.$game['name']
  192. ];
  193. }
  194. return array_values($groupedGames);
  195. }
  196. /**
  197. * 根据权限获取游戏列表树形(有权限)
  198. */
  199. public function getGameListTree($where)
  200. {
  201. $auth_game_list = request()->header('auth_game_list');
  202. $query = $this->search();
  203. if(!empty($auth_game_list)) {
  204. $authGameList = explode(',', $auth_game_list);
  205. $query->where('id', 'in', $authGameList);
  206. }
  207. $query->where('status', 1);
  208. $query->order('sort', 'desc');
  209. $game_list = $query->select()->toArray();
  210. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  211. $groupedGames = [];
  212. foreach ($game_list as $game) {
  213. $mainId = $game['main_id'];
  214. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  215. $groupedGames[$mainId]['disabled'] = ($where['single'] ?? false);
  216. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  217. $groupedGames[$mainId]['children'][] = [
  218. 'id' => $game['id'],
  219. 'name' => '['.$game['id'].']'.$game['name']
  220. ];
  221. }
  222. return array_values($groupedGames);
  223. }
  224. /**
  225. * 根据部门ID获取游戏列表
  226. * @param array $where
  227. * @return array
  228. */
  229. public function getGameListByDeptId($dept_id)
  230. {
  231. $game_ids = $this->systemDeptLogic->getGameIdsByDeptId($dept_id);
  232. $game_list = [];
  233. if($game_ids==='*'){
  234. $game_list = $this->model->where('status', 1)->order('sort', 'desc')->select()->toArray();
  235. }else{
  236. $game_list = $this->model->where('id', 'in', $game_ids)->where('status', 1)->order('sort', 'desc')->select()->toArray();
  237. }
  238. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  239. $groupedGames = [];
  240. foreach ($game_list as $game) {
  241. $mainId = $game['main_id'];
  242. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  243. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  244. $groupedGames[$mainId]['children'][] = [
  245. 'id' => $game['id'],
  246. 'name' => '['.$game['id'].']'.$game['name']
  247. ];
  248. }
  249. return array_values($groupedGames);
  250. }
  251. /**
  252. * 设置部门游戏权限
  253. * @param array $where
  254. * @param array $game_list
  255. * @return void
  256. */
  257. public function setGameListByDeptId($dept_id, $game_list)
  258. {
  259. $this->systemDeptLogic->setGameListByDeptId($dept_id, $game_list);
  260. }
  261. }