WebRouteController.php 29 KB

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