ApkService.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 setRecentsNew($recents, $url_sign)
  93. {
  94. Redis::set("recent_apks_$url_sign", json_encode($recents));
  95. }
  96. public static function getRecentsNew($url_sign)
  97. {
  98. $recents = [];
  99. if (Redis::exists("recent_apks_$url_sign")) {
  100. $recents = json_decode(Redis::get("recent_apks_$url_sign"), true);
  101. }
  102. if (!isset($recents) || empty($recents)) $recents = [];
  103. return $recents;
  104. }
  105. public const KWAI_EVENT=[
  106. 'EVENT_DOWNLOAD'=>'EVENT_DOWNLOAD',//download
  107. 'EVENT_COMPLETE_REGISTRATION'=>'EVENT_COMPLETE_REGISTRATION',//reg
  108. 'EVENT_PURCHASE'=>'EVENT_PURCHASE',//purchase
  109. 'EVENT_FIRST_DEPOSIT'=>'EVENT_FIRST_DEPOSIT',//d0
  110. 'EVENT_ADD_TO_CART'=>'EVENT_ADD_TO_CART',//
  111. 'EVENT_CONTENT_VIEW'=>'EVENT_CONTENT_VIEW',//
  112. ];
  113. public static function kwaiEvent($userid,$event,$event_values=[])
  114. {
  115. // $cookies=['Cookie' => $data['cookie'],
  116. // 'UrlSign' => $data['url_sign'],
  117. // 'Platform' => $data['type']]
  118. $user=self::loadCookie($userid);
  119. /*
  120. * {
  121. CampaignID: '987654321',
  122. adSETID: '123456789',
  123. CreativeID: '123456789',
  124. click_id: 'CCpgibAfpRkSWv9zxgDuFHX71q5u_vMxqmvRClE6enEynSNeQAaSvb3CCcXoqXaPwbYpThJBW5n6-62kmmECfY5He3KFgOJB9YErIJ9cIUk=',
  125. pixel_id: '12345678987654321'
  126. }
  127. */
  128. if(isset($user)&&$user['Platform']=='kw') {
  129. $params = json_decode($user['Cookie'], true);
  130. self::sendToKwai($params, $event, $event_values);
  131. }
  132. }
  133. public static function sendToKwai($params,$event,$event_values=[]){
  134. if((isset($params['pixel_id'])&&$params['pixel_id']=='545692252418097195')||(isset($params['kwai_api_id'])&&$params['kwai_api_id']=='545692252418097195')){
  135. $access_token='1Wvai0OBGGQlO9eVZIm+wnv1tq5hL9IudwMRdntfms4=';
  136. }
  137. if((isset($params['pixel_id'])&&$params['pixel_id']=='545692410115539004')||(isset($params['kwai_api_id'])&&$params['kwai_api_id']=='545692410115539004')){
  138. $access_token='cwYDbfB+9lWmVaSBWNxhFUVtL764Or8AzxGVclhG44g=';
  139. }
  140. try {
  141. $url = 'http://www.adsnebula.com/log/common/api';
  142. $data = [
  143. "access_token" => $access_token,
  144. "clickid" => $params['click_id'],
  145. "event_name" => $event,
  146. "is_attributed" => 1,
  147. "mmpcode" => "PL",
  148. "pixelId" => $params['pixel_id']??$params['kwai_api_id'],
  149. "pixelSdkVersion" => "9.9.9",
  150. "properties" => json_encode($event_values),
  151. "testFlag" => false,
  152. "trackFlag" => false
  153. ];
  154. $httpCurl = new HttpCurl();
  155. $result = $httpCurl->curlPost($url, $data, 'json',true);
  156. Util::WriteLog("kwai", $result);
  157. }catch (\Exception $e){
  158. Util::WriteLog("kwai_error", compact("params","event","event_values"));
  159. Util::WriteLog("kwai_error", $e->getMessage().$e->getTraceAsString());
  160. }
  161. }
  162. public static function sendToKwaiTest($params,$event,$event_values=[]){
  163. if((isset($params['pixel_id'])&&$params['pixel_id']=='545692252418097195')||(isset($params['kwai_api_id'])&&$params['kwai_api_id']=='545692252418097195')){
  164. $access_token='1Wvai0OBGGQlO9eVZIm+wnv1tq5hL9IudwMRdntfms4=';
  165. }
  166. if((isset($params['pixel_id'])&&$params['pixel_id']=='545692410115539004')||(isset($params['kwai_api_id'])&&$params['kwai_api_id']=='545692410115539004')){
  167. $access_token='cwYDbfB+9lWmVaSBWNxhFUVtL764Or8AzxGVclhG44g=';
  168. }
  169. try {
  170. $url = 'http://www.adsnebula.com/log/common/api';
  171. $data = [
  172. "access_token" => $access_token,
  173. "clickid" => $params['click_id'],
  174. "event_name" => $event,
  175. "is_attributed" => 1,
  176. "mmpcode" => "PL",
  177. "pixelId" => $params['pixel_id']??$params['kwai_api_id'],
  178. "pixelSdkVersion" => "9.9.9",
  179. "properties" => json_encode($event_values),
  180. "testFlag" => false,
  181. "trackFlag" => true
  182. ];
  183. $httpCurl = new HttpCurl();
  184. $result = $httpCurl->curlPost($url, $data, 'json',true);
  185. Util::WriteLog("kwai", $data);
  186. Util::WriteLog("kwai", $result);
  187. }catch (\Exception $e){
  188. Util::WriteLog("kwai_error", compact("params","event","event_values"));
  189. Util::WriteLog("kwai_error", $e->getMessage().$e->getTraceAsString());
  190. }
  191. }
  192. public static function ControlOldUser($UserID,$Params='')
  193. {
  194. $openGames = config('games.openKGame');
  195. $query = DB::table('QPTreasureDB.dbo.UserScoreControl')->where('UserID', $UserID)->first();
  196. if (!$query) {
  197. $build_sql = DB::connection('write')->table('QPTreasureDB.dbo.UserScoreControl');
  198. Util::WriteLog('control_old_user', compact("UserID","Params"));
  199. $data = [
  200. 'ControlScore' => (int)(-40 * NumConfig::NUM_VALUE),
  201. 'EffectiveScore' => 0,
  202. 'ControlKindID' => -1,
  203. 'Remarks' => '',
  204. 'InsertDate' => date('Y-m-d H:i:s'),
  205. 'ControlRadian' => 0
  206. ];
  207. $build_sql->updateOrInsert(['UserID' => $UserID], $data);
  208. foreach ($openGames as $GameID) {
  209. $KindData = [
  210. 'UserID' => $UserID,
  211. 'KindID' => $GameID,
  212. 'ControlRadian' => 50,
  213. 'ControlDate' => date('Y-m-d H:i:s')
  214. ];
  215. DB::connection('write')->table('QPTreasureDB.dbo.UserControlKind')->updateOrInsert(['UserID' => $UserID, 'KindID' => $GameID], $KindData);
  216. }
  217. }
  218. }
  219. }