ApkService.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. <?php
  2. namespace App\Services;
  3. use App\Facade\TableName;
  4. use App\Http\helper\HttpCurl;
  5. use App\Http\helper\NumConfig;
  6. use App\IpLocation;
  7. use App\Models\AccountsSource;
  8. use App\Util;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Facades\Redis;
  12. /**
  13. * AccountCookie 广告归因服务。
  14. *
  15. * 核心职责:管理 AccountCookie 表,将用户的广告点击(Facebook / Google / Kwai)
  16. * 与注册账号关联起来,用于后续的 S2S 事件上报(CAPI / Kwai / Google Offline Conversion)。
  17. *
  18. * 关键概念:
  19. * - AccountCookie:落地页 JS 上报的广告点击信息(_fbc、fbclid、gclid 等),存入 DB。
  20. * - FPID (BrowserFingerPrintID):浏览器指纹 ID,前端通过 FingerprintJS 生成。
  21. * - FF (FontFingerprint):字体指纹哈希,辅助设备识别。
  22. * - SPE_KEY:quickSave 时生成的短哈希,用于跨页面快速匹配。
  23. * - fbclid:Facebook 广告点击 ID,从 _fbc cookie 中提取(格式 fb.1.{ts}.{fbclid}),
  24. * 同一次广告点击在不同浏览器中 fbclid 相同,是跨浏览器匹配的关键依据。
  25. * - UrlSign:渠道号(Channel),用于区分不同推广渠道。
  26. *
  27. * 数据流:
  28. * 1. 用户点击广告 → 落地页 JS 调用 saveEnv/quickSave → saveCookie() 写入 AccountCookie(UserID=0)
  29. * 2. 用户注册 → bindCookie() 将注册的 UserID 回填到匹配的 AccountCookie 记录
  30. * 3. 用户登录/充值 → loadCookie() 读取 AccountCookie → AfEvent 上报 Facebook CAPI
  31. *
  32. * 匹配安全原则:
  33. * - "尽量不绑错" 优先于 "尽量绑上"
  34. * - 绝不覆盖已绑定其他 UserID 的记录,避免串号
  35. * - 跨渠道纠偏必须满足:唯一命中(或 fbclid 精准匹配) + 时间窗口内
  36. */
  37. class ApkService
  38. {
  39. /**
  40. * 按 UserID 更新已有的 AccountCookie 记录(充值/首存等事件触发后更新 UniqueAction)。
  41. * 仅用于已确定 UserID 的场景,不涉及指纹匹配,安全无串号风险。
  42. */
  43. public static function updateCookie($UserID, $arr)
  44. {
  45. DB::table(TableName::QPAccountsDB() . 'AccountCookie')->updateOrInsert(['UserID' => $UserID], $arr);
  46. $key = "AccountCookie_$UserID";
  47. Redis::set($key, json_encode($arr));
  48. Redis::expire($key, 36000);
  49. }
  50. /**
  51. * 保存广告点击的 Cookie 数据到 AccountCookie 表。
  52. *
  53. * 查重优先级:SPE_KEY > UserID > FPID > FF。
  54. * 安全保护:如果通过 FPID/FF 找到的记录已绑定了其他 UserID,
  55. * 不覆盖旧记录,而是插入新记录,防止串号。
  56. */
  57. public static function saveCookie($UserID, $data, $FPID = '', $FF = '',$SPE_KEY='')
  58. {
  59. $agent = $_SERVER['HTTP_USER_AGENT'];
  60. $table = TableName::QPAccountsDB() . 'AccountCookie';
  61. $checkKey = "";
  62. $checkValue = "";
  63. if (!empty($SPE_KEY)) {
  64. $checkKey = 'SPE_KEY';
  65. $checkValue = $SPE_KEY;
  66. } else if (!empty($UserID)) {
  67. $checkKey = 'UserID';
  68. $checkValue = $UserID;
  69. } else if (!empty($FPID)) {
  70. $checkKey = 'FPID';
  71. $checkValue = $FPID;
  72. } else if (!empty($FF)) {
  73. $checkKey = 'FF';
  74. $checkValue = $FF;
  75. }
  76. if (empty($checkKey)) return;
  77. $existing = DB::table($table)->where($checkKey, $checkValue)->first();
  78. if(isset($data['localStorage'])&&!empty($data['localStorage'])){
  79. $ls=json_decode($data['localStorage'],true);
  80. if(isset($ls['user']))unset($ls['user']);
  81. $data['localStorage']=json_encode($ls);
  82. }
  83. $arr = [
  84. 'UserID' => $UserID,
  85. 'FPID' => $FPID,
  86. 'FF' => $FF,
  87. 'SPE_KEY' => $SPE_KEY,
  88. 'Cookie' => $data['cookie'],
  89. 'Params' => $data['params'],
  90. 'LocalStorage' => $data['localStorage']?? "",
  91. 'UrlSign' => $data['url_sign'],
  92. 'Platform' => $data['type'],
  93. 'GameUA' => $agent,
  94. 'ClickUA' => $data['agent'],
  95. 'Origin' => $data['origin'] ?? "",
  96. 'Locale' => $data['locale'] ?? $_SERVER['HTTP_ACCEPT_LANGUAGE'],
  97. 'ClickTime' => date('Y-m-d H:i:s', $data['time']),
  98. 'UniqueAction' => 2,
  99. 'IP' => IpLocation::getIP(),
  100. ];
  101. if (!$existing) {
  102. DB::table($table)->insert($arr);
  103. if (!empty($UserID)) {
  104. if (strstr($arr['Params'], 'gclid') || strstr($arr['Params'], 'fbclid')) {
  105. (new AccountsSource())->notExistsInsert($UserID, "APK_AD(" . $arr['Platform'] . ")");
  106. }
  107. }
  108. if ($arr['Platform'] == 'kw') {
  109. $cookie = $arr['Cookie'];
  110. }
  111. if ($arr['Platform'] == 'gg') {
  112. $cookie = $arr['Params'];
  113. }
  114. }else if(!empty($data['cookie'])){
  115. $existingUserID = $existing->UserID ?? 0;
  116. if (!empty($existingUserID) && $existingUserID != 0
  117. && !empty($UserID) && $UserID != $existingUserID) {
  118. $arr['UserID'] = $UserID;
  119. DB::table($table)->insert($arr);
  120. return;
  121. }
  122. $shouldInsertNew = false;
  123. if (in_array($checkKey, ['FPID', 'FF'])) {
  124. $newFbclid = self::extractFbclid($data['cookie'] ?? '');
  125. $existingFbclid = self::extractFbclid($existing->Cookie ?? '');
  126. $newChannel = $data['url_sign'] ?? '';
  127. $existingChannel = $existing->UrlSign ?? '';
  128. if (!empty($newFbclid) && !empty($existingFbclid) && $newFbclid !== $existingFbclid) {
  129. $shouldInsertNew = true;
  130. } elseif (!empty($newChannel) && !empty($existingChannel) && $newChannel !== $existingChannel) {
  131. $newParamsFbclid = '';
  132. $existingParamsFbclid = '';
  133. $newParams = json_decode($data['params'] ?? '', true);
  134. $existingParams = json_decode($existing->Params ?? '', true);
  135. if (is_array($newParams)) $newParamsFbclid = $newParams['fbclid'] ?? '';
  136. if (is_array($existingParams)) $existingParamsFbclid = $existingParams['fbclid'] ?? '';
  137. if (!empty($newParamsFbclid) && !empty($existingParamsFbclid) && $newParamsFbclid !== $existingParamsFbclid) {
  138. $shouldInsertNew = true;
  139. }
  140. }
  141. }
  142. if ($shouldInsertNew) {
  143. DB::table($table)->insert($arr);
  144. } else {
  145. if (!empty($existingUserID) && $existingUserID != 0) {
  146. $arr['UserID'] = $existingUserID;
  147. }
  148. DB::table($table)->where('ID', $existing->ID)->update($arr);
  149. }
  150. }
  151. }
  152. /**
  153. * 注册后将 UserID 回填到匹配的 AccountCookie 记录。
  154. *
  155. * 两阶段匹配:
  156. * 1. 同渠道(sameChannel):UrlSign == Channel,匹配即绑。
  157. * 2. 跨渠道纠偏(crossChannel):FBIOS → Safari/PWA 跳转导致渠道不一致时的兜底。
  158. * 额外约束:唯一命中(或 fbclid 精准匹配) + 时间窗口内。
  159. * 成功后把用户 Channel 纠正为 Cookie 的 UrlSign(同步 GlobalUserInfo + AccountsInfo)。
  160. *
  161. * 匹配器优先级(高→低):
  162. * SPE_KEY > FPID+FF+IP > FPID+FF > FPID+IP > FF+IP > FPID > FF > IP(仅跨渠道,窗口5min)
  163. *
  164. * 多条候选时用 fbclid 精准匹配,匹配不到则取最新。
  165. *
  166. * @param object &$user GlobalUserInfo(引用,跨渠道命中会修改 Channel)
  167. * @param string $SPE_KEY quickSave 生成的特征哈希
  168. * @param string $fbclid 从 _fbc cookie 提取的 Facebook 点击 ID
  169. * @param string $cookie 客户端上报的完整 cookie 串(与 API 域名不同域时需 body 传参)
  170. * @return object|false 更新后的 AccountCookie 记录,或 false
  171. */
  172. public static function bindCookie(&$user, $SPE_KEY='', $fbclid='', $cookie='')
  173. {
  174. // 注册成功后回填 AccountCookie 的 UserID。
  175. // 这里的目标不是“尽量绑上”,而是“尽量不绑错”。
  176. $UserID=$user->UserID;
  177. $FPID=$user->FPID;
  178. $FF=$user->FF;
  179. $IP=$user->RegisterIP;
  180. $Channel = $user->Channel ?? '';
  181. $registerTime = !empty($user->RegisterDate) ? strtotime($user->RegisterDate) : time();
  182. if (empty($UserID)) {
  183. return false;
  184. }
  185. $logContext = [
  186. 'user_id' => $UserID,
  187. 'channel' => $Channel,
  188. 'fpid' => $FPID,
  189. 'ff' => $FF,
  190. 'ip' => $IP,
  191. 'spe_key' => $SPE_KEY,
  192. 'fbclid' => $fbclid,
  193. 'cookie_has_fbc' => is_string($cookie) && strpos($cookie, '_fbc') !== false,
  194. 'register_date' => $user->RegisterDate ?? '',
  195. ];
  196. $table = TableName::QPAccountsDB() . 'AccountCookie';
  197. $updateArr = ['UserID' => $UserID];
  198. if (!empty($FPID)) {
  199. $updateArr['FPID'] = $FPID;
  200. }
  201. if (!empty($FF)) {
  202. $updateArr['FF'] = $FF;
  203. }
  204. if (!empty($SPE_KEY)) {
  205. $updateArr['SPE_KEY'] = $SPE_KEY;
  206. }
  207. $baseQuery = function () use ($table) {
  208. // 只允许回填原始未绑定用户的数据,避免覆盖已经有 UserID 的老记录。
  209. return DB::table($table)
  210. ->where('UserID', 0);
  211. };
  212. $sameChannelMatchers = [];
  213. $crossChannelMatchers = [];
  214. // 匹配强度从高到低排列:越靠前越可信。
  215. // 先跑“同渠道”命中;只有完全找不到时,才尝试“跨渠道纠偏”。
  216. if (!empty($SPE_KEY)) {
  217. $sameChannelMatchers[] = ['label' => 'same_channel:SPE_KEY', 'query' => $baseQuery()->where('SPE_KEY', $SPE_KEY)];
  218. $crossChannelMatchers[] = ['label' => 'cross_channel:SPE_KEY', 'query' => $baseQuery()->where('SPE_KEY', $SPE_KEY)];
  219. }
  220. if (!empty($FPID) && !empty($FF) && !empty($IP)) {
  221. $sameChannelMatchers[] = ['label' => 'same_channel:FPID+FF+IP', 'query' => $baseQuery()->where('FPID', $FPID)->where('FF', $FF)->where('IP', $IP)];
  222. $crossChannelMatchers[] = ['label' => 'cross_channel:FPID+FF+IP', 'query' => $baseQuery()->where('FPID', $FPID)->where('FF', $FF)->where('IP', $IP)];
  223. }
  224. if (!empty($FPID) && !empty($FF)) {
  225. $sameChannelMatchers[] = ['label' => 'same_channel:FPID+FF', 'query' => $baseQuery()->where('FPID', $FPID)->where('FF', $FF)];
  226. $crossChannelMatchers[] = ['label' => 'cross_channel:FPID+FF', 'query' => $baseQuery()->where('FPID', $FPID)->where('FF', $FF)];
  227. }
  228. if (!empty($FPID) && !empty($IP)) {
  229. $sameChannelMatchers[] = ['label' => 'same_channel:FPID+IP', 'query' => $baseQuery()->where('FPID', $FPID)->where('IP', $IP)];
  230. $crossChannelMatchers[] = ['label' => 'cross_channel:FPID+IP', 'query' => $baseQuery()->where('FPID', $FPID)->where('IP', $IP)];
  231. }
  232. if (!empty($FF) && !empty($IP)) {
  233. $sameChannelMatchers[] = ['label' => 'same_channel:FF+IP', 'query' => $baseQuery()->where('FF', $FF)->where('IP', $IP)];
  234. $crossChannelMatchers[] = ['label' => 'cross_channel:FF+IP', 'query' => $baseQuery()->where('FF', $FF)->where('IP', $IP)];
  235. }
  236. if (!empty($FPID)) {
  237. $sameChannelMatchers[] = ['label' => 'same_channel:FPID', 'query' => $baseQuery()->where('FPID', $FPID)];
  238. $crossChannelMatchers[] = ['label' => 'cross_channel:FPID', 'query' => $baseQuery()->where('FPID', $FPID)];
  239. }
  240. if (!empty($FF)) {
  241. $sameChannelMatchers[] = ['label' => 'same_channel:FF', 'query' => $baseQuery()->where('FF', $FF)];
  242. $crossChannelMatchers[] = ['label' => 'cross_channel:FF', 'query' => $baseQuery()->where('FF', $FF)];
  243. }
  244. if (!empty($IP)) {
  245. $crossChannelMatchers[] = [
  246. 'label' => 'cross_channel:IP_only',
  247. 'query' => $baseQuery()->where('IP', $IP),
  248. 'time_window' => 300,
  249. ];
  250. }
  251. foreach ($sameChannelMatchers as $matcher) {
  252. $query = $matcher['query'];
  253. if (!empty($Channel)) {
  254. $query->where('UrlSign', $Channel);
  255. }
  256. $candidates = (clone $query)
  257. ->orderBy('CreateTime', 'desc')
  258. ->orderBy('ID', 'desc')
  259. ->get();
  260. $record = self::pickBestCandidate($candidates, $fbclid);
  261. if (!$record) {
  262. continue;
  263. }
  264. Util::WriteLog('bindCookie', $logContext + [
  265. 'stage' => 'matched',
  266. 'matcher' => $matcher['label'],
  267. 'before' => (array)$record,
  268. ]);
  269. $rowUpdate = self::applyFbcCookieBackfillToUpdate($updateArr, $record, $cookie);
  270. DB::table($table)->where('ID', $record->ID)->update($rowUpdate);
  271. $updatedRecord = DB::table($table)->where('ID', $record->ID)->first();
  272. Util::WriteLog('bindCookie', $logContext + [
  273. 'stage' => 'updated',
  274. 'matcher' => $matcher['label'],
  275. 'after' => (array)$updatedRecord,
  276. ]);
  277. return $updatedRecord;
  278. }
  279. foreach ($crossChannelMatchers as $matcher) {
  280. $query = $matcher['query'];
  281. if (!empty($Channel)) {
  282. $query->where('UrlSign', '<>', strval($Channel));
  283. }
  284. $records = (clone $query)
  285. ->orderBy('CreateTime', 'desc')
  286. ->orderBy('ID', 'desc')
  287. ->get();
  288. if ($records->isEmpty()) {
  289. continue;
  290. }
  291. if ($records->count() > 1 && empty($fbclid)) {
  292. continue;
  293. }
  294. $record = self::pickBestCandidate($records, $fbclid);
  295. $cookieTime = strtotime($record->CreateTime);
  296. $maxWindow = $matcher['time_window'] ?? 600;
  297. if ($cookieTime === false || abs($cookieTime - $registerTime) > $maxWindow) {
  298. Util::WriteLog('bindCookie', $logContext + [
  299. 'stage' => 'skipped',
  300. 'matcher' => $matcher['label'],
  301. 'reason' => 'time_window_exceeded',
  302. 'candidate' => (array)$record,
  303. ]);
  304. // 纠偏只接受注册前后 10 分钟内的 Cookie,防止把历史 Cookie 误绑到新用户。
  305. continue;
  306. }
  307. Util::WriteLog('bindCookie', $logContext + [
  308. 'stage' => 'matched',
  309. 'matcher' => $matcher['label'],
  310. 'before' => (array)$record,
  311. 'channel_corrected_to' => $record->UrlSign,
  312. ]);
  313. $rowUpdate = self::applyFbcCookieBackfillToUpdate($updateArr, $record, $cookie);
  314. DB::table($table)->where('ID', $record->ID)->update($rowUpdate);
  315. if (!empty($record->UrlSign) && isset($user->Channel) && $user->Channel != $record->UrlSign) {
  316. $oldChannel = $user->Channel;
  317. $user->Channel = $record->UrlSign;
  318. try {
  319. $user->save();
  320. } catch (\Exception $exception) {
  321. Log::error($exception->getMessage());
  322. }
  323. DB::table(TableName::QPAccountsDB() . 'AccountsInfo')
  324. ->where('UserID', $UserID)
  325. ->update(['Channel' => $record->UrlSign]);
  326. Util::WriteLog('bindCookie', $logContext + [
  327. 'stage' => 'channel_corrected',
  328. 'old_channel' => $oldChannel,
  329. 'new_channel' => $record->UrlSign,
  330. ]);
  331. }
  332. $updatedRecord = DB::table($table)->where('ID', $record->ID)->first();
  333. Util::WriteLog('bindCookie', $logContext + [
  334. 'stage' => 'updated',
  335. 'matcher' => $matcher['label'],
  336. 'after' => (array)$updatedRecord,
  337. 'channel_corrected_to' => $user->Channel ?? '',
  338. ]);
  339. return $updatedRecord;
  340. }
  341. Util::WriteLog('bindCookie', $logContext + ['stage' => 'no_match']);
  342. return false;
  343. }
  344. /**
  345. * 加载用户的 AccountCookie 记录,用于 S2S 事件上报(CAPI / Kwai / Google)。
  346. *
  347. * 查找优先级:UserID 精确查 → FPID/FF 指纹查。
  348. * 多条候选时用 fbclid 精准匹配,匹配不到则取最新。
  349. * 通过 FPID/FF 找到记录后,仅补充空缺字段(FPID/FF/UserID),
  350. * 绝不覆盖已有的 UserID,防止串号。
  351. *
  352. * @param string $UserID 用户 ID
  353. * @param string $FPID 浏览器指纹 ID
  354. * @param string $FF 字体指纹哈希
  355. * @param string $cookie _fbc cookie 值或完整 cookie 字符串,用于 fbclid 匹配
  356. * @return array|null AccountCookie 记录数组,或 null
  357. */
  358. public static function loadCookie($UserID='', $FPID = '', $FF = '', $cookie = '')
  359. {
  360. $table = TableName::QPAccountsDB() . 'AccountCookie';
  361. $ac = null;
  362. $fbclid = self::extractFbclid($cookie);
  363. if (!empty($UserID)) {
  364. $candidates = DB::table($table)
  365. ->where('UserID', $UserID)
  366. ->orderBy('CreateTime', 'desc')
  367. ->get();
  368. $ac = self::pickBestCandidate($candidates, $fbclid);
  369. }
  370. if (!$ac && (!empty($FPID) || !empty($FF))) {
  371. $query = DB::table($table);
  372. if (!empty($FPID) && !empty($FF)) {
  373. $query->where(function ($q) use ($FPID, $FF) {
  374. $q->where('FPID', $FPID)->orWhere('FF', $FF);
  375. });
  376. } elseif (!empty($FPID)) {
  377. $query->where('FPID', $FPID);
  378. } else {
  379. $query->where('FF', $FF);
  380. }
  381. $candidates = $query->orderBy('CreateTime', 'desc')->get();
  382. $ac = self::pickBestCandidate($candidates, $fbclid);
  383. if ($ac) {
  384. $updateArr = [];
  385. if (!empty($FPID) && empty($ac->FPID)) $updateArr['FPID'] = $FPID;
  386. if (!empty($FF) && empty($ac->FF)) $updateArr['FF'] = $FF;
  387. if (!empty($UserID) && (empty($ac->UserID) || $ac->UserID == 0)) {
  388. $updateArr['UserID'] = $UserID;
  389. }
  390. if (!empty($updateArr)) {
  391. DB::table($table)->where('ID', $ac->ID)->update($updateArr);
  392. }
  393. }
  394. }
  395. if ($ac) {
  396. $ac=(array)$ac;
  397. if(isset($ac['localStorage'])&&isset($ac['ID'])) {
  398. $ls = json_decode($ac['localStorage'], true);
  399. if (isset($ls['user'])) {
  400. unset($ls['user']);
  401. $ac['localStorage'] = json_encode($ls);
  402. DB::table($table)->where('ID', $ac['ID'])->update(['localStorage' => $ac['localStorage']]);
  403. }
  404. }
  405. return $ac;
  406. }
  407. return null;
  408. }
  409. /**
  410. * bindCookie 成功更新行时:若请求里带有效 _fbc,而库中 Cookie 字段尚无 _fbc,则补写 Cookie。
  411. *
  412. * @param array $updateArr 原 update 数组(UserID/FPID/FF/SPE_KEY)
  413. * @param object $record 当前命中的 AccountCookie 行
  414. * @param string $incoming 客户端上报的 cookie 串
  415. * @return array 合并后的 update 数组
  416. */
  417. protected static function applyFbcCookieBackfillToUpdate(array $updateArr, $record, $incomingCookie)
  418. {
  419. $merged = self::mergeIncomingFbcIntoAccountCookie($record->Cookie ?? '', $incomingCookie);
  420. if ($merged !== null) {
  421. $updateArr['Cookie'] = $merged;
  422. }
  423. return $updateArr;
  424. }
  425. /**
  426. * 请求侧有有效 _fbc,且库中 Cookie 不含 _fbc 时,生成应写入的 Cookie 全文。
  427. * - 库为空:采用客户端完整串(含 _fbp 等)
  428. * - 库有其它键无 _fbc:前置 _fbc=...; 保留原串
  429. *
  430. * @return string|null 需更新时返回新串;否则 null
  431. */
  432. public static function mergeIncomingFbcIntoAccountCookie($storedCookie, $incomingCookie)
  433. {
  434. $incomingCookie = is_string($incomingCookie) ? trim($incomingCookie) : '';
  435. if ($incomingCookie === '') {
  436. return null;
  437. }
  438. if (self::extractFbclid($incomingCookie) === '') {
  439. return null;
  440. }
  441. $storedCookie = trim((string)$storedCookie);
  442. if ($storedCookie !== '' && preg_match('/_fbc\s*=/i', $storedCookie)) {
  443. return null;
  444. }
  445. if (preg_match('/_fbc\s*=\s*([^;]+)/', $incomingCookie, $m)) {
  446. $fbcPair = '_fbc=' . trim($m[1]);
  447. } elseif (preg_match('/\bfb\.\d+\.\d+\.[^;\s]+/', $incomingCookie, $m)) {
  448. $fbcPair = '_fbc=' . $m[0];
  449. } else {
  450. return null;
  451. }
  452. if ($storedCookie === '') {
  453. return $incomingCookie;
  454. }
  455. return $fbcPair . '; ' . $storedCookie;
  456. }
  457. /**
  458. * 从 _fbc cookie 中提取 fbclid。
  459. *
  460. * _fbc 格式:fb.{version}.{timestamp}.{fbclid}
  461. * 例:fb.1.1774133029549.IwZXh0bgNhZW0BMABh...
  462. *
  463. * 支持三种输入:
  464. * - 完整 cookie 字符串(_fbc=fb.1.xxx;_fbp=...)→ 提取 _fbc 值再取第4段
  465. * - 纯 _fbc 值(fb.1.xxx.yyy)→ 直接取第4段
  466. * - 纯 fbclid 字符串 → 原样返回
  467. *
  468. * @param string $cookieStr cookie 字符串
  469. * @return string fbclid,或空串
  470. */
  471. public static function extractFbclid($cookieStr)
  472. {
  473. if (empty($cookieStr)) return '';
  474. $fbc = '';
  475. if (preg_match('/_fbc=([^;]+)/', $cookieStr, $m)) {
  476. $fbc = $m[1];
  477. } elseif (str_starts_with($cookieStr, 'fb.')) {
  478. $fbc = $cookieStr;
  479. }
  480. if (empty($fbc)) return '';
  481. $parts = explode('.', $fbc, 4);
  482. if (count($parts) < 4 || empty($parts[3])) return '';
  483. return $parts[3];
  484. }
  485. /**
  486. * 从多条候选记录中,用 fbclid 匹配最精准的一条,匹配不到则取最新。
  487. * $candidates 必须已按 CreateTime desc 排序。
  488. */
  489. public static function pickBestCandidate($candidates, $fbclid = '')
  490. {
  491. if ($candidates->isEmpty()) return null;
  492. if ($candidates->count() === 1) return $candidates->first();
  493. if (!empty($fbclid)) {
  494. $match = $candidates->first(function ($row) use ($fbclid) {
  495. return !empty($row->Cookie) && str_contains($row->Cookie, $fbclid);
  496. });
  497. if ($match) return $match;
  498. }
  499. return $candidates->first();
  500. }
  501. /** 将一条广告点击数据压入该渠道的最近记录队列(最多保留 50 条)。 */
  502. public static function addRecentsNew($data, $url_sign)
  503. {
  504. $recents = self::getRecentsNew($url_sign);
  505. array_push($recents, $data);
  506. if (count($recents) > 50) {
  507. array_shift($recents);
  508. }
  509. Redis::set("recent_apks_$url_sign", json_encode($recents));
  510. }
  511. public static function setRecentsNew($recents, $url_sign)
  512. {
  513. Redis::set("recent_apks_$url_sign", json_encode($recents));
  514. }
  515. public static function getRecentsNew($url_sign)
  516. {
  517. $recents = [];
  518. if (Redis::exists("recent_apks_$url_sign")) {
  519. $recents = json_decode(Redis::get("recent_apks_$url_sign"), true);
  520. }
  521. if (!isset($recents) || empty($recents)) $recents = [];
  522. return $recents;
  523. }
  524. public const KWAI_EVENT = [
  525. 'EVENT_DOWNLOAD' => 'EVENT_DOWNLOAD',//download
  526. 'EVENT_COMPLETE_REGISTRATION' => 'EVENT_COMPLETE_REGISTRATION',//reg
  527. 'EVENT_PURCHASE' => 'EVENT_PURCHASE',//purchase
  528. 'EVENT_FIRST_DEPOSIT' => 'EVENT_FIRST_DEPOSIT',//d0
  529. 'EVENT_ADD_TO_CART' => 'EVENT_ADD_TO_CART',//
  530. 'EVENT_CONTENT_VIEW' => 'EVENT_CONTENT_VIEW',//
  531. ];
  532. /** 快手 S2S 事件上报:根据 UserID 加载 Cookie,如果是快手渠道则发送事件。 */
  533. public static function kwaiEvent($userid, $event, $event_values = [])
  534. {
  535. // $cookies=['Cookie' => $data['cookie'],
  536. // 'UrlSign' => $data['url_sign'],
  537. // 'Platform' => $data['type']]
  538. $user = self::loadCookie($userid);
  539. /*
  540. * {
  541. CampaignID: '987654321',
  542. adSETID: '123456789',
  543. CreativeID: '123456789',
  544. click_id: 'CCpgibAfpRkSWv9zxgDuFHX71q5u_vMxqmvRClE6enEynSNeQAaSvb3CCcXoqXaPwbYpThJBW5n6-62kmmECfY5He3KFgOJB9YErIJ9cIUk=',
  545. pixel_id: '12345678987654321'
  546. }
  547. */
  548. if (isset($user) && $user['Platform'] == 'kw') {
  549. $params = json_decode($user['Cookie'], true);
  550. self::sendToKwai($params, $event, $event_values);
  551. }
  552. }
  553. /** 向快手广告 API 发送转化事件(生产模式,trackFlag=false)。 */
  554. public static function sendToKwai($params, $event, $event_values = [])
  555. {
  556. if ((isset($params['pixel_id']) && $params['pixel_id'] == '545692252418097195') || (isset($params['kwai_api_id']) && $params['kwai_api_id'] == '545692252418097195')) {
  557. $access_token = '1Wvai0OBGGQlO9eVZIm+wnv1tq5hL9IudwMRdntfms4=';
  558. }
  559. if ((isset($params['pixel_id']) && $params['pixel_id'] == '545692410115539004') || (isset($params['kwai_api_id']) && $params['kwai_api_id'] == '545692410115539004')) {
  560. $access_token = 'cwYDbfB+9lWmVaSBWNxhFUVtL764Or8AzxGVclhG44g=';
  561. }
  562. try {
  563. $url = 'http://www.adsnebula.com/log/common/api';
  564. $data = [
  565. "access_token" => $access_token,
  566. "clickid" => $params['click_id'],
  567. "event_name" => $event,
  568. "is_attributed" => 1,
  569. "mmpcode" => "PL",
  570. "pixelId" => $params['pixel_id'] ?? $params['kwai_api_id'],
  571. "pixelSdkVersion" => "9.9.9",
  572. "properties" => json_encode($event_values),
  573. "testFlag" => false,
  574. "trackFlag" => false
  575. ];
  576. $httpCurl = new HttpCurl();
  577. $result = $httpCurl->curlPost($url, $data, 'json', true);
  578. Util::WriteLog("kwai", $result);
  579. } catch (\Exception $e) {
  580. Util::WriteLog("kwai_error", compact("params", "event", "event_values"));
  581. Util::WriteLog("kwai_error", $e->getMessage() . $e->getTraceAsString());
  582. }
  583. }
  584. /** 向快手广告 API 发送转化事件(测试模式,trackFlag=true)。 */
  585. public static function sendToKwaiTest($params, $event, $event_values = [])
  586. {
  587. if ((isset($params['pixel_id']) && $params['pixel_id'] == '545692252418097195') || (isset($params['kwai_api_id']) && $params['kwai_api_id'] == '545692252418097195')) {
  588. $access_token = '1Wvai0OBGGQlO9eVZIm+wnv1tq5hL9IudwMRdntfms4=';
  589. }
  590. if ((isset($params['pixel_id']) && $params['pixel_id'] == '545692410115539004') || (isset($params['kwai_api_id']) && $params['kwai_api_id'] == '545692410115539004')) {
  591. $access_token = 'cwYDbfB+9lWmVaSBWNxhFUVtL764Or8AzxGVclhG44g=';
  592. }
  593. try {
  594. $url = 'http://www.adsnebula.com/log/common/api';
  595. $data = [
  596. "access_token" => $access_token,
  597. "clickid" => $params['click_id'],
  598. "event_name" => $event,
  599. "is_attributed" => 1,
  600. "mmpcode" => "PL",
  601. "pixelId" => $params['pixel_id'] ?? $params['kwai_api_id'],
  602. "pixelSdkVersion" => "9.9.9",
  603. "properties" => json_encode($event_values),
  604. "testFlag" => false,
  605. "trackFlag" => true
  606. ];
  607. $httpCurl = new HttpCurl();
  608. $result = $httpCurl->curlPost($url, $data, 'json', true);
  609. Util::WriteLog("kwai", $data);
  610. Util::WriteLog("kwai", $result);
  611. } catch (\Exception $e) {
  612. Util::WriteLog("kwai_error", compact("params", "event", "event_values"));
  613. Util::WriteLog("kwai_error", $e->getMessage() . $e->getTraceAsString());
  614. }
  615. }
  616. /** 对老用户设置游戏控分参数(首次设置后不再重复写入)。 */
  617. public static function ControlOldUser($UserID, $Params = '')
  618. {
  619. $openGames = config('games.openKGame');
  620. $query = DB::table('QPTreasureDB.dbo.UserScoreControl')->where('UserID', $UserID)->first();
  621. if (!$query) {
  622. $build_sql = DB::connection('write')->table('QPTreasureDB.dbo.UserScoreControl');
  623. Util::WriteLog('control_old_user', compact("UserID", "Params"));
  624. $data = [
  625. 'ControlScore' => (int)(-40 * NumConfig::NUM_VALUE),
  626. 'EffectiveScore' => 0,
  627. 'ControlKindID' => -1,
  628. 'Remarks' => '',
  629. 'InsertDate' => date('Y-m-d H:i:s'),
  630. 'ControlRadian' => 0
  631. ];
  632. $build_sql->updateOrInsert(['UserID' => $UserID], $data);
  633. foreach ($openGames as $GameID) {
  634. $KindData = [
  635. 'UserID' => $UserID,
  636. 'KindID' => $GameID,
  637. 'ControlRadian' => 50,
  638. 'ControlDate' => date('Y-m-d H:i:s')
  639. ];
  640. DB::connection('write')->table('QPTreasureDB.dbo.UserControlKind')->updateOrInsert(['UserID' => $UserID, 'KindID' => $GameID], $KindData);
  641. }
  642. }
  643. }
  644. }