GameLogic.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. public function gameMainTree($gameList): array
  135. {
  136. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  137. $groupedGames = [];
  138. foreach ($gameList as $game) {
  139. $mainId = $game['main_id'];
  140. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  141. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  142. $groupedGames[$mainId]['children'][] = [
  143. 'id' => $game['id'],
  144. 'name' => '['.$game['id'].']'.$game['name']
  145. ];
  146. }
  147. return array_values($groupedGames);
  148. }
  149. /**
  150. * 根据权限获取游戏列表
  151. */
  152. public function getGameListByPermission($where)
  153. {
  154. $auth_game_list = request()->header('auth_game_list');
  155. $query = $this->search($where);
  156. if(!empty($auth_game_list)) {
  157. $authGameList = explode(',', $auth_game_list);
  158. $query->where('id', 'in', $authGameList);
  159. }
  160. $query->where('status', 1);
  161. $query->order('sort', 'desc');
  162. return $query->select()->toArray();
  163. }
  164. /**
  165. * 根据权限获取游戏列表树形(无权限)
  166. */
  167. public function getGameListTreeNoAuth($where)
  168. {
  169. $pfGame = $this->model->where('status', 1)->order('sort', 'desc')->select()->toArray();
  170. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  171. $groupedGames = [];
  172. foreach ($pfGame as $game) {
  173. $mainId = $game['main_id'];
  174. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  175. $groupedGames[$mainId]['disabled'] = ($where['single'] ?? false);
  176. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  177. $groupedGames[$mainId]['children'][] = [
  178. 'id' => $game['id'],
  179. 'name' => '['.$game['id'].']'.$game['name']
  180. ];
  181. }
  182. return array_values($groupedGames);
  183. }
  184. /**
  185. * 根据权限获取游戏列表树形(有权限)
  186. */
  187. public function getGameListTree($where)
  188. {
  189. $auth_game_list = request()->header('auth_game_list');
  190. $query = $this->search();
  191. if(!empty($auth_game_list)) {
  192. $authGameList = explode(',', $auth_game_list);
  193. $query->where('id', 'in', $authGameList);
  194. }
  195. $query->where('status', 1);
  196. $query->order('sort', 'desc');
  197. $gameList = $query->select()->toArray();
  198. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  199. $groupedGames = [];
  200. foreach ($gameList as $game) {
  201. $mainId = $game['main_id'];
  202. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  203. $groupedGames[$mainId]['disabled'] = ($where['single'] ?? false);
  204. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  205. $groupedGames[$mainId]['children'][] = [
  206. 'id' => $game['id'],
  207. 'name' => '['.$game['id'].']'.$game['name']
  208. ];
  209. }
  210. return array_values($groupedGames);
  211. }
  212. /**
  213. * 设置部门游戏权限
  214. * @param array $where
  215. * @param array $gameList
  216. * @return void
  217. */
  218. public function setGameListByDeptId($dept_id, $gameList)
  219. {
  220. $this->systemDeptLogic->setGameListByDeptId($dept_id, $gameList);
  221. }
  222. }