PageModule.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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)
  60. ->whereRaw(RouteService::getStateToWhereRaw())
  61. ->where('theme_key',RouteService::getThemeKey())
  62. ->orderBy('b_order','desc');
  63. // return $this->hasMany(Banner::class, 'link_module', 'id');
  64. }
  65. // 返回特定模块类型的结构化数据
  66. public function getSpecificDataAttribute()
  67. {
  68. switch ($this->type) {
  69. case 'ModuleAutoBanner':
  70. return $this->handleAutoBanner();
  71. case 'ModuleWinList':
  72. return $this->handleWinList();
  73. case 'ModuleSearch':
  74. return $this->handleSearch();
  75. case 'ModuleGameTabs':
  76. return $this->handleGameTabs();
  77. case 'GameTab':
  78. return $this->handleTab();
  79. case 'ModuleHomeWithdraw':
  80. return $this->handleWithdraw();
  81. case 'ModuleSmallGameList':
  82. case 'ModuleRollSmallGameList':
  83. return $this->handleSmallGameList();
  84. case 'ModuleGameList':
  85. return $this->handleGameList();
  86. default:
  87. return $this->data;
  88. }
  89. }
  90. private function handleWithdraw()
  91. {
  92. return [
  93. 'title'=>$this->title,
  94. 'type' => $this->type,
  95. 'api' => $this->api,
  96. 'tabtype' =>$this->tabtype,
  97. 'tabs' => "",
  98. ];
  99. }
  100. private function handleAutoBanner()
  101. {
  102. return [
  103. 'type' => $this->type,
  104. 'data'=>$this->banners()->get()
  105. ];
  106. }
  107. private function handleWinList()
  108. {
  109. $data['type']=$this->type;
  110. BigWinner::FindWinnerFromGame();
  111. $titles=[0=>'win_list.tab_all',1=>'win_list.tab_slots',2=>'win_list.tab_sport'];
  112. // 获取所有唯一的 gtype
  113. $cache=BigWinner::getCache();
  114. $gtypes = $cache['gtypes'];
  115. // 为每个 gtype 检索赢家数据
  116. $data['tabs']=[];
  117. foreach ($gtypes as $type) {
  118. $data['tabs'][]=['type'=>$type,'title'=>$titles[$type]];
  119. }
  120. $data['data']= $cache['data'];
  121. return $data;
  122. }
  123. private function handleSearch()
  124. {
  125. return [
  126. 'title'=>$this->title,
  127. 'type' => $this->type,
  128. 'api' => $this->api,
  129. 'tabtype' =>$this->tabtype,
  130. 'tabs' => "",
  131. ];
  132. }
  133. private function handleTab()
  134. {
  135. return [
  136. 'id'=>$this->id,
  137. 'title'=>$this->title,
  138. 'icon' => $this->icon,
  139. 'tabtype' =>$this->tabtype,
  140. 'link' => $this->link,
  141. ];
  142. }
  143. private function handleGameTabs()
  144. {
  145. $data=['type'=>$this->type,'tabtype'=>$this->tabtype,'id'=>$this->id];
  146. $data['data'] = $this->subs()->where('type','<>','GameTab')->get()->map(function ($sub) {
  147. return $sub->getSpecificDataAttribute();
  148. });
  149. $data['tabs'] = $this->subs()->where('type','GameTab')->get()->map(function ($sub) {
  150. return $sub->getSpecificDataAttribute();
  151. });
  152. return $data;
  153. }
  154. private function handleSmallGameList()
  155. {
  156. $pageSize = 16; // 从请求获取pageSize或使用默认值
  157. // 获取与模块相关的游戏列表并应用分页
  158. $gamesQuery = $this->games();
  159. if($gamesQuery) {
  160. $games = $gamesQuery->forPage(1, $pageSize)->get();
  161. $games = GameCard::formatGames($games);
  162. $this->data = $games;
  163. }else{
  164. $games=null;
  165. }
  166. return [
  167. 'id'=>$this->id,
  168. 'title'=>$this->title,
  169. 'icon' => $this->icon,
  170. 'type' => $this->type,
  171. 'api' => $this->api,
  172. 'tabtype' =>$this->tabtype,
  173. 'data_key' =>$this->data_key,
  174. 'data' => $games,
  175. 'link' => $this->link,
  176. ];
  177. }
  178. private static $DEFAULT_PAGE_SIZE=9;
  179. public static function getDefaultPageSize()
  180. {
  181. self::$DEFAULT_PAGE_SIZE= ($_REQUEST['_d']??"m")=="m"?23:24;
  182. return self::$DEFAULT_PAGE_SIZE;
  183. }
  184. private function handleGameList()
  185. {
  186. $pageSize = self::getDefaultPageSize(); // 从请求获取pageSize或使用默认值
  187. $page = 1; // 从请求获取当前页或使用默认值
  188. // 根据模块获取游戏ID列表
  189. // 获取与模块相关的游戏列表并应用分页
  190. $gamesQuery = $this->games();
  191. $total = $gamesQuery->count();
  192. // print_r([$total,$gamesQuery->toSql(),$pageSize]);die;
  193. $games = $gamesQuery->forPage($page, $pageSize)->get();
  194. $games=GameCard::formatGames($games);
  195. $totalPage = ceil($total / $pageSize);
  196. $this->data=$games;
  197. return [
  198. 'id'=>$this->id,
  199. 'title'=>$this->title,
  200. 'tabtype' =>$this->tabtype,
  201. 'icon' => $this->icon,
  202. 'type' => $this->type,
  203. 'api' => $this->api,
  204. 'data' => $games,
  205. 'data_key' =>$this->data_key,
  206. 'page' => $page,
  207. 'pageSize' => $pageSize,
  208. 'total' => $total,
  209. 'totalPage' => $totalPage,
  210. ];
  211. }
  212. /**
  213. * 关闭state检查
  214. * @var bool
  215. */
  216. public static $enableStateCheck=true;
  217. protected static function boot()
  218. {
  219. parent::boot();
  220. // 默认按照 pos_index 升序排序
  221. static::addGlobalScope('order', function (Builder $builder) {
  222. $builder->orderBy('pos_index', 'asc');
  223. });
  224. if(self::$enableStateCheck) {
  225. static::addGlobalScope('where', function (Builder $builder) {
  226. // $builder->where('state', 1);
  227. $builder->whereRaw(RouteService::getStateToWhereRaw());
  228. });
  229. }
  230. }
  231. }