RouteService.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 WebChannelConfig|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\LaravelIdea\Helper\App\Game\_IH_WebChannelConfig_QB|mixed|object|null
  69. */
  70. public static function getChannelConfig(Request $request=null)
  71. {
  72. if(!is_null(self::$_ChannelConfig)){
  73. return self::$_ChannelConfig;
  74. }
  75. $Channel=self::getChannel($request);
  76. $config=WebChannelConfig::getByChannel($Channel);
  77. if(!$config){
  78. $config=WebChannelConfig::getByChannel(env('REGION_24680_DEFAULT_CHANNEL',100));
  79. if(!$config)$config=WebChannelConfig::query()->first();
  80. }else if($config->SHADOW_CHANNEL()!=$config->Channel){
  81. $config=WebChannelConfig::getByChannel($config->SHADOW_CHANNEL());
  82. }
  83. self::$_ChannelConfig=$config;
  84. return $config;
  85. }
  86. public static function getChannel(Request $request=null)
  87. {
  88. //从参数获取
  89. if(!$request){
  90. $origin = $_SERVER['HTTP_ORIGIN'] ??$_SERVER['HTTP_REFERER']?? '*';
  91. if(isset($_REQUEST['c'])&&!empty($_REQUEST['c'])){
  92. $Channel=$_REQUEST['c'];
  93. $Channel=explode('/',$Channel)[0];
  94. }
  95. // 处理 pixel 参数(与 $request 分支保持一致)
  96. if((!isset($Channel) || !$Channel) && isset($_REQUEST['pixel']) && !empty($_REQUEST['pixel'])){
  97. $pixel = trim($_REQUEST['pixel']);
  98. if($pixel){
  99. $config = WebChannelConfig::where('PlatformID', $pixel)->first();
  100. if($config){
  101. $Channel = $config->Channel;
  102. }
  103. }
  104. }
  105. }else{
  106. $origin = $request->server('HTTP_ORIGIN') ?? $request->server('HTTP_REFERER') ?? '*';
  107. $Channel=$request->input('c','');
  108. $Channel=explode('/',$Channel)[0];
  109. $pixel = trim($request->input('pixel',''));
  110. if(!$Channel && $pixel){
  111. $config = WebChannelConfig::where('PlatformID', $pixel)->first();
  112. if($config){
  113. $Channel = $config->Channel;
  114. }
  115. }
  116. }
  117. //从用户获取
  118. if(GlobalUserInfo::$me){
  119. $Channel=GlobalUserInfo::$me->Channel;
  120. }else if(isset($_REQUEST['act'])&&!empty($_REQUEST['act'])){
  121. $ActCode = $_REQUEST['act'];
  122. $link = AgentLinks::getByCode($ActCode);
  123. if ($link) {
  124. $Channel = $link->Channel;
  125. }
  126. }
  127. //TODO 根据域名获取渠道
  128. //默认站点
  129. if(!isset($Channel)||!$Channel||$Channel==env('REGION_24680_DEFAULT_CHANNEL',100)) {
  130. $region = WebRegionConfig::where('DomainUrl', $origin)->first();
  131. if ($region) {
  132. $bindChannels = is_array($region->BindChannels) ? $region->BindChannels : [];
  133. if ($bindChannels) {
  134. $Channel = @$bindChannels[0];
  135. }
  136. }
  137. //
  138. //
  139. // if (strstr($origin, "24680.pro") || strstr($origin, "localhost")) {
  140. // $Channel = 100;
  141. // } else {
  142. // $Channel = env('REGION_24680_DEFAULT_CHANNEL',100);
  143. // }
  144. }
  145. return $Channel??env('REGION_24680_DEFAULT_CHANNEL',100);
  146. }
  147. public static function isTestSite()
  148. {
  149. $origin = $_SERVER['HTTP_ORIGIN'] ??$_SERVER['HTTP_REFERER']?? '*';
  150. // if(isset($_REQUEST['c'])&&!empty($_REQUEST['c'])){
  151. // $Channel=$_REQUEST['c'];
  152. // $Channel=explode('/',$Channel)[0];
  153. // }
  154. if (strstr($origin, "test")) {
  155. // $Channel = 44;
  156. return $origin;
  157. }
  158. return false;
  159. }
  160. public static function isTestOrLocalSite()
  161. {
  162. // return true;
  163. $origin = self::isTestSite();
  164. // if(isset($_REQUEST['c'])&&!empty($_REQUEST['c'])){
  165. // $Channel=$_REQUEST['c'];
  166. // $Channel=explode('/',$Channel)[0];
  167. // }
  168. if (strstr($origin, "localhost")) {
  169. // $Channel = 44;
  170. return $origin;
  171. }
  172. return false;
  173. }
  174. }