AsyncUserRegister.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. }
  47. private function reportToChannel(){
  48. $link = ReportToChannelUserAndOrderService::getReportUrl($this->distribution_channel_id,ReportToChannelUserAndOrderService::$user_type);
  49. if(!$link) return ;
  50. $client = new Client(['timeout'=>3.0]);
  51. try{
  52. $result = $client->request('post',$link,['form_params'=>[
  53. 'uid'=>$this->uid,
  54. 'distribution_channel_id'=>$this->distribution_channel_id,
  55. 'register_ip'=>$this->register_ip,
  56. 'user_agent'=>$this->user_agent,
  57. 'user_created_at'=>$this->user_created_at,
  58. 'event'=>'user'
  59. ]])->getBody()->getContents();
  60. if($result && str_contains(strtolower($result),self::SUCCESS)){
  61. return ;
  62. }
  63. $error = 'result error ,'.$result;
  64. }catch (\Exception $e){
  65. $error = 'exception,'.$e->getMessage();
  66. }
  67. DB::table('report_to_channel_users')->insert([
  68. 'uid'=>$this->uid,
  69. 'distribution_channel_id'=>$this->distribution_channel_id,
  70. 'register_ip'=>$this->register_ip,
  71. 'user_agent'=>$this->user_agent,
  72. 'user_created_at'=>$this->user_created_at,
  73. 'report_status'=>0,
  74. 'error_msg'=>$error,
  75. 'created_at'=>date('Y-m-d H:i:s'),
  76. 'updated_at'=>date('Y-m-d H:i:s'),
  77. ]);
  78. }
  79. }