AsyncUserRegister.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 DB;
  10. use GuzzleHttp\Client;
  11. use Illuminate\Bus\Queueable;
  12. use Illuminate\Queue\SerializesModels;
  13. use Illuminate\Queue\InteractsWithQueue;
  14. use Illuminate\Contracts\Queue\ShouldQueue;
  15. use Illuminate\Foundation\Bus\Dispatchable;
  16. /**
  17. * 回传订单给渠道
  18. * Class AsyncUserRegister
  19. * @package App\Jobs\
  20. */
  21. class AsyncUserRegister implements ShouldQueue
  22. {
  23. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  24. private $uid;
  25. private $distribution_channel_id;
  26. private $register_ip;
  27. private $user_agent;
  28. private $user_created_at;
  29. const SUCCESS = 'success';
  30. public function __construct($uid,$distribution_channel_id,$register_ip,$user_agent,$user_created_at){
  31. $this->uid = $uid;
  32. $this->distribution_channel_id = $distribution_channel_id;
  33. $this->register_ip = $register_ip;
  34. $this->user_agent = $user_agent;
  35. $this->user_created_at = $user_created_at;
  36. }
  37. /**
  38. * Execute the job.
  39. *
  40. * @return void
  41. */
  42. public function handle()
  43. {
  44. $this->reportToChannel();
  45. }
  46. private function reportToChannel(){
  47. $link = $this->getReportUrl($this->distribution_channel_id);
  48. if(!$link) return ;
  49. $client = new Client(['timeout'=>3.0]);
  50. try{
  51. $result = $client->request('post',$link,['form_params'=>[
  52. 'uid'=>$this->uid,
  53. 'distribution_channel_id'=>$this->distribution_channel_id,
  54. 'register_ip'=>$this->register_ip,
  55. 'user_agent'=>$this->user_agent,
  56. 'user_created_at'=>$this->user_created_at
  57. ]])->getBody()->getContents();
  58. if($result && str_contains(strtolower($result),self::SUCCESS)){
  59. return ;
  60. }
  61. $error = 'result error ,'.$result;
  62. }catch (\Exception $e){
  63. $error = 'exception,'.$e->getMessage();
  64. }
  65. DB::table('report_to_channel_users')->insert([
  66. 'uid'=>$this->uid,
  67. 'distribution_channel_id'=>$this->distribution_channel_id,
  68. 'register_ip'=>$this->register_ip,
  69. 'user_agent'=>$this->user_agent,
  70. 'user_created_at'=>$this->user_created_at,
  71. 'report_status'=>0,
  72. 'error_msg'=>$error,
  73. 'created_at'=>date('Y-m-d H:i:s'),
  74. 'updated_at'=>date('Y-m-d H:i:s'),
  75. ]);
  76. }
  77. private function getReportUrl($distribution_channel_id){
  78. return '';
  79. }
  80. }