AsyncUserRegister.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. ]])->getBody()->getContents();
  59. if($result && str_contains(strtolower($result),self::SUCCESS)){
  60. return ;
  61. }
  62. $error = 'result error ,'.$result;
  63. }catch (\Exception $e){
  64. $error = 'exception,'.$e->getMessage();
  65. }
  66. DB::table('report_to_channel_users')->insert([
  67. 'uid'=>$this->uid,
  68. 'distribution_channel_id'=>$this->distribution_channel_id,
  69. 'register_ip'=>$this->register_ip,
  70. 'user_agent'=>$this->user_agent,
  71. 'user_created_at'=>$this->user_created_at,
  72. 'report_status'=>0,
  73. 'error_msg'=>$error,
  74. 'created_at'=>date('Y-m-d H:i:s'),
  75. 'updated_at'=>date('Y-m-d H:i:s'),
  76. ]);
  77. }
  78. }