ApkService.php 29 KB

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