WebRouteController.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. <?php
  2. namespace App\Http\Controllers\Game;
  3. use App\Facade\TableName;
  4. use App\Game\Block;
  5. use App\Game\Config\GameBasicConfig;
  6. use Carbon\Carbon;
  7. use App\Game\GameCard;
  8. use App\Game\GlobalUserInfo;
  9. use App\Game\Route;
  10. use App\Game\RouteModel;
  11. use App\Game\Services\BetbyService;
  12. use App\Game\Services\BetbyTestService;
  13. use App\Game\Services\OuroGameService;
  14. use App\Game\Services\RouteService;
  15. use App\Game\Services\TelegramAppService;
  16. use App\Game\Style;
  17. use App\Game\WebChannelConfig;
  18. use App\Game\WebRegionConfig;
  19. use App\Http\Controllers\Api\ApiController;
  20. use App\Http\Controllers\Api\WeightConfigController;
  21. use App\Http\Controllers\Controller;
  22. use App\Http\helper\NumConfig;
  23. use App\IpLocation;
  24. use App\Models\AccountsInfo;
  25. use App\Models\AccountLoginDomain;
  26. use App\Models\SystemStatusInfo;
  27. use App\Jobs\IpRiskDetection;
  28. use App\Services\ApkService;
  29. use App\Services\VipService;
  30. use App\Util;
  31. use Illuminate\Http\Request;
  32. use Illuminate\Support\Facades\DB;
  33. use Illuminate\Support\Facades\Redis;
  34. // use Yansongda\Pay\Log;
  35. class WebRouteController extends Controller
  36. {
  37. protected $routeService;
  38. public function __construct(RouteService $routeService)
  39. {
  40. $this->routeService = $routeService;
  41. }
  42. private function recordLoginDomain(Request $request, $userId, $channel = 0)
  43. {
  44. $origin = $request
  45. ? ($request->server('HTTP_ORIGIN') ?? $request->server('HTTP_REFERER') ?? '*')
  46. : ($_SERVER['HTTP_ORIGIN'] ?? $_SERVER['HTTP_REFERER'] ?? '*');
  47. try {
  48. AccountLoginDomain::record($userId, $origin, $channel);
  49. } catch (\Throwable $exception) {
  50. \Log::warning('record_login_origin_failed', [
  51. 'UserID' => $userId,
  52. 'origin' => $origin,
  53. 'channel' => $channel,
  54. 'message' => $exception->getMessage(),
  55. ]);
  56. }
  57. }
  58. public function Routes(Request $request)
  59. {
  60. GlobalUserInfo::UpdateLoginDate($request, true);
  61. $FPID = $request->input("bfp", "");
  62. $inApp = $request->input('ia', 0);
  63. // 仅加载顶层路由,并预加载所有嵌套子路由
  64. $routes = RouteModel::whereNull('parent_id')
  65. ->whereRaw(RouteService::getStateToWhereRaw($request))
  66. ->with('subs.subs.subs') // 根据实际层级深度调整
  67. ->orderBy('index')
  68. ->get();
  69. $styles = Style::all();
  70. $blocks = Block::all();
  71. $config = RouteService::getChannelConfig($request);
  72. $guestOpen = $config->isGuestOpen();
  73. $disablePromote = $config->isDisablePromote();
  74. //在fb内,节省时间,不快速注册
  75. //if($inApp)$guestOpen=false;
  76. $upgradeBonus = intval($config->BONUS_VERIFY_PHONE());
  77. if ($guestOpen && !$upgradeBonus) {
  78. //游客模式打开,随时可以登录
  79. $upgradeBonus = SystemStatusInfo::OnlyGetCacheValue('BindPhoneReward') ?? 500;
  80. }
  81. $user = GlobalUserInfo::$me;//LoginController::checkLogin($request);
  82. if ($user) {
  83. $this->recordLoginDomain($request, $user->UserID ?? 0, $user->Channel ?? 0);
  84. $ua = $request->userAgent();
  85. if (stripos($ua, 'iPhone') !== false) {
  86. $mobileBand = 'iPhone';
  87. } else if (stripos($ua, 'iPad') !== false) {
  88. $mobileBand = 'iPad';
  89. } else if (stripos($ua, 'Android') !== false) {
  90. $mobileBand = 'Android';
  91. } else if (stripos($ua, 'Windows') !== false) {
  92. $mobileBand = 'PC';
  93. } else if (stripos($ua, 'Mac') !== false) {
  94. $mobileBand = 'Mac';
  95. }
  96. if (isset($mobileBand)) {
  97. AccountsInfo::saveMobileBand($user->UserID, $mobileBand);
  98. }
  99. }
  100. $hashadd = $request->input("hashadd", "");
  101. $isreg = 0;
  102. if (!empty($hashadd)) {
  103. try {
  104. $hashadd = json_decode($hashadd, true);
  105. if ($hashadd['type'] == 'tele') {
  106. $teleUser = TelegramAppService::decodeHash($hashadd['data']);
  107. if (intval($teleUser->UserID)) {
  108. if (!$user || $user->UserID != $teleUser->UserID) {
  109. $user = GlobalUserInfo::getGameUserInfo('UserID', $teleUser->UserID);
  110. }
  111. } else {
  112. //不存在用户
  113. if (!$user) {
  114. $guestUser = (new LoginController())->registerUser($request, true);
  115. if (!is_array($guestUser)) {
  116. $guestUser->NickName = $teleUser->first_name;
  117. $guestUser->save();
  118. $isreg = 1;
  119. $user = $guestUser;
  120. }
  121. }
  122. if ($user) {
  123. //绑定现有用户
  124. $teleUser->UserID = $user->UserID;
  125. $teleUser->GlobalUID = $user->GlobalUID;
  126. $teleUser->save();
  127. }
  128. }
  129. }
  130. } catch (\Exception $e) {
  131. }
  132. }
  133. if (!$user) {
  134. $loginController = new LoginController();
  135. //游客模式打开,随时可以登录
  136. $user = $loginController->getUserByFPID($FPID);
  137. // if (!$user) {
  138. // $user = $loginController->registerUser($request, true);
  139. // }
  140. }
  141. $FF=$request->input('ff', '');
  142. $isPWA=$request->input('pwa', 0);
  143. $urlvars=json_decode($request->input('urlvars',''));
  144. if(!$user&&!empty($FF)&&$isPWA){
  145. $user=GlobalUserInfo::GetRecentLogin($request);
  146. }
  147. //转换成web数据
  148. //转换成web数据
  149. if ($user){
  150. // 异步派发 IP 风险检测
  151. $userId = $user->UserID ?? 0;
  152. $ip = IpLocation::getRealIp();
  153. if ($ip && $userId) {
  154. IpRiskDetection::dispatch($userId, $ip);
  155. }
  156. $user = GlobalUserInfo::toWebData($user);
  157. $config=WebChannelConfig::getByChannel($user['Channel']);
  158. } else{
  159. Util::WriteLog('routes_params',[$request]);
  160. }
  161. $data=['code'=>0,'data'=>$routes,'blocks'=>$blocks,'styles'=>$styles,'user'=>$user];
  162. $origin = $request->server('HTTP_ORIGIN') ?? $request->server('HTTP_REFERER')?? '*';
  163. $data['origin']=$origin;
  164. $isDesktop=($request->input('_d','m')=='d');
  165. $firstBonus=1;
  166. if(env('CONFIG_24680_NFTD_99',0)==0)if($config->Channel==99)$firstBonus=0;
  167. $registerBonus = SystemStatusInfo::OnlyGetCacheValue('GrantScoreCountNew') ?? 1000;
  168. $chat = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
  169. ->where('StatusName', 'Telegram')
  170. ->first();
  171. $servicelist = (new ApiController())->getServiceList();
  172. // $chat = "https://m.me/930365713484502";
  173. // 默认推荐游戏
  174. $defaultGameId = 931;
  175. $recommendGame = '/game/' . $defaultGameId;
  176. $popPwaBonus=$user?(Redis::get('pwa_bonus:'.$user['UserID'])??0):0;
  177. $ChannelPackageName = DB::table('QPPlatformDB.dbo.ChannelPackageName')->where('Channel',$config->Channel??100)
  178. ->first();
  179. // slotsPartner: 与 WebChannelConfig 通过 RegionID 关联且 RegionID 不为空的区域,且当前 $config 的 Channel 不在该区域的 BindChannels 中(即其他“伙伴”区域)
  180. // $currentChannel = $config->Channel;
  181. // $slotsPartner = WebRegionConfig::query()
  182. // ->where('RegionID', '!=', '')
  183. // ->whereIn('RegionID', function ($q) use ($currentChannel) {
  184. // $q->select('RegionID')
  185. // ->from((new WebChannelConfig())->getTable())
  186. // ->where('RegionID', '!=', '')
  187. // ->where('Channel', '!=', $currentChannel);
  188. // })
  189. // ->get()
  190. //// ->filter(function ($region) use ($currentChannel) {
  191. //// $bindChannels = $region->BindChannels;
  192. //// return !is_array($bindChannels) || !in_array((int)$currentChannel, $bindChannels);
  193. //// })
  194. // ->map(function ($region) {
  195. // return [
  196. // 'DomainUrl' => $region->DomainUrl ?? '',
  197. // 'LogoUrl' => $region->LogoUrl ?? '',
  198. // 'GameDesc' => $region->GameDesc ?? '',
  199. // ];
  200. // })
  201. // ->values()
  202. // ->all();
  203. $GroupID=0;
  204. $RegionID = $request ? $request->input('regionid', '') : ($_REQUEST['regionid'] ?? '');
  205. if (empty($RegionID) && !empty($origin)) {
  206. $RegionID = explode('.', $origin)[str_starts_with($origin, 'www') ? 1 : 0];
  207. }
  208. if(!empty($RegionID)){
  209. $GroupID=WebRegionConfig::query()->where('RegionID',$RegionID)->value('GroupID')??0;
  210. }
  211. // slotsPartner: 与 WebChannelConfig 通过 RegionID 关联且 RegionID 不为空的区域,且当前 $config 的 Channel 不在该区域的 BindChannels 中(即其他“伙伴”区域)
  212. // $currentChannel = $config->Channel;
  213. $slotsPartner = WebRegionConfig::query()
  214. ->where('GroupID', $GroupID)
  215. ->whereIn('RegionID', function ($q) {
  216. $q->select('RegionID')
  217. ->from((new WebChannelConfig())->getTable())
  218. ->where('RegionID', '!=', '');
  219. })
  220. ->get()
  221. ->map(function ($region) {
  222. return [
  223. 'DomainUrl' => $region->DomainUrl ?? '',
  224. 'LogoUrl' => $region->LogoUrl ?? '',
  225. 'GameDesc' => $region->GameDesc ?? '',
  226. 'SC' => $region->SuggestChannel ?? '',
  227. ];
  228. })
  229. ->values()
  230. ->all();
  231. // sharePop:用户注册日起满 3 个自然日后开始弹(例:1 日注册则 4 日及以后满足)
  232. $sharePop = 0;
  233. if ($user && isset($user['UserID'])) {
  234. $userModel = GlobalUserInfo::getGameUserInfo('UserID', $user['UserID']);
  235. if ($userModel && $userModel->RegisterDate) {
  236. $registerDate = Carbon::parse($userModel->RegisterDate)->startOfDay();
  237. $today = Carbon::today();
  238. $user = GlobalUserInfo::toWebData($userModel);
  239. if ($today->gte($registerDate->copy()->addDays(3)) && $user['vip'] > 0) {
  240. $sharePop = 1;
  241. } else if ($user['vip'] == 0 && time() > strtotime($userModel->RegisterDate) + 1800) {
  242. $sharePop = 1;
  243. }
  244. }
  245. }
  246. $spe_key=$request->input('s_k', 0);
  247. $data['conf']=[
  248. 'hall'=>env("CONFIG_24680_HALL")??GameBasicConfig::$HallServer,
  249. 'DOLLAR'=>env("CONFIG_24680_DOLLAR")??GameBasicConfig::$DOLLAR,
  250. 'currency'=>env("CONFIG_24680_CURRENCY","USD"),
  251. 'promoteInstall'=>$disablePromote?0:($inApp?1:((RouteService::isTestOrLocalSite()||$isDesktop)?0:25)),
  252. 'showInstall'=>$disablePromote?0:($inApp?1:((RouteService::isTestOrLocalSite()||$isDesktop)?0:25)),
  253. 'guest'=>$guestOpen?1:0,
  254. 'AdjustToken' => $ChannelPackageName?$ChannelPackageName->AdjustToken:null,
  255. 'AdjustConfig' => $ChannelPackageName?$ChannelPackageName->AdjustConfig:null,
  256. 'upgradeBonus'=>$upgradeBonus,
  257. 'registerBonus' =>$registerBonus,
  258. 'recommendGame' => $recommendGame,
  259. 'LandscapeGames' => [962,963,964,965,966,967,972,973,974,975, 976, 977, 978, 979, 980, 982, 983,
  260. 962, 941, 942, 943, 945, 946, 947, 948, 949, 950, 951, 952, 953,
  261. 936, 938, 939,940,934],
  262. 'getStateToWhereRaw' =>RouteService::getStateToWhereRaw($request),
  263. // 'serviceLink' => $chat,
  264. 'serviceLink' => $chat?$chat->StatusString:'https://m.me/930365713484502',
  265. 'cs' => $servicelist,
  266. 'vipConfig' => VipService::getVipLevelConfig(),
  267. 'popWheel'=>0,
  268. 'firstBonus'=>$firstBonus,
  269. 'popFirst'=>$firstBonus,
  270. 'openRelief'=>$firstBonus,
  271. 'popBindPhone'=>1,
  272. 'popPwaBonus' => $popPwaBonus,
  273. 'jumplater' => $config->isFbJumpLater(),
  274. 'download'=>['light'=>$config->LightApk,'full'=>$config->FullApk,'bonus'=>$config->BONUS_PWA()],
  275. 'registerOpen'=>$config->RegOpen??env('CONFIG_REG_OPEN','sms,mail'),//id,phone,sms,mail,guest
  276. 'loginOpen'=>$config->LoginOpen??'id,phone,sms,mail,guest',
  277. 'slotsPartner' => $slotsPartner,
  278. 'outLimit' => ['cashapp' => 2000,'paypal' => 2000],
  279. 'withdrawChannel' => ['cashapp','paypal'],
  280. 'freeChannel' => ['paypal'],
  281. 'sharePop' => $sharePop,
  282. 's_k' => $this->quickLoad($spe_key, $user['UserID']??0, $FPID, $FF, $request->input('cookie', ''))??'',
  283. 'weight1234' => WeightConfigController::getConfig(),
  284. ];
  285. $data['conf']['pf']=['type'=>$config->PlatformName,'id'=>$config->PlatformID];
  286. // if (!$user){
  287. Util::WriteLog('routes_rs',[$data['conf']]);
  288. // }
  289. // $data['request']=$request->all();
  290. return response()->json($data);
  291. }
  292. public function getRegisterGold(Request $request)
  293. {
  294. try {
  295. $user = $request->user();
  296. $UserID = $user->UserID;
  297. if ($user->Registed == 1) {
  298. return apiReturnFail('Fail');
  299. }
  300. // 添加金币(10金币)
  301. $addResult = OuroGameService::AddScore($UserID, 10 * NumConfig::NUM_VALUE, null, false);
  302. // 更新 webgame.GlobalUserInfo 的 Registed 字段
  303. DB::connection('mysql')->table('webgame.GlobalUserInfo')
  304. ->where('UserID', $UserID)
  305. ->update(['Registed' => 1]);
  306. // 更新 QPAccountsDB.dbo.AccountsInfo 的 Registed 字段
  307. DB::connection('write')->table('QPAccountsDB.dbo.AccountsInfo')
  308. ->where('UserID', $UserID)
  309. ->update(['Registed' => 1]);
  310. return apiReturnSuc([
  311. 'user' => [
  312. 'InsureScore' => 10,
  313. 'Registed' => 1,
  314. 'message' => 'Success'
  315. ]
  316. ]);
  317. } catch (\Exception $e) {
  318. \Log::error('注册送金币失败:' . $e->getMessage(), [
  319. 'UserID' => $UserID ?? 0,
  320. 'trace' => $e->getTraceAsString()
  321. ]);
  322. return apiReturnFail('领取失败:' . $e->getMessage());
  323. }
  324. }
  325. public function log(Request $request)
  326. {
  327. Util::writeLog("gamelog", [
  328. 'user' => $request->user(),
  329. 'request' => $request->all()
  330. ]);
  331. return apiReturnSuc();
  332. }
  333. public function checkApkInstall(Request $request)
  334. {
  335. $user = $request->user();
  336. $FPID = $request->input("bfp", "");
  337. $ff = $request->input('ff', '');
  338. $url_sign = $request->input('us', RouteService::getChannel($request));
  339. $UserID = $user ? $user->UserID : "";
  340. $ip = $request->ip();
  341. $agent = $request->userAgent();
  342. $alen = strlen($agent);
  343. $key = "apktmp_{$url_sign}_$ip";
  344. Util::writeLog("apkload", [
  345. 'FPID' => $FPID,
  346. 'FF' => $ff,
  347. 'url_sign' => $url_sign,
  348. 'user' => $user,
  349. 'ip' => IpLocation::getRealIp(),
  350. 'agent' => $agent,
  351. 'req' => $request->all()
  352. ]);
  353. $agent = explode('AppleWebKit', $agent)[0];
  354. //截取到最后一个分号Mozilla/5.0 (Linux; Android 16; SM-S936U Build/BP2A.250605.031.A3; wv) 去掉了wv和后面
  355. $lastSemicolon = strrpos($agent, ';');
  356. if ($lastSemicolon !== false) {
  357. $agent = substr($agent, 0, $lastSemicolon);
  358. }
  359. $cookieExist = ApkService::loadCookie($UserID, $FPID, $ff, $request->input('cookie', ''));
  360. if ($cookieExist && is_array($cookieExist)) {
  361. $data = [];
  362. $data['cookie'] = $cookieExist['Cookie'] ?? "";
  363. $data['params'] = $cookieExist['Params'] ?? "";
  364. $data['ls'] = $cookieExist['LocalStorage'] ?? "";
  365. $data['us'] = $cookieExist['UrlSign'] ?? "";
  366. $data['type'] = $cookieExist['Platform'] ?? "";
  367. $data['agent'] = $cookieExist['ClickUA'] ?? "";
  368. $data['origin'] = $_SERVER['HTTP_ORIGIN'] ?? $_SERVER['HTTP_REFERER'] ?? '*';
  369. Util::writeLog("apkload", "existUser:::" . json_encode($data));
  370. return apiReturnSuc($data);
  371. }
  372. $datas = [];
  373. if (Redis::exists($key)) {
  374. $datas = json_decode(Redis::get($key), true);
  375. //规则1,只有一个数据,直接归1
  376. if (count($datas) == 1) {
  377. Redis::del($key);
  378. ApkService::saveCookie($UserID, $datas[0], $FPID, $ff);
  379. Util::writeLog("apkload", "onlyone:::" . json_encode($datas[0]));
  380. return apiReturnSuc($datas[0]);
  381. }
  382. Util::WriteLog("apkload", $datas);
  383. foreach ($datas as $k => $v) {
  384. if (strstr($v['agent'], $agent) || $ff == $v['ff']) {
  385. array_splice($datas, $k, 1);
  386. Redis::set($key, json_encode($datas));
  387. Redis::expire($key, 7200);
  388. ApkService::saveCookie($UserID, $v, $FPID, $ff);
  389. Util::writeLog("apkload", "sameagent:::" . json_encode($v));
  390. return apiReturnSuc($v);
  391. }
  392. }
  393. }
  394. $recents = ApkService::getRecentsNew($url_sign);
  395. foreach ($recents as $v) {
  396. if (strstr($v['agent'], $agent) || $ff == $v['ff']) {
  397. ApkService::saveCookie($UserID, $v, $FPID, $ff);
  398. Util::writeLog("apkload", "recent:::" . json_encode($v));
  399. return apiReturnSuc($v);
  400. }
  401. }
  402. return apiReturnFail("");
  403. }
  404. public function quickLoad($spe_key=null, $UserID=0, $FPID='', $FF='', $cookie='')
  405. {
  406. $data=null;
  407. $fbclid = ApkService::extractFbclid($cookie);
  408. if($spe_key){
  409. $key = "quick_{$spe_key}";
  410. if(Redis::exists($key)) {
  411. $data = json_decode(Redis::get($key), true);
  412. }
  413. }
  414. if(!$data){
  415. $table = TableName::QPAccountsDB() . "AccountCookie";
  416. $obj=null;
  417. if($spe_key) {
  418. $candidates = DB::table($table)->where('SPE_KEY', $spe_key)->orderBy('CreateTime', 'desc')->get();
  419. $obj = ApkService::pickBestCandidate($candidates, $fbclid);
  420. }
  421. if(!$obj && $UserID){
  422. $candidates = DB::table($table)->where('UserID', $UserID)->orderBy('CreateTime', 'desc')->get();
  423. $obj = ApkService::pickBestCandidate($candidates, $fbclid);
  424. }
  425. if(!$obj && (!empty($FPID) || !empty($FF))){
  426. $query = DB::table($table);
  427. if (!empty($FPID) && !empty($FF)) {
  428. $query->where(function ($q) use ($FPID, $FF) {
  429. $q->where('FPID', $FPID)->orWhere('FF', $FF);
  430. });
  431. } elseif (!empty($FPID)) {
  432. $query->where('FPID', $FPID);
  433. } else {
  434. $query->where('FF', $FF);
  435. }
  436. $candidates = $query->orderBy('CreateTime', 'desc')->get();
  437. $obj = ApkService::pickBestCandidate($candidates, $fbclid);
  438. }
  439. try {
  440. if(!$obj && !empty($fbclid)){
  441. $obj = DB::table($table)
  442. ->where('Cookie', 'like', '%' . $fbclid . '%')
  443. ->orderBy('CreateTime', 'desc')
  444. ->first();
  445. }
  446. }catch (\Exception $e) {
  447. Util::WriteLog('invalid_cookie',$fbclid);
  448. $obj = null;
  449. }
  450. if($obj){
  451. $data=['type'=>$obj->Platform,'cookie'=>$obj->Cookie,'s_k'=>$obj->SPE_KEY, 'url_sign'=>$obj->UrlSign, 'params'=>$obj->Params, 'ff'=>$obj->FF, 'localStorage'=>$obj->LocalStorage];
  452. }
  453. }
  454. return $data;
  455. }
  456. public function quickSave(Request $request)
  457. {
  458. $FPID = $request->input("bfp", "");
  459. $ff = $request->input('ff', '');
  460. $url_sign = RouteService::getChannel($request);
  461. $UserID =$request->user()?$request->user()->UserID:0;
  462. $ip = IpLocation::getRealIp();
  463. $agent = $request->userAgent();
  464. ///gg or fb
  465. $type = $request->get('type') ?? "fb";
  466. $cookie = $request->get('cookie') ?? '';
  467. $localStorage = $request->get('ls') ?? '';
  468. $params = $request->get('params') ?? '';
  469. $origin = $_SERVER['HTTP_ORIGIN'] ?? $_SERVER['HTTP_REFERER'] ?? '*';
  470. $time = time();
  471. $spe_key = $request->input('s_k',0);
  472. if(!$spe_key)$spe_key=$this->md5Base62($cookie . $localStorage . $params);
  473. $locale = $request->get('locale') ?? '';
  474. $data = compact('ip', 'agent', 'cookie', 'type', 'url_sign', 'time', 'params', 'locale', 'origin', 'ff', 'localStorage');
  475. $key = "quick_{$spe_key}";
  476. if (Redis::exists($key)) {
  477. $cached = json_decode(Redis::get($key), true);
  478. if (is_array($cached)) {
  479. $data = $this->mergeQuickData($cached, $data);
  480. }
  481. }
  482. ApkService::saveCookie($UserID, $data, $FPID, $ff,$spe_key);
  483. Redis::set($key, json_encode($data));
  484. Redis::expire($key, 7200);
  485. Util::WriteLog("saveQuick", $data);
  486. return apiReturnSuc(['s_k'=>$spe_key]);
  487. }
  488. public function saveEnv(Request $request)
  489. {
  490. $user = $request->user();
  491. $FPID = $request->input("bfp", "");
  492. $ff = $request->input('ff', '');
  493. $url_sign = $request->input('us', RouteService::getChannel($request));
  494. $UserID = $user ? $user->UserID : "";
  495. $ip = IpLocation::getRealIp();
  496. $agent = $request->userAgent();
  497. $alen = strlen($agent);
  498. $key = "apktmp_{$url_sign}_$ip";
  499. ///gg or fb
  500. $type = $request->get('type') ?? "fb";
  501. $cookie = $request->get('cookie') ?? '';
  502. $localStorage = $request->get('ls') ?? '';
  503. $params = $request->get('params') ?? '';
  504. $origin = $_SERVER['HTTP_ORIGIN'] ?? $_SERVER['HTTP_REFERER'] ?? '*';
  505. $time = time();
  506. $locale = $request->get('locale') ?? '';
  507. $data = compact('ip', 'agent', 'cookie', 'type', 'url_sign', 'time', 'params', 'locale', 'origin', 'ff', 'localStorage');
  508. $cookieExist = ApkService::loadCookie($UserID, $FPID, $ff, $cookie);
  509. if (!$cookieExist) {
  510. ApkService::saveCookie($UserID, $data, $FPID, $ff);
  511. $key = "apktmp_{$url_sign}_$ip";
  512. $datas = [];
  513. if (Redis::exists($key)) {
  514. $datas = json_decode(Redis::get($key), true);
  515. //防止重复压入
  516. foreach ($datas as $v) {
  517. if ($data['agent'] == $v['agent'] && $data['type'] == $v['type']) {
  518. return apiReturnSuc(1);
  519. }
  520. }
  521. }
  522. array_unshift($datas, $data);
  523. Redis::set($key, json_encode($datas));
  524. Redis::expire($key, 7200);
  525. //压入最近记录
  526. ApkService::addRecentsNew($data, $url_sign);
  527. //写入快手
  528. if ($type == 'kw') {
  529. ApkService::sendToKwai(json_decode($cookie, true), ApkService::KWAI_EVENT['EVENT_DOWNLOAD']);
  530. }
  531. Util::WriteLog("saveEnv", $data);
  532. }
  533. return apiReturnSuc(1);
  534. }
  535. private function mergeQuickData(array $cached, array $current)
  536. {
  537. $merged = $cached;
  538. foreach ($current as $k => $v) {
  539. if ($k === 'cookie' || $k === 'params') {
  540. continue;
  541. }
  542. if ($v !== '' && $v !== null) {
  543. $merged[$k] = $v;
  544. }
  545. }
  546. $merged['cookie'] = $this->mergeCookieString($cached['cookie'] ?? '', $current['cookie'] ?? '');
  547. $merged['params'] = $this->mergeParamsString($cached['params'] ?? '', $current['params'] ?? '');
  548. return $merged;
  549. }
  550. private function mergeParamsString($cached, $current)
  551. {
  552. if ($cached === '') return $current;
  553. if ($current === '') return $cached;
  554. $cachedJson = json_decode($cached, true);
  555. $currentJson = json_decode($current, true);
  556. if (is_array($cachedJson) && is_array($currentJson)) {
  557. return json_encode(array_replace_recursive($cachedJson, $currentJson), JSON_UNESCAPED_UNICODE);
  558. }
  559. $cachedArr = [];
  560. $currentArr = [];
  561. parse_str($cached, $cachedArr);
  562. parse_str($current, $currentArr);
  563. if (!empty($cachedArr) || !empty($currentArr)) {
  564. return http_build_query(array_replace($cachedArr, $currentArr));
  565. }
  566. if ($cached === $current) return $current;
  567. return $cached . '&' . $current;
  568. }
  569. private function mergeCookieString($cached, $current)
  570. {
  571. if ($cached === '') return $current;
  572. if ($current === '') return $cached;
  573. $cachedJson = json_decode($cached, true);
  574. $currentJson = json_decode($current, true);
  575. if (is_array($cachedJson) && is_array($currentJson)) {
  576. return json_encode(array_replace_recursive($cachedJson, $currentJson), JSON_UNESCAPED_UNICODE);
  577. }
  578. $cookies = $this->parseCookiePairs($cached);
  579. $newCookies = $this->parseCookiePairs($current);
  580. if (!empty($cookies) || !empty($newCookies)) {
  581. $cookies = array_replace($cookies, $newCookies);
  582. $cookieParts = [];
  583. foreach ($cookies as $k => $v) {
  584. $cookieParts[] = $k . '=' . $v;
  585. }
  586. return implode('; ', $cookieParts);
  587. }
  588. if ($cached === $current) return $current;
  589. return $cached . '; ' . $current;
  590. }
  591. private function parseCookiePairs($cookieStr)
  592. {
  593. $pairs = [];
  594. foreach (explode(';', $cookieStr) as $segment) {
  595. $segment = trim($segment);
  596. if ($segment === '') {
  597. continue;
  598. }
  599. $pos = strpos($segment, '=');
  600. if ($pos === false) {
  601. continue;
  602. }
  603. $name = trim(substr($segment, 0, $pos));
  604. $value = trim(substr($segment, $pos + 1));
  605. if ($name !== '') {
  606. $pairs[$name] = $value;
  607. }
  608. }
  609. return $pairs;
  610. }
  611. /**
  612. * 环境短 key:MD5 二进制取前 6 字节(48bit)再 base62,长度 <=9(10 字符以内)。
  613. * PHP 对齐示例:$b = substr(md5($value, true), 0, 6); 再对 $b 做相同 base62 循环。
  614. */
  615. private function md5Base62($value)
  616. {
  617. $b = substr(md5($value, true), 0, 6);
  618. $bytes = array_values(unpack('C*', $b));
  619. $alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  620. $result = '';
  621. while (!empty($bytes)) {
  622. $quotient = [];
  623. $remainder = 0;
  624. foreach ($bytes as $byte) {
  625. $acc = ($remainder << 8) + $byte;
  626. $q = intdiv($acc, 62);
  627. $remainder = $acc % 62;
  628. if (!empty($quotient) || $q !== 0) {
  629. $quotient[] = $q;
  630. }
  631. }
  632. $result = $alphabet[$remainder] . $result;
  633. $bytes = $quotient;
  634. }
  635. return $result === '' ? '0' : $result;
  636. }
  637. public function SaveRoutes(Request $request)
  638. {
  639. // Assuming $jsonData is your JSON data
  640. $jsonData = json_decode(file_get_contents('path_to_your_json_file.json'), true);
  641. foreach ($jsonData['data'] as $routeData) {
  642. $this->insertRoute($routeData);
  643. }
  644. }
  645. function insertRoute($routeData, $parentId = null)
  646. {
  647. $route = new RouteModel([
  648. 'parent_id' => $parentId,
  649. 'path' => $routeData['path'],
  650. 'type' => $routeData['type'],
  651. 'side' => $routeData['side'],
  652. 'block' => $routeData['block'],
  653. 'title' => $routeData['title'],
  654. 'icon' => $routeData['icon'],
  655. 'fill' => $routeData['fill'],
  656. 'component' => $routeData['component'],
  657. 'query' => $routeData['query'],
  658. 'login' => $routeData['login'],
  659. 'lpath' => $routeData['lpath']
  660. ]);
  661. $route->save();
  662. foreach ($routeData['subs'] as $sub) {
  663. $this->insertRoute($sub, $route->id);
  664. }
  665. }
  666. public function testScoreChange(Request $request)
  667. {
  668. $user = $request->user();
  669. $nowGolds = $request->input("nowGolds", 4000);
  670. $AddNum = $request->input("AddNum", 1000);
  671. // notifyWebHall($UserID,"",'pay_finish',["Golds"=>$NowScore,"PayNum"=>$GiftScore]);
  672. OuroGameService::notifyWebHall($user->UserID, "", 'call_client', ["Golds" => $nowGolds, "AddNum" => $AddNum, "type" => "start_change"]);
  673. // ($user_id,$GlobalUID,'call_client',["type"=>"refresh_mail"]);
  674. return apiReturnSuc("");
  675. }
  676. }