ApkService.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. class ApkService
  13. {
  14. //'ip','agent','cookie','type','url_sign','time'
  15. public static function updateCookie($UserID, $arr){
  16. DB::table(TableName::QPAccountsDB() . 'AccountCookie')->updateOrInsert(['UserID'=>$UserID],$arr);
  17. $key = "AccountCookie_$UserID";
  18. Redis::set($key, json_encode($arr));
  19. Redis::expire($key,36000);
  20. }
  21. //'ip','agent','cookie','type','url_sign','time'
  22. public static function saveCookie($UserID, $data,$FPID='')
  23. {
  24. $agent = $_SERVER['HTTP_USER_AGENT'];
  25. $key = "AccountCookie_$UserID";
  26. if(empty($UserID)&&!empty($FPID))$key="AccountCookie_$FPID";
  27. if (Redis::exists($key)) {
  28. return;
  29. }
  30. if (!DB::table(TableName::QPAccountsDB() . "AccountCookie")->where('UserID', $UserID)->exists()) {
  31. // if(DB::table(TableName::QPAccountsDB() . "AccountCookie")->where('Params', $data['params'])->exists()){
  32. // $data['params']='';
  33. //重复注册,下蛊吧
  34. // self::ControlOldUser($UserID,$data);
  35. // }
  36. $arr = [
  37. 'UserID' => $UserID,
  38. 'FPID' => $FPID,
  39. 'Cookie' => $data['cookie'],
  40. 'Params' => $data['params'],
  41. 'UrlSign' => $data['url_sign'],
  42. 'Platform' => $data['type'],
  43. 'GameUA' => $agent,
  44. 'ClickUA' => $data['agent'],
  45. 'Origin' => $data['origin']??"",
  46. 'Locale' => $data['locale']??$_SERVER['HTTP_ACCEPT_LANGUAGE'],
  47. 'ClickTime' => date('Y-m-d H:i:s', $data['time']),
  48. 'UniqueAction' => 2,
  49. 'IP'=>IpLocation::getIP(),
  50. ];
  51. DB::table(TableName::QPAccountsDB() . 'AccountCookie')->insert($arr);
  52. Redis::set($key, json_encode($arr));
  53. Redis::expire($key,36000);
  54. if(strstr($arr['Params'],'gclid')||strstr($arr['Params'],'fbclid')) {
  55. (new AccountsSource())->notExistsInsert($UserID, "APK_AD(" . $arr['Platform'] . ")");
  56. }
  57. //kwai手打点
  58. if($arr['Platform']=='kw') {
  59. $cookie = $arr['Cookie'];
  60. ApkService::sendToKwai(json_decode($cookie, true), ApkService::KWAI_EVENT['EVENT_COMPLETE_REGISTRATION']);
  61. }
  62. if($arr['Platform']=='gg') {
  63. $cookie = $arr['Params'];
  64. // UploadOfflineConversion::reg($UserID,json_decode($cookie, true));
  65. }
  66. }
  67. }
  68. public static function loadCookie($UserID)
  69. {
  70. $key = "AccountCookie_$UserID";
  71. if (Redis::exists($key)) {
  72. return json_decode(Redis::get($key), true);
  73. }
  74. if (DB::table(TableName::QPAccountsDB() . "AccountCookie")->where('UserID', $UserID)->exists()) {
  75. $ac = DB::table(TableName::QPAccountsDB() . "AccountCookie")->where('UserID', $UserID)->first();
  76. $ac = (array)$ac;
  77. Redis::set($key, json_encode($ac));
  78. Redis::expire($key, 36000);
  79. return $ac;
  80. }
  81. return null;
  82. }
  83. public static function addRecentsNew($data, $url_sign)
  84. {
  85. $recents = self::getRecentsNew($url_sign);
  86. array_push($recents, $data);
  87. if (count($recents) > 50) {
  88. array_shift($recents);
  89. }
  90. Redis::set("recent_apks_$url_sign", json_encode($recents));
  91. }
  92. public static function getRecentsNew($url_sign)
  93. {
  94. $recents = [];
  95. if (Redis::exists("recent_apks_$url_sign")) {
  96. $recents = json_decode(Redis::get("recent_apks_$url_sign"), true);
  97. }
  98. if (!isset($recents) || empty($recents)) $recents = [];
  99. return $recents;
  100. }
  101. public const KWAI_EVENT=[
  102. 'EVENT_DOWNLOAD'=>'EVENT_DOWNLOAD',//download
  103. 'EVENT_COMPLETE_REGISTRATION'=>'EVENT_COMPLETE_REGISTRATION',//reg
  104. 'EVENT_PURCHASE'=>'EVENT_PURCHASE',//purchase
  105. 'EVENT_FIRST_DEPOSIT'=>'EVENT_FIRST_DEPOSIT',//d0
  106. 'EVENT_ADD_TO_CART'=>'EVENT_ADD_TO_CART',//
  107. 'EVENT_CONTENT_VIEW'=>'EVENT_CONTENT_VIEW',//
  108. ];
  109. public static function kwaiEvent($userid,$event,$event_values=[])
  110. {
  111. // $cookies=['Cookie' => $data['cookie'],
  112. // 'UrlSign' => $data['url_sign'],
  113. // 'Platform' => $data['type']]
  114. $user=self::loadCookie($userid);
  115. /*
  116. * {
  117. CampaignID: '987654321',
  118. adSETID: '123456789',
  119. CreativeID: '123456789',
  120. click_id: 'CCpgibAfpRkSWv9zxgDuFHX71q5u_vMxqmvRClE6enEynSNeQAaSvb3CCcXoqXaPwbYpThJBW5n6-62kmmECfY5He3KFgOJB9YErIJ9cIUk=',
  121. pixel_id: '12345678987654321'
  122. }
  123. */
  124. if(isset($user)&&$user['Platform']=='kw') {
  125. $params = json_decode($user['Cookie'], true);
  126. self::sendToKwai($params, $event, $event_values);
  127. }
  128. }
  129. public static function sendToKwai($params,$event,$event_values=[]){
  130. if((isset($params['pixel_id'])&&$params['pixel_id']=='545692252418097195')||(isset($params['kwai_api_id'])&&$params['kwai_api_id']=='545692252418097195')){
  131. $access_token='1Wvai0OBGGQlO9eVZIm+wnv1tq5hL9IudwMRdntfms4=';
  132. }
  133. if((isset($params['pixel_id'])&&$params['pixel_id']=='545692410115539004')||(isset($params['kwai_api_id'])&&$params['kwai_api_id']=='545692410115539004')){
  134. $access_token='cwYDbfB+9lWmVaSBWNxhFUVtL764Or8AzxGVclhG44g=';
  135. }
  136. try {
  137. $url = 'http://www.adsnebula.com/log/common/api';
  138. $data = [
  139. "access_token" => $access_token,
  140. "clickid" => $params['click_id'],
  141. "event_name" => $event,
  142. "is_attributed" => 1,
  143. "mmpcode" => "PL",
  144. "pixelId" => $params['pixel_id']??$params['kwai_api_id'],
  145. "pixelSdkVersion" => "9.9.9",
  146. "properties" => json_encode($event_values),
  147. "testFlag" => false,
  148. "trackFlag" => false
  149. ];
  150. $httpCurl = new HttpCurl();
  151. $result = $httpCurl->curlPost($url, $data, 'json',true);
  152. Util::WriteLog("kwai", $result);
  153. }catch (\Exception $e){
  154. Util::WriteLog("kwai_error", compact("params","event","event_values"));
  155. Util::WriteLog("kwai_error", $e->getMessage().$e->getTraceAsString());
  156. }
  157. }
  158. public static function sendToKwaiTest($params,$event,$event_values=[]){
  159. if((isset($params['pixel_id'])&&$params['pixel_id']=='545692252418097195')||(isset($params['kwai_api_id'])&&$params['kwai_api_id']=='545692252418097195')){
  160. $access_token='1Wvai0OBGGQlO9eVZIm+wnv1tq5hL9IudwMRdntfms4=';
  161. }
  162. if((isset($params['pixel_id'])&&$params['pixel_id']=='545692410115539004')||(isset($params['kwai_api_id'])&&$params['kwai_api_id']=='545692410115539004')){
  163. $access_token='cwYDbfB+9lWmVaSBWNxhFUVtL764Or8AzxGVclhG44g=';
  164. }
  165. try {
  166. $url = 'http://www.adsnebula.com/log/common/api';
  167. $data = [
  168. "access_token" => $access_token,
  169. "clickid" => $params['click_id'],
  170. "event_name" => $event,
  171. "is_attributed" => 1,
  172. "mmpcode" => "PL",
  173. "pixelId" => $params['pixel_id']??$params['kwai_api_id'],
  174. "pixelSdkVersion" => "9.9.9",
  175. "properties" => json_encode($event_values),
  176. "testFlag" => false,
  177. "trackFlag" => true
  178. ];
  179. $httpCurl = new HttpCurl();
  180. $result = $httpCurl->curlPost($url, $data, 'json',true);
  181. Util::WriteLog("kwai", $data);
  182. Util::WriteLog("kwai", $result);
  183. }catch (\Exception $e){
  184. Util::WriteLog("kwai_error", compact("params","event","event_values"));
  185. Util::WriteLog("kwai_error", $e->getMessage().$e->getTraceAsString());
  186. }
  187. }
  188. public static function ControlOldUser($UserID,$Params='')
  189. {
  190. $openGames = config('games.openKGame');
  191. $query = DB::table('QPTreasureDB.dbo.UserScoreControl')->where('UserID', $UserID)->first();
  192. if (!$query) {
  193. $build_sql = DB::connection('write')->table('QPTreasureDB.dbo.UserScoreControl');
  194. Util::WriteLog('control_old_user', compact("UserID","Params"));
  195. $data = [
  196. 'ControlScore' => (int)(-40 * NumConfig::NUM_VALUE),
  197. 'EffectiveScore' => 0,
  198. 'ControlKindID' => -1,
  199. 'Remarks' => '',
  200. 'InsertDate' => date('Y-m-d H:i:s'),
  201. 'ControlRadian' => 0
  202. ];
  203. $build_sql->updateOrInsert(['UserID' => $UserID], $data);
  204. foreach ($openGames as $GameID) {
  205. $KindData = [
  206. 'UserID' => $UserID,
  207. 'KindID' => $GameID,
  208. 'ControlRadian' => 50,
  209. 'ControlDate' => date('Y-m-d H:i:s')
  210. ];
  211. DB::connection('write')->table('QPTreasureDB.dbo.UserControlKind')->updateOrInsert(['UserID' => $UserID, 'KindID' => $GameID], $KindData);
  212. }
  213. }
  214. }
  215. }