RouteService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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\Models\AccountsInfo;
  9. use Illuminate\Http\Request;
  10. class RouteService
  11. {
  12. public function createRoute(array $data)
  13. {
  14. $model = RouteModel::create($data);
  15. return $model;
  16. }
  17. /**
  18. * 获取所有路由及其层级结构
  19. *
  20. * @return \Illuminate\Database\Eloquent\Collection
  21. */
  22. public function getAllRoutesWithHierarchy($StateConfig=1)
  23. {
  24. // 仅加载顶层路由,并预加载所有嵌套子路由
  25. return RouteModel::whereNull('parent_id')
  26. ->whereRaw('state&'.$StateConfig.'='.$StateConfig)
  27. ->with('subs.subs.subs') // 根据实际层级深度调整
  28. ->get();
  29. }
  30. /**
  31. * 通过访问的网站获取配置区分
  32. * @param Request|null $request
  33. * @return int
  34. */
  35. public static function getStateConfig(Request $request=null)
  36. {
  37. // if(!$request){
  38. // $origin = $_SERVER['HTTP_ORIGIN'] ?? '*';
  39. // }else{
  40. // $origin = $request->server('HTTP_ORIGIN') ?? '*';
  41. // }
  42. //
  43. //// $routes = $this->routeService->getAllRoutesWithHierarchy();
  44. // $StateConfig=1;
  45. // if(strstr($origin,"24680.pro")||strstr($origin,"localhost")){
  46. // $StateConfig=2;
  47. // }
  48. $config=self::getChannelConfig($request);
  49. $StateConfig=$config->StateNo;
  50. return $StateConfig;
  51. }
  52. /**
  53. * @param Request|null $request
  54. * @return string
  55. */
  56. public static function getStateToWhereRaw(Request $request=null)
  57. {
  58. $StateConfig=self::getStateConfig($request);
  59. return 'state&'.$StateConfig.'='.$StateConfig;
  60. }
  61. private static $_ChannelConfig=null;
  62. public static function clearChannelConfig(){
  63. self::$_ChannelConfig=null;
  64. }
  65. /**
  66. * @param Request|null $request
  67. * @return WebChannelConfig|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\LaravelIdea\Helper\App\Game\_IH_WebChannelConfig_QB|mixed|object|null
  68. */
  69. public static function getChannelConfig(Request $request=null)
  70. {
  71. if(!is_null(self::$_ChannelConfig)){
  72. return self::$_ChannelConfig;
  73. }
  74. $Channel=self::getChannel($request);
  75. $config=WebChannelConfig::getByChannel($Channel);
  76. if(!$config){
  77. $config=WebChannelConfig::getByChannel(env('REGION_24680_DEFAULT_CHANNEL',100));
  78. if(!$config)$config=WebChannelConfig::query()->first();
  79. }else if($config->SHADOW_CHANNEL()!=$config->Channel){
  80. $config=WebChannelConfig::getByChannel($config->SHADOW_CHANNEL());
  81. }
  82. self::$_ChannelConfig=$config;
  83. return $config;
  84. }
  85. public static function getChannel(Request $request=null)
  86. {
  87. //从参数获取
  88. if(!$request){
  89. $origin = $_SERVER['HTTP_ORIGIN'] ??$_SERVER['HTTP_REFERER']?? '*';
  90. if(isset($_REQUEST['c'])&&!empty($_REQUEST['c'])){
  91. $Channel=$_REQUEST['c'];
  92. $Channel=explode('/',$Channel)[0];
  93. }
  94. }else{
  95. $origin = $request->server('HTTP_ORIGIN') ?? $request->server('HTTP_REFERER') ?? '*';
  96. $Channel=$request->input('c','');
  97. $Channel=explode('/',$Channel)[0];
  98. }
  99. //从用户获取
  100. if(GlobalUserInfo::$me){
  101. $Channel=GlobalUserInfo::$me->Channel;
  102. }else if(isset($_REQUEST['act'])&&!empty($_REQUEST['act'])){
  103. $ActCode = $_REQUEST['act'];
  104. $link = AgentLinks::getByCode($ActCode);
  105. if ($link) {
  106. $Channel = $link->Channel;
  107. }
  108. }
  109. //默认站点
  110. if(!isset($Channel)||!$Channel||$Channel==env('REGION_24680_DEFAULT_CHANNEL',100)) {
  111. if (strstr($origin, "24680.pro") || strstr($origin, "localhost")) {
  112. $Channel = 100;
  113. } else {
  114. $Channel = env('REGION_24680_DEFAULT_CHANNEL',100);
  115. }
  116. }
  117. return $Channel;
  118. }
  119. public static function isTestSite()
  120. {
  121. $origin = $_SERVER['HTTP_ORIGIN'] ??$_SERVER['HTTP_REFERER']?? '*';
  122. // if(isset($_REQUEST['c'])&&!empty($_REQUEST['c'])){
  123. // $Channel=$_REQUEST['c'];
  124. // $Channel=explode('/',$Channel)[0];
  125. // }
  126. if (strstr($origin, "test")) {
  127. // $Channel = 44;
  128. return $origin;
  129. }
  130. return false;
  131. }
  132. public static function isTestOrLocalSite()
  133. {
  134. // return true;
  135. $origin = self::isTestSite();
  136. // if(isset($_REQUEST['c'])&&!empty($_REQUEST['c'])){
  137. // $Channel=$_REQUEST['c'];
  138. // $Channel=explode('/',$Channel)[0];
  139. // }
  140. if (strstr($origin, "localhost")) {
  141. // $Channel = 44;
  142. return $origin;
  143. }
  144. return false;
  145. }
  146. }