RouteService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace App\Game\Services;
  3. use App\Game\AgentLinks;
  4. use App\Game\GlobalUserInfo;
  5. use App\Game\Route;
  6. use App\Game\RouteModel;
  7. use App\Game\WebChannelConfig;
  8. use App\Game\WebRegionConfig;
  9. use App\Models\AccountsInfo;
  10. use Illuminate\Http\Request;
  11. class RouteService
  12. {
  13. public function createRoute(array $data)
  14. {
  15. $model = RouteModel::create($data);
  16. return $model;
  17. }
  18. /**
  19. * 获取所有路由及其层级结构
  20. *
  21. * @return \Illuminate\Database\Eloquent\Collection
  22. */
  23. public function getAllRoutesWithHierarchy($StateConfig=1)
  24. {
  25. // 仅加载顶层路由,并预加载所有嵌套子路由
  26. return RouteModel::whereNull('parent_id')
  27. ->whereRaw('state&'.$StateConfig.'='.$StateConfig)
  28. ->with('subs.subs.subs') // 根据实际层级深度调整
  29. ->get();
  30. }
  31. /**
  32. * 通过访问的网站获取配置区分
  33. * @param Request|null $request
  34. * @return int
  35. */
  36. public static function getStateConfig(Request $request=null)
  37. {
  38. // if(!$request){
  39. // $origin = $_SERVER['HTTP_ORIGIN'] ?? '*';
  40. // }else{
  41. // $origin = $request->server('HTTP_ORIGIN') ?? '*';
  42. // }
  43. //
  44. //// $routes = $this->routeService->getAllRoutesWithHierarchy();
  45. // $StateConfig=1;
  46. // if(strstr($origin,"24680.pro")||strstr($origin,"localhost")){
  47. // $StateConfig=2;
  48. // }
  49. $config=self::getChannelConfig($request);
  50. $StateConfig=$config->StateNo;
  51. return $StateConfig;
  52. }
  53. /**
  54. * @param Request|null $request
  55. * @return string
  56. */
  57. public static function getStateToWhereRaw(Request $request=null)
  58. {
  59. $StateConfig=self::getStateConfig($request);
  60. return 'state&'.$StateConfig.'='.$StateConfig;
  61. }
  62. private static $_ChannelConfig=null;
  63. public static function clearChannelConfig(){
  64. self::$_ChannelConfig=null;
  65. }
  66. /**
  67. * @param Request|null $request
  68. * @return WebRegionConfig|null
  69. */
  70. public static function getRegionConfig()
  71. {
  72. $RegionID = $_REQUEST['regionid'] ?? '';
  73. if (empty($RegionID) ) {
  74. $origin = ($_SERVER['HTTP_ORIGIN'] ?? $_SERVER['HTTP_REFERER'] ??'');
  75. if(!empty($origin)) {
  76. $RegionID = explode('.', $origin)[str_starts_with($origin, 'www') ? 1 : 0];
  77. }
  78. }
  79. if(empty($RegionID)) {
  80. $RegionID= env('DEFAULT_REGIONID','winus777');
  81. }
  82. $regionConfig = WebRegionConfig::query()->where('RegionID', $RegionID)->first();
  83. if ($regionConfig) return $regionConfig;
  84. return null;
  85. }
  86. public static function getThemeKey()
  87. {
  88. $regionConfig=self::getRegionConfig();
  89. if($regionConfig)return $regionConfig->ThemeKey;
  90. return env('DEFAULT_THEME_KEY','Theme1');
  91. }
  92. /**
  93. * @param Request|null $request
  94. * @return WebChannelConfig|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\LaravelIdea\Helper\App\Game\_IH_WebChannelConfig_QB|mixed|object|null
  95. */
  96. public static function getChannelConfig(Request $request=null)
  97. {
  98. if(!is_null(self::$_ChannelConfig)){
  99. return self::$_ChannelConfig;
  100. }
  101. $Channel=self::getChannel($request);
  102. $config=WebChannelConfig::getByChannel($Channel);
  103. if(!$config){
  104. $config=WebChannelConfig::getByChannel(env('REGION_24680_DEFAULT_CHANNEL',100));
  105. if(!$config)$config=WebChannelConfig::query()->first();
  106. }else if($config->SHADOW_CHANNEL()!=$config->Channel){
  107. $config=WebChannelConfig::getByChannel($config->SHADOW_CHANNEL());
  108. }
  109. self::$_ChannelConfig=$config;
  110. return $config;
  111. }
  112. public static function getChannel(Request $request=null)
  113. {
  114. //从参数获取
  115. $origin = $request
  116. ? ($request->server('HTTP_ORIGIN') ?? $request->server('HTTP_REFERER') ?? '*')
  117. : ($_SERVER['HTTP_ORIGIN'] ?? $_SERVER['HTTP_REFERER'] ?? '*');
  118. $Channel = $request ? $request->input('c', '') : ($_REQUEST['c'] ?? '');
  119. $Channel = explode('/', $Channel)[0];
  120. // 如果没有 Channel,尝试从 pixel 获取
  121. if (!$Channel) {
  122. $pixel = trim($request ? $request->input('pixel', '') : ($_REQUEST['pixel'] ?? ''));
  123. if ($pixel) {
  124. $config = WebChannelConfig::where('PlatformID', $pixel)->first();
  125. if ($config) {
  126. $Channel = $config->Channel;
  127. }
  128. }
  129. }
  130. //从用户获取
  131. if(GlobalUserInfo::$me){
  132. $Channel=GlobalUserInfo::$me->Channel;
  133. }else if(isset($_REQUEST['act'])&&!empty($_REQUEST['act'])){
  134. $ActCode = $_REQUEST['act'];
  135. $link = AgentLinks::getByCode($ActCode);
  136. if ($link) {
  137. $Channel = $link->Channel;
  138. }
  139. }
  140. //默认值用region绑定
  141. if(empty($Channel)||$Channel==env('REGION_24680_DEFAULT_CHANNEL',100)) {
  142. //根据域名获取渠道
  143. $RegionID = $request ? $request->input('regionid', '') : ($_REQUEST['regionid'] ?? '');
  144. if (empty($RegionID) && !empty($origin)) {
  145. $RegionID = explode('.', $origin)[str_starts_with($origin, 'www') ? 1 : 0];
  146. }
  147. if (!empty($RegionID)) {
  148. $exist = WebChannelConfig::where('RegionID', $RegionID)->whereRaw('SpecialMode&' . SPECIAL_MODE_REGION_UNIQUE)->value('Channel');
  149. if ($exist) {
  150. $Channel = $exist;
  151. }
  152. }
  153. }
  154. return $Channel??env('REGION_24680_DEFAULT_CHANNEL',100);
  155. }
  156. public static function isTestSite()
  157. {
  158. $origin = $_SERVER['HTTP_ORIGIN'] ??$_SERVER['HTTP_REFERER']?? '*';
  159. // if(isset($_REQUEST['c'])&&!empty($_REQUEST['c'])){
  160. // $Channel=$_REQUEST['c'];
  161. // $Channel=explode('/',$Channel)[0];
  162. // }
  163. if (strstr($origin, "test")||strstr($origin, "dev")) {
  164. // $Channel = 44;
  165. return $origin;
  166. }
  167. return false;
  168. }
  169. public static function isTestOrLocalSite()
  170. {
  171. // return true;
  172. $origin = self::isTestSite();
  173. // if(isset($_REQUEST['c'])&&!empty($_REQUEST['c'])){
  174. // $Channel=$_REQUEST['c'];
  175. // $Channel=explode('/',$Channel)[0];
  176. // }
  177. if (strstr($origin, "localhost")) {
  178. // $Channel = 44;
  179. return $origin;
  180. }
  181. return false;
  182. }
  183. }