2
0

RouteService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. public static function getRegionConfig()
  67. {
  68. $RegionID = $_REQUEST['regionid'] ?? '';
  69. if (empty($RegionID) ) {
  70. $origin = ($_SERVER['HTTP_ORIGIN'] ?? $_SERVER['HTTP_REFERER'] ??'');
  71. if(!empty($origin)) {
  72. $RegionID = explode('.', $origin)[str_starts_with($origin, 'www') ? 1 : 0];
  73. }
  74. }
  75. if(empty($RegionID)) {
  76. $RegionID= env('DEFAULT_REGIONID','winus777');
  77. }
  78. $regionConfig = WebRegionConfig::query()->where('RegionID', $RegionID)->first();
  79. if ($regionConfig) return $regionConfig;
  80. return null;
  81. }
  82. public static function getThemeKey()
  83. {
  84. $regionConfig=self::getRegionConfig();
  85. if($regionConfig)return $regionConfig->ThemeKey;
  86. return env('DEFAULT_THEME_KEY','Theme1');
  87. }
  88. /**
  89. * @param Request|null $request
  90. * @return WebChannelConfig|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\LaravelIdea\Helper\App\Game\_IH_WebChannelConfig_QB|mixed|object|null
  91. */
  92. public static function getChannelConfig(Request $request=null)
  93. {
  94. if(!is_null(self::$_ChannelConfig)){
  95. return self::$_ChannelConfig;
  96. }
  97. $Channel=self::getChannel($request);
  98. $config=WebChannelConfig::getByChannel($Channel);
  99. if(!$config){
  100. $config=WebChannelConfig::getByChannel(env('REGION_24680_DEFAULT_CHANNEL',100));
  101. if(!$config)$config=WebChannelConfig::query()->first();
  102. }else if($config->SHADOW_CHANNEL()!=$config->Channel){
  103. $config=WebChannelConfig::getByChannel($config->SHADOW_CHANNEL());
  104. }
  105. self::$_ChannelConfig=$config;
  106. return $config;
  107. }
  108. public static function getChannel(Request $request=null)
  109. {
  110. //从参数获取
  111. $origin = $request
  112. ? ($request->server('HTTP_ORIGIN') ?? $request->server('HTTP_REFERER') ?? '*')
  113. : ($_SERVER['HTTP_ORIGIN'] ?? $_SERVER['HTTP_REFERER'] ?? '*');
  114. $Channel = $request ? $request->input('c', '') : ($_REQUEST['c'] ?? '');
  115. $Channel = explode('/', $Channel)[0];
  116. // 如果没有 Channel,尝试从 pixel 获取
  117. if (!$Channel) {
  118. $pixel = trim($request ? $request->input('pixel', '') : ($_REQUEST['pixel'] ?? ''));
  119. if ($pixel) {
  120. $config = WebChannelConfig::where('PlatformID', $pixel)->first();
  121. if ($config) {
  122. $Channel = $config->Channel;
  123. }
  124. }
  125. }
  126. //从用户获取
  127. if(GlobalUserInfo::$me){
  128. $Channel=GlobalUserInfo::$me->Channel;
  129. }else if(isset($_REQUEST['act'])&&!empty($_REQUEST['act'])){
  130. $ActCode = $_REQUEST['act'];
  131. $link = AgentLinks::getByCode($ActCode);
  132. if ($link) {
  133. $Channel = $link->Channel;
  134. }
  135. }
  136. //默认值用region绑定
  137. if(empty($Channel)||$Channel==env('REGION_24680_DEFAULT_CHANNEL',100)) {
  138. //根据域名获取渠道
  139. $RegionID = $request ? $request->input('regionid', '') : ($_REQUEST['regionid'] ?? '');
  140. if (empty($RegionID) && !empty($origin)) {
  141. $RegionID = explode('.', $origin)[str_starts_with($origin, 'www') ? 1 : 0];
  142. }
  143. if (!empty($RegionID)) {
  144. $exist = WebChannelConfig::where('RegionID', $RegionID)->whereRaw('SpecialMode&' . SPECIAL_MODE_REGION_UNIQUE)->value('Channel');
  145. if ($exist) {
  146. $Channel = $exist;
  147. }
  148. }
  149. }
  150. return $Channel??env('REGION_24680_DEFAULT_CHANNEL',100);
  151. }
  152. public static function isTestSite()
  153. {
  154. $origin = $_SERVER['HTTP_ORIGIN'] ??$_SERVER['HTTP_REFERER']?? '*';
  155. // if(isset($_REQUEST['c'])&&!empty($_REQUEST['c'])){
  156. // $Channel=$_REQUEST['c'];
  157. // $Channel=explode('/',$Channel)[0];
  158. // }
  159. if (strstr($origin, "test")||strstr($origin, "dev")) {
  160. // $Channel = 44;
  161. return $origin;
  162. }
  163. return false;
  164. }
  165. public static function isTestOrLocalSite()
  166. {
  167. // return true;
  168. $origin = self::isTestSite();
  169. // if(isset($_REQUEST['c'])&&!empty($_REQUEST['c'])){
  170. // $Channel=$_REQUEST['c'];
  171. // $Channel=explode('/',$Channel)[0];
  172. // }
  173. if (strstr($origin, "localhost")) {
  174. // $Channel = 44;
  175. return $origin;
  176. }
  177. return false;
  178. }
  179. }