AfEvent.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace App\dao\af;
  3. use App\Facade\TableName;
  4. use App\Http\helper\HttpCurl;
  5. use App\Services\ApkService;
  6. use App\Services\UploadOfflineConversion;
  7. use App\Util;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. class AfEvent
  11. {
  12. protected $config;
  13. protected $afToken;
  14. protected $userinfo=null;
  15. protected $adjustConfig=null;
  16. // 设备码类型 -- 可用的设备 ID
  17. protected $eventType = [1 => 'adid', 2 => 'gps_adid', 3 => 'fire_adid', 4 => 'oaid', 5 => 'idfa', 6 => 'idfv', 7 => 'android_id'];
  18. public function __construct()
  19. {
  20. $this->config = config('api.af');
  21. $this->afToken = config('aftoken');
  22. }
  23. // af充值事件
  24. public function pay($AdId, $UserID, $eventType)
  25. {
  26. try {
  27. if (!array_key_exists($eventType, $this->eventType)) return true;
  28. $userinfo=$this->getUserInfo($UserID);
  29. $Channel = $userinfo->Channel;
  30. $CustomID = $userinfo->CustomID;
  31. $PackageName = $userinfo->LastLogonPackage;
  32. if (empty($AdId)) {
  33. Log::channel('adjustEvent')->info('adid为空 '.$UserID);
  34. $AdId = DB::connection('write')->table(TableName::QPAccountsDB() . 'AccountsInfo')
  35. ->where('UserID', $UserID)
  36. ->value('LastLogonMachine');
  37. }
  38. $gateway = $this->config['gateway'];
  39. $afTokenConfig = $this->adjustConfig;
  40. if(!isset($afTokenConfig)||empty($afTokenConfig)){
  41. Util::WriteLog("aftoken",$userinfo);
  42. return true;
  43. }
  44. if($afTokenConfig['token']=='wgh5')return;
  45. //过度adjust账号
  46. // if(intval($Channel)==105&&intval($CustomID)>0){
  47. // $CustomID=intval($CustomID);
  48. // $v=intval($CustomID/1000);
  49. // if(isset($this->afToken[$Channel."_".$v])){
  50. // $afTokenConfig = $this->afToken[$Channel."_".$v];
  51. // }
  52. // }
  53. $data = [$this->eventType[$eventType] => $AdId, 'event_token' => $afTokenConfig['payOk'], 'app_token' => $afTokenConfig['token'], 's2s' => 1, 'created_at_unix' => time()];
  54. Log::channel('adjustEvent')->info('记录渠道包:'.$Channel);
  55. Log::channel('adjustEvent')->info('请求数据'.\GuzzleHttp\json_encode($data));
  56. $curl = new HttpCurl();
  57. $res = $curl->curlPost($gateway, $data, '');
  58. if (is_array($res)) {
  59. $res = json_encode($res);
  60. }
  61. Log::channel('adjustEvent')->info('af充值事件' . $UserID . ' - ' . $res);
  62. } catch (\Exception $exception) {
  63. Log::channel('adjustEvent')->info('af充值事件失败' . $UserID . $exception->getMessage());
  64. }
  65. }
  66. // af充值金额统计事件
  67. public function paySum($AdId, $UserID, $Recharge, $eventType)
  68. {
  69. try {
  70. if (!array_key_exists($eventType, $this->eventType)) return true;
  71. if ($Recharge < 0) return true;
  72. // $userinfo=DB::table(TableName::QPAccountsDB() . 'AccountsInfo')
  73. // ->where('UserID', $UserID)->select("Channel","CustomID")->first();
  74. $userinfo=$this->getUserInfo($UserID);
  75. $Channel = $userinfo->Channel;
  76. $CustomID = $userinfo->CustomID;
  77. $gateway = $this->config['gateway'];
  78. $afTokenConfig = $this->adjustConfig;
  79. if(!isset($afTokenConfig)||empty($afTokenConfig)){
  80. Util::WriteLog("aftoken",$userinfo);
  81. return true;
  82. }
  83. if($afTokenConfig['token']=='wgh5')return;
  84. //过度adjust账号
  85. // if(intval($Channel)==105&&intval($CustomID)>0){
  86. // $CustomID=intval($CustomID);
  87. // $v=intval($CustomID/1000);
  88. // if(isset($this->afToken[$Channel."_".$v])){
  89. // $afTokenConfig = $this->afToken[$Channel."_".$v];
  90. // }
  91. // }
  92. $data = [$this->eventType[$eventType] => $AdId, 'event_token' => $afTokenConfig['PaySum'], 'app_token' => $afTokenConfig['token'], 's2s' => 1, 'created_at_unix' => time(), 'revenue' => $Recharge, 'currency' => env('CONFIG_24680_CURRENCY','BRL')];
  93. Log::channel('adjustEvent')->info('记录渠道包:'.$Channel);
  94. Log::channel('adjustEvent')->info('请求数据'.\GuzzleHttp\json_encode($data));
  95. if(!in_array($Channel,[103,"103"])){
  96. //为了投vo取消了
  97. // $data['revenue']=$Recharge/10;
  98. // Log::channel('adjustEvent')->info('非联运渠道,收入数据/10:'.$data['revenue']);
  99. }
  100. $curl = new HttpCurl();
  101. $res = $curl->curlPost($gateway, $data, '');
  102. if (is_array($res)) {
  103. $res = json_encode($res);
  104. }
  105. Log::channel('adjustEvent')->info('af充值金额统计事件' . $UserID . ' - ' . $res);
  106. } catch (\Exception $exception) {
  107. Log::channel('adjustEvent')->info('af充值金额统计事件' . $UserID . $exception->getMessage());
  108. }
  109. }
  110. protected $UserID;
  111. protected function getUserInfo($UserID){
  112. if(!$this->userinfo||$this->UserID!=$UserID){
  113. $this->UserID=$UserID;
  114. $this->userinfo=DB::table(TableName::QPAccountsDB() . 'AccountsInfo')
  115. ->where('UserID', $UserID)->first();
  116. }
  117. if(!$this->adjustConfig){
  118. $this->adjustConfig=DB::table('QPPlatformDB.dbo.ChannelPackageName')->where('Channel',$this->userinfo->Channel)->value('AdjustPayConfig');
  119. if(!empty( $this->adjustConfig)) $this->adjustConfig=json_decode( $this->adjustConfig,true);
  120. }
  121. return $this->userinfo;
  122. }
  123. // af充值人数统计事件
  124. public function payCount($AdId, $UserID, $eventType)
  125. {
  126. try {
  127. if (!array_key_exists($eventType, $this->eventType)) return true;
  128. $userInfo=$this->getUserInfo($UserID);
  129. $Channel = $userInfo->Channel;
  130. $RegisterDate=$userInfo->RegisterDate;
  131. $CustomID = $userInfo->CustomID;
  132. // $isD0=(date("Y-m-d")==date("Y-m-d", strtotime($RegisterDate)));
  133. $isD0=(time()-strtotime($RegisterDate)<86400);
  134. $gateway = $this->config['gateway'];
  135. $afTokenConfig = $this->adjustConfig;
  136. if(!isset($afTokenConfig)||empty($afTokenConfig)){
  137. Util::WriteLog("aftoken",$userInfo);
  138. return true;
  139. }
  140. if($afTokenConfig['token']=='wgh5')return;
  141. //过度adjust账号
  142. // if(intval($Channel)==105&&intval($CustomID)>0){
  143. // $CustomID=intval($CustomID);
  144. // $v=intval($CustomID/1000);
  145. // if(isset($this->afToken[$Channel."_".$v])){
  146. // $afTokenConfig = $this->afToken[$Channel."_".$v];
  147. // }
  148. // }
  149. $PayCountD0Event = $afTokenConfig['PayCountD0'] ?? 0;
  150. $PayCountEvent = $afTokenConfig['PayCount'] ?? 0;
  151. if($PayCountEvent === 0) return true;
  152. Log::channel('adjustEvent')->info('记录渠道包:'.$Channel.",isD0:".($isD0?1:0).",date:".$RegisterDate);
  153. if($PayCountD0Event!==0&&$isD0){
  154. $data = [$this->eventType[$eventType] => $AdId, 'event_token' => $PayCountD0Event, 'app_token' => $afTokenConfig['token'], 's2s' => 1, 'created_at_unix' => time()];
  155. Log::channel('adjustEvent')->info('请求数据'.\GuzzleHttp\json_encode($data));
  156. $curl = new HttpCurl();
  157. $res = $curl->curlPost($gateway, $data, '');
  158. if (is_array($res)) {
  159. $res = json_encode($res);
  160. }
  161. Log::channel('adjustEvent')->info('af充值人数D0统计事件' . $UserID . ' - ' . $res);
  162. }
  163. $data = [$this->eventType[$eventType] => $AdId, 'event_token' => $PayCountEvent, 'app_token' => $afTokenConfig['token'], 's2s' => 1, 'created_at_unix' => time()];
  164. Log::channel('adjustEvent')->info('请求数据'.\GuzzleHttp\json_encode($data));
  165. $curl = new HttpCurl();
  166. $res = $curl->curlPost($gateway, $data, '');
  167. if (is_array($res)) {
  168. $res = json_encode($res);
  169. }
  170. Log::channel('adjustEvent')->info('af充值人数统计事件' . $UserID . ' - ' . $res);
  171. } catch (\Exception $exception) {
  172. Log::channel('adjustEvent')->info('af充值人数统计事件' . $UserID . $exception->getMessage());
  173. }
  174. }
  175. public function recordKwaiRecharge($userid,$payAmt){
  176. $arr=ApkService::loadCookie($userid);
  177. if(isset($arr)&&$arr['Platform']=='kw') {
  178. $cookie = $arr['Cookie'];
  179. ApkService::sendToKwai(json_decode($cookie, true), ApkService::KWAI_EVENT['EVENT_PURCHASE'],['currency'=>'USD','value'=>floor($payAmt/5)]);
  180. //唯一性记录
  181. if($arr['UniqueAction']<4){
  182. $userinfo=$this->getUserInfo($userid);
  183. $RegisterDate=$userinfo->RegisterDate;
  184. // $isD0=(date("Y-m-d")==date("Y-m-d", strtotime($RegisterDate)));
  185. $isD0=(time()-strtotime($RegisterDate)<86400);
  186. if($isD0) {
  187. $arr['UniqueAction'] = 4;
  188. ApkService::sendToKwai(json_decode($cookie, true), ApkService::KWAI_EVENT['EVENT_ADD_TO_CART']);
  189. }else{
  190. $arr['UniqueAction'] = 6;
  191. }
  192. ApkService::updateCookie($userid, $arr);
  193. ApkService::sendToKwai(json_decode($cookie, true), ApkService::KWAI_EVENT['EVENT_FIRST_DEPOSIT']);
  194. }
  195. }
  196. }
  197. public function recordGoogleRecharge($userid,$payAmt){
  198. $arr=ApkService::loadCookie($userid);
  199. if(isset($arr)&&$arr['Platform']=='gg') {
  200. $cookie = json_decode($arr['Cookie'], true);
  201. $userinfo=$this->getUserInfo($userid);
  202. UploadOfflineConversion::pay($userinfo->Channel,$cookie,$payAmt,$userinfo->LastLogonPackage);
  203. //唯一性记录
  204. if($arr['UniqueAction']<4){
  205. $userinfo=$this->getUserInfo($userid);
  206. $RegisterDate=$userinfo->RegisterDate;
  207. // $isD0=(date("Y-m-d")==date("Y-m-d", strtotime($RegisterDate)));
  208. $isD0=(time()-strtotime($RegisterDate)<86400);
  209. if($isD0) {
  210. $arr['UniqueAction'] = 4;
  211. UploadOfflineConversion::d0($userinfo->Channel,$cookie,$userinfo->LastLogonPackage);
  212. }else{
  213. $arr['UniqueAction'] = 6;
  214. }
  215. ApkService::updateCookie($userid, $arr);
  216. UploadOfflineConversion::d1($userinfo->Channel,$cookie,$userinfo->LastLogonPackage);
  217. }
  218. }
  219. }
  220. }