PageModule.php 7.7 KB

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