GameLogic.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. $userAuthCache = new UserInfoCache(getCurrentInfo()['id']);
  34. $userInfo = $userAuthCache->getUserInfo();
  35. $authGameList = $userInfo['deptList']['game_list'];
  36. $query = $this->search($where);
  37. if(!empty($authGameList) && $authGameList!='*') {
  38. $authGameList = explode(',', $authGameList);
  39. $query->where('id', 'in', $authGameList);
  40. }
  41. $query->order('id', 'asc');
  42. $data = $this->getList($query);
  43. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  44. foreach ($data['data'] as $key => $value) {
  45. $data['data'][$key]['main_game_name'] = $mainGameMap[$value['main_id']] ?? '';
  46. }
  47. return $data;
  48. }
  49. /**
  50. * 获取游戏options列表(有权限限制)
  51. * @return array
  52. */
  53. public function getGameOptions($where): array
  54. {
  55. $hasPackage = $where['has_package']??false;
  56. unset($where['has_package']);
  57. $userAuthCache = new UserInfoCache(getCurrentInfo()['id']);
  58. $userInfo = $userAuthCache->getUserInfo();
  59. $authGameList = $userInfo['deptList']['game_list'];
  60. $query = $this->search($where);
  61. if(!empty($authGameList) && $authGameList!='*') {
  62. $authGameList = explode(',', $authGameList);
  63. $query->where('id', 'in', $authGameList);
  64. }
  65. $query->where('status', 1);
  66. $list = $query->select()->toArray();
  67. return $this->getGameGroupTree($list, $hasPackage);
  68. }
  69. /**
  70. * 获取所有游戏
  71. * @param mixed $where
  72. * @return array
  73. */
  74. public function getAllGameOptions(): array
  75. {
  76. $query = $this->search([]);
  77. $query->where('status', 1);
  78. $list = $query->select()->toArray();
  79. $data= [];
  80. foreach($list as $key => $value){
  81. $data[] = [
  82. 'value' => $value['id'],
  83. 'label' => '['.$value['id'].']'.$value['name']
  84. ];
  85. }
  86. return $data;
  87. }
  88. /**
  89. * 获取游戏options列表(无权限限制)
  90. * @return array
  91. */
  92. public function getGameOptionsNoAuth($where): array
  93. {
  94. $query = $this->search($where);
  95. $query->where('status', 1);
  96. $list = $query->select()->toArray();
  97. return $this->getGameGroupTree($list);
  98. }
  99. // 公共方法抽离
  100. protected function getGameGroupTree($list, $hasPackage=null): array
  101. {
  102. $gameListMap = array_column($list, null, 'id');
  103. // 携带分组信息
  104. $gameGroupList = Db::connect('db_center')->table('game_group')->select()->toArray();
  105. // 按照gameGroupList分组输出,组内携带game_list(如2,3,4),并将gameListMap中的游戏信息塞入分组,树形结构输出
  106. $result = [];
  107. foreach ($gameGroupList as $group) {
  108. $groupGames = [];
  109. if (!empty($group['game_list'])) {
  110. $gameIds = explode(',', $group['game_list']);
  111. foreach ($gameIds as $gid) {
  112. $gid = trim($gid);
  113. if (isset($gameListMap[$gid])) {
  114. $gameInfo = $gameListMap[$gid];
  115. // 兼容输出格式
  116. $groupGames[] = [
  117. 'id' => $gameInfo['id'],
  118. 'name' => $hasPackage ? $gameInfo['id'].':'.$gameInfo['name'].':'.$gameInfo['package_name'] : '['.$gameInfo['id'].']'.$gameInfo['name'],
  119. ];
  120. }
  121. }
  122. }
  123. $result[] = [
  124. 'id' =>'group_id_'.$group['id'],
  125. 'name' => $group['name'],
  126. 'children' => $groupGames
  127. ];
  128. }
  129. foreach($result as $key => $value){
  130. if(empty($value['children'])){
  131. unset($result[$key]);
  132. }
  133. }
  134. return $result;
  135. }
  136. public function gameMainTree($gameList): array
  137. {
  138. $mainGameMap = $this->gameMainLogic->getMainGameMap();
  139. $groupedGames = [];
  140. foreach ($gameList as $game) {
  141. $mainId = $game['main_id'];
  142. $groupedGames[$mainId]['id'] = 'main_id_'.$mainId;
  143. $groupedGames[$mainId]['name'] = $mainGameMap[$mainId] ?? $mainId;
  144. $groupedGames[$mainId]['children'][] = [
  145. 'id' => $game['id'],
  146. 'name' => '['.$game['id'].']'.$game['name']
  147. ];
  148. }
  149. return array_values($groupedGames);
  150. }
  151. /**
  152. * 设置部门游戏权限
  153. * @param array $where
  154. * @param array $gameList
  155. * @return void
  156. */
  157. public function setGameListByDeptId($dept_id, $gameList)
  158. {
  159. $this->systemDeptLogic->setGameListByDeptId($dept_id, $gameList);
  160. }
  161. }