GameLogic.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace app\v1\logic\center;
  3. use plugin\saiadmin\app\cache\UserInfoCache;
  4. use plugin\saiadmin\basic\BaseLogic;
  5. use app\v1\model\center\Game;
  6. use plugin\saiadmin\app\logic\system\SystemDeptLogic;
  7. use support\think\Db;
  8. /**
  9. * 游戏列表逻辑层
  10. */
  11. class GameLogic extends BaseLogic
  12. {
  13. protected $gameMainLogic;
  14. protected $systemDeptLogic;
  15. protected $gameGroupLogic;
  16. /**
  17. * 构造函数
  18. */
  19. public function __construct()
  20. {
  21. $this->model = new Game();
  22. $this->gameMainLogic = new GameMainLogic();
  23. $this->systemDeptLogic = new SystemDeptLogic();
  24. $this->gameGroupLogic = new GameGroupLogic();
  25. }
  26. /**
  27. * 获取游戏列表
  28. * @param array $where
  29. * @return array
  30. */
  31. public function getIndex($where)
  32. {
  33. // $authGameList = request()->header('auth_game_list');
  34. $userAuthCache = new UserInfoCache(getCurrentInfo()['id']);
  35. $userInfo = $userAuthCache->getUserInfo();
  36. $authGameList = $userInfo['deptList']['game_list'];
  37. $query = $this->search($where);
  38. if(!empty($authGameList) && $authGameList!='*') {
  39. $authGameList = explode(',', $authGameList);
  40. $query->where('id', 'in', $authGameList);
  41. }
  42. $query->order('sort', 'desc');
  43. $data = $this->getList($query);
  44. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  45. foreach ($data['data'] as $key => $value) {
  46. $data['data'][$key]['main_game_name'] = $mainGameMap[$value['main_id']] ?? '';
  47. }
  48. return $data;
  49. }
  50. /**
  51. * 获取游戏options列表(有权限限制)
  52. * @return array
  53. */
  54. public function getGameOptions($where): array
  55. {
  56. $hasPackage = $where['has_package']??false;
  57. unset($where['has_package']);
  58. $userAuthCache = new UserInfoCache(getCurrentInfo()['id']);
  59. $userInfo = $userAuthCache->getUserInfo();
  60. $authGameList = $userInfo['deptList']['game_list'];
  61. $query = $this->search($where);
  62. if(!empty($authGameList) && $authGameList!='*') {
  63. $authGameList = explode(',', $authGameList);
  64. $query->where('id', 'in', $authGameList);
  65. }
  66. $query->where('status', 1);
  67. $list = $query->select()->toArray();
  68. return $this->getGameGroupTree($list, $hasPackage);
  69. }
  70. /**
  71. * 获取所有游戏
  72. * @param mixed $where
  73. * @return array
  74. */
  75. public function getAllGameOptions(): array
  76. {
  77. $query = $this->search([]);
  78. $query->where('status', 1);
  79. $list = $query->select()->toArray();
  80. $data= [];
  81. foreach($list as $key => $value){
  82. $data[] = [
  83. 'value' => $value['id'],
  84. 'label' => '['.$value['id'].']'.$value['name']
  85. ];
  86. }
  87. return $data;
  88. }
  89. /**
  90. * 获取游戏options列表(无权限限制)
  91. * @return array
  92. */
  93. public function getGameOptionsNoAuth($where): array
  94. {
  95. $query = $this->search($where);
  96. $query->where('status', 1);
  97. $list = $query->select()->toArray();
  98. return $this->getGameGroupTree($list);
  99. }
  100. // 公共方法抽离
  101. protected function getGameGroupTree($list, $hasPackage=null): array
  102. {
  103. $gameListMap = array_column($list, null, 'id');
  104. // 携带分组信息
  105. $gameGroupList = Db::connect('db_center')->table('game_group')->select()->toArray();
  106. // 按照gameGroupList分组输出,组内携带game_list(如2,3,4),并将gameListMap中的游戏信息塞入分组,树形结构输出
  107. $result = [];
  108. foreach ($gameGroupList as $group) {
  109. $groupGames = [];
  110. if (!empty($group['game_list'])) {
  111. $gameIds = explode(',', $group['game_list']);
  112. foreach ($gameIds as $gid) {
  113. $gid = trim($gid);
  114. if (isset($gameListMap[$gid])) {
  115. $gameInfo = $gameListMap[$gid];
  116. // 兼容输出格式
  117. $groupGames[] = [
  118. 'id' => $gameInfo['id'],
  119. 'name' => $hasPackage ? $gameInfo['id'].':'.$gameInfo['name'].':'.$gameInfo['package_name'] : '['.$gameInfo['id'].']'.$gameInfo['name'],
  120. ];
  121. }
  122. }
  123. }
  124. $result[] = [
  125. 'id' =>'group_id_'.$group['id'],
  126. 'name' => $group['name'],
  127. 'children' => $groupGames
  128. ];
  129. }
  130. foreach($result as $key => $value){
  131. if(empty($value['children'])){
  132. unset($result[$key]);
  133. }
  134. }
  135. return $result;
  136. }
  137. public function gameMainTree($gameList): array
  138. {
  139. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  140. $groupedGames = [];
  141. foreach ($gameList as $game) {
  142. $mainId = $game['main_id'];
  143. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  144. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  145. $groupedGames[$mainId]['children'][] = [
  146. 'id' => $game['id'],
  147. 'name' => '['.$game['id'].']'.$game['name']
  148. ];
  149. }
  150. return array_values($groupedGames);
  151. }
  152. /**
  153. * 设置部门游戏权限
  154. * @param array $where
  155. * @param array $gameList
  156. * @return void
  157. */
  158. public function setGameListByDeptId($dept_id, $gameList)
  159. {
  160. $this->systemDeptLogic->setGameListByDeptId($dept_id, $gameList);
  161. }
  162. }