AsyncUserRegister.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z-yang
  5. * Date: 2020/12/24
  6. * Time: 11:36
  7. */
  8. namespace App\Jobs;
  9. use App\Modules\Channel\Services\ReportToChannelUserAndOrderService;
  10. use DB;
  11. use GuzzleHttp\Client;
  12. use Illuminate\Bus\Queueable;
  13. use Illuminate\Queue\SerializesModels;
  14. use Illuminate\Queue\InteractsWithQueue;
  15. use Illuminate\Contracts\Queue\ShouldQueue;
  16. use Illuminate\Foundation\Bus\Dispatchable;
  17. /**
  18. * 回传订单给渠道
  19. * Class AsyncUserRegister
  20. * @package App\Jobs\
  21. */
  22. class AsyncUserRegister implements ShouldQueue
  23. {
  24. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  25. private $uid;
  26. private $distribution_channel_id;
  27. private $register_ip;
  28. private $user_agent;
  29. private $user_created_at;
  30. const SUCCESS = 'success';
  31. public function __construct($uid,$distribution_channel_id,$register_ip,$user_agent,$user_created_at){
  32. $this->uid = $uid;
  33. $this->distribution_channel_id = $distribution_channel_id;
  34. $this->register_ip = $register_ip;
  35. $this->user_agent = $user_agent;
  36. $this->user_created_at = $user_created_at;
  37. }
  38. /**
  39. * Execute the job.
  40. *
  41. * @return void
  42. */
  43. public function handle()
  44. {
  45. $this->reportToChannel();
  46. $this->fireRegisterReport();
  47. }
  48. private function reportToChannel(){
  49. $link = ReportToChannelUserAndOrderService::getReportUrl($this->distribution_channel_id,ReportToChannelUserAndOrderService::$user_type);
  50. if(!$link) return ;
  51. $client = new Client(['timeout'=>3.0]);
  52. try{
  53. $result = $client->request('post',$link,['form_params'=>[
  54. 'uid'=>$this->uid,
  55. 'distribution_channel_id'=>$this->distribution_channel_id,
  56. 'register_ip'=>$this->register_ip,
  57. 'user_agent'=>$this->user_agent,
  58. 'user_created_at'=>$this->user_created_at,
  59. 'event'=>'user',
  60. 'source'=>'wangduyun'
  61. ]])->getBody()->getContents();
  62. if($result && str_contains(strtolower($result),self::SUCCESS)){
  63. return ;
  64. }
  65. $error = 'result error ,'.$result;
  66. }catch (\Exception $e){
  67. $error = 'exception,'.$e->getMessage();
  68. }
  69. DB::table('report_to_channel_users')->insert([
  70. 'uid'=>$this->uid,
  71. 'distribution_channel_id'=>$this->distribution_channel_id,
  72. 'register_ip'=>$this->register_ip,
  73. 'user_agent'=>$this->user_agent,
  74. 'user_created_at'=>$this->user_created_at,
  75. 'report_status'=>0,
  76. 'error_msg'=>$error,
  77. 'created_at'=>date('Y-m-d H:i:s'),
  78. 'updated_at'=>date('Y-m-d H:i:s'),
  79. ]);
  80. }
  81. private function fireRegisterReport(){
  82. $client = new Client();
  83. $ip = $this->register_ip;
  84. $register_time = $this->user_created_at;
  85. $reportData = [
  86. 'platform' => 'wangduyun',
  87. 'register' => [
  88. 'uid' => $this->uid,
  89. 'channel_id' => $this->distribution_channel_id,
  90. 'register_time' => $register_time,
  91. 'register_ip' => $ip,
  92. ]
  93. ];
  94. $record = [
  95. 'uid'=>$this->uid,
  96. 'ip'=>$ip,
  97. 'register_time'=>$register_time,
  98. 'created_at'=>date('Y-m-d H:i:s'),
  99. 'updated_at'=>date('Y-m-d H:i:s'),
  100. ];
  101. $host = 'https://firetrack.wd.amanbook.com';
  102. try{
  103. // 执行上报
  104. $client->post($host . '/api/reportRegister', [
  105. 'headers' => [
  106. 'x-code' => 'Mvnx1Yr3O8i!TS5u'
  107. ],
  108. 'json' => $reportData
  109. ]);
  110. }catch (\Exception $e){
  111. \Log::error($e);
  112. $record['status'] = 2;
  113. DB::table('fire_user_report_record')->insert($record);
  114. }
  115. }
  116. }