GameLogic.php 8.3 KB

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