WebRouteController.php 27 KB

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