PageModule.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace App\Game;
  3. use App\Game\Services\RouteService;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Builder;
  6. class PageModule extends Model
  7. {
  8. //
  9. protected $connection='mysql';
  10. public $timestamps = false; // 表中没有 Laravel 自动管理的时间戳字段
  11. const TABLE = 'webgame.modules';
  12. protected $table = self::TABLE;
  13. protected $fillable = ['page_id', 'pos_index','icon','type', 'api', 'data','game_ids','tabtype','title','state'];
  14. protected $casts = [
  15. 'data' => 'array',
  16. ];
  17. // public function subs()
  18. // {
  19. // return $this->hasMany(PageModule::class, 'parent_id','id');
  20. // }
  21. //
  22. // public function parent()
  23. // {
  24. // return $this->belongsTo(PageModule::class, 'parent_id','id');
  25. // }
  26. // 多对多关系 - 上级模块
  27. public function parents()
  28. {
  29. return $this->belongsToMany(PageModule::class, 'webgame.module_parents', 'module_id', 'parent_id');
  30. }
  31. // 多对多关系 - 下级模块
  32. public function subs()
  33. {
  34. return $this->belongsToMany(PageModule::class, 'webgame.module_parents', 'parent_id', 'module_id')->whereRaw(RouteService::getStateToWhereRaw());
  35. }
  36. // public function games()
  37. // {
  38. // return $this->belongsToMany(GameCard::class, null, 'game_ids');
  39. // }
  40. // 添加了 state 状态过滤
  41. public function games()
  42. {
  43. if(strlen($this->game_ids)) {
  44. $gameIds = $this->parseGameIds(); // 假设这是一个解析 game_ids 字段的方法
  45. // 将 $gameIds 转换为逗号分隔的字符串,用于 SQL 查询中的 FIELD 函数
  46. $orderClause = 'FIELD(id, ' . $this->game_ids . ')';
  47. $games = GameCard::whereIn('id', $gameIds)->orderByRaw($orderClause); // 只获取状态为 1 的游戏
  48. return $games;
  49. }
  50. return null;
  51. }
  52. protected function parseGameIds()
  53. {
  54. // 这里简化处理,假设 game_ids 字段是逗号分隔的字符串
  55. return explode(',', $this->game_ids);
  56. }
  57. public function banners()
  58. {
  59. return Banner::where('link_module',$this->id)->whereRaw(RouteService::getStateToWhereRaw());
  60. // return $this->hasMany(Banner::class, 'link_module', 'id');
  61. }
  62. // 返回特定模块类型的结构化数据
  63. public function getSpecificDataAttribute()
  64. {
  65. switch ($this->type) {
  66. case 'ModuleAutoBanner':
  67. return $this->handleAutoBanner();
  68. case 'ModuleWinList':
  69. return $this->handleWinList();
  70. case 'ModuleSearch':
  71. return $this->handleSearch();
  72. case 'ModuleGameTabs':
  73. return $this->handleGameTabs();
  74. case 'GameTab':
  75. return $this->handleTab();
  76. case 'ModuleSmallGameList':
  77. case 'ModuleRollSmallGameList':
  78. return $this->handleSmallGameList();
  79. case 'ModuleGameList':
  80. return $this->handleGameList();
  81. default:
  82. return $this->data;
  83. }
  84. }
  85. private function handleAutoBanner()
  86. {
  87. return [
  88. 'type' => $this->type,
  89. 'data'=>$this->banners()->get()
  90. ];
  91. }
  92. private function handleWinList()
  93. {
  94. $data['type']=$this->type;
  95. BigWinner::FindWinnerFromGame();
  96. $titles=[0=>'win_list.tab_all',1=>'win_list.tab_slots',2=>'win_list.tab_sport'];
  97. // 获取所有唯一的 gtype
  98. $cache=BigWinner::getCache();
  99. $gtypes = $cache['gtypes'];
  100. // 为每个 gtype 检索赢家数据
  101. $data['tabs']=[];
  102. foreach ($gtypes as $type) {
  103. $data['tabs'][]=['type'=>$type,'title'=>$titles[$type]];
  104. }
  105. $data['data']= $cache['data'];
  106. return $data;
  107. }
  108. private function handleSearch()
  109. {
  110. return [
  111. 'title'=>$this->title,
  112. 'type' => $this->type,
  113. 'api' => $this->api,
  114. 'tabtype' =>$this->tabtype,
  115. 'tabs' => "",
  116. ];
  117. }
  118. private function handleTab()
  119. {
  120. return [
  121. 'id'=>$this->id,
  122. 'title'=>$this->title,
  123. 'icon' => $this->icon,
  124. 'tabtype' =>$this->tabtype,
  125. 'link' => $this->link,
  126. ];
  127. }
  128. private function handleGameTabs()
  129. {
  130. $data=['type'=>$this->type,'tabtype'=>$this->tabtype,'id'=>$this->id];
  131. $data['data'] = $this->subs()->where('type','<>','GameTab')->get()->map(function ($sub) {
  132. return $sub->getSpecificDataAttribute();
  133. });
  134. $data['tabs'] = $this->subs()->where('type','GameTab')->get()->map(function ($sub) {
  135. return $sub->getSpecificDataAttribute();
  136. });
  137. return $data;
  138. }
  139. private function handleSmallGameList()
  140. {
  141. $pageSize = 16; // 从请求获取pageSize或使用默认值
  142. // 获取与模块相关的游戏列表并应用分页
  143. $gamesQuery = $this->games();
  144. if($gamesQuery) {
  145. $games = $gamesQuery->forPage(1, $pageSize)->get();
  146. $games = GameCard::formatGames($games);
  147. $this->data = $games;
  148. }else{
  149. $games=null;
  150. }
  151. return [
  152. 'id'=>$this->id,
  153. 'title'=>$this->title,
  154. 'icon' => $this->icon,
  155. 'type' => $this->type,
  156. 'api' => $this->api,
  157. 'tabtype' =>$this->tabtype,
  158. 'data_key' =>$this->data_key,
  159. 'data' => $games,
  160. 'link' => $this->link,
  161. ];
  162. }
  163. private static $DEFAULT_PAGE_SIZE=9;
  164. public static function getDefaultPageSize()
  165. {
  166. self::$DEFAULT_PAGE_SIZE= ($_REQUEST['_d']??"m")=="m"?23:24;
  167. return self::$DEFAULT_PAGE_SIZE;
  168. }
  169. private function handleGameList()
  170. {
  171. $pageSize = self::getDefaultPageSize(); // 从请求获取pageSize或使用默认值
  172. $page = 1; // 从请求获取当前页或使用默认值
  173. // 根据模块获取游戏ID列表
  174. // 获取与模块相关的游戏列表并应用分页
  175. $gamesQuery = $this->games();
  176. $total = $gamesQuery->count();
  177. // print_r([$total,$gamesQuery->toSql(),$pageSize]);die;
  178. $games = $gamesQuery->forPage($page, $pageSize)->get();
  179. $games=GameCard::formatGames($games);
  180. $totalPage = ceil($total / $pageSize);
  181. $this->data=$games;
  182. return [
  183. 'id'=>$this->id,
  184. 'title'=>$this->title,
  185. 'tabtype' =>$this->tabtype,
  186. 'icon' => $this->icon,
  187. 'type' => $this->type,
  188. 'api' => $this->api,
  189. 'data' => $games,
  190. 'data_key' =>$this->data_key,
  191. 'page' => $page,
  192. 'pageSize' => $pageSize,
  193. 'total' => $total,
  194. 'totalPage' => $totalPage,
  195. ];
  196. }
  197. /**
  198. * 关闭state检查
  199. * @var bool
  200. */
  201. public static $enableStateCheck=true;
  202. protected static function boot()
  203. {
  204. parent::boot();
  205. // 默认按照 pos_index 升序排序
  206. static::addGlobalScope('order', function (Builder $builder) {
  207. $builder->orderBy('pos_index', 'asc');
  208. });
  209. if(self::$enableStateCheck) {
  210. static::addGlobalScope('where', function (Builder $builder) {
  211. // $builder->where('state', 1);
  212. $builder->whereRaw(RouteService::getStateToWhereRaw());
  213. });
  214. }
  215. }
  216. }