ApkService.php 11 KB

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