TongBuStatusJob.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Order;
  4. use App\Models\OrderSendLog;
  5. use App\Models\UrlList;
  6. use GuzzleHttp\Client;
  7. use Illuminate\Bus\Queueable;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Queue\SerializesModels;
  12. use Illuminate\Support\Facades\Log;
  13. class TongBuStatusJob implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. /**
  17. * Create a new job instance.
  18. *
  19. * @return void
  20. */
  21. protected $data;
  22. public function __construct($data)
  23. {
  24. $this->data=$data;
  25. }
  26. /**
  27. * Execute the job.
  28. *
  29. * @return void
  30. */
  31. public function handle()
  32. {
  33. $data=$this->data;
  34. foreach ($data as $k=>$v){
  35. $order=Order::where('id',$v)->first();
  36. try {
  37. $url_info=UrlList::where('url',$order->url)->first();
  38. if ($url_info->time<(time()+30)){
  39. $token=$this->getToekn($order->url,$url_info->id);
  40. }else{
  41. $token=$url_info->token;
  42. }
  43. $client=new Client();
  44. $url = $order->url.'/api/Admin/CompanyCustomerOrderSubmit/OrderReviewSuccess';
  45. $data=['ID'=>$order->ZDL_id];
  46. $re=$client->post($url,[
  47. 'headers' => [
  48. 'Content-Type' => 'application/json', // 设置请求头
  49. 'Auth-AdminToken' => $token, // 如果需要身份验证令牌
  50. ],
  51. 'json'=>$data
  52. ]);
  53. $res=$re->getBody()->getContents();
  54. $info=json_decode($res,true);
  55. if (isset($info['success'])){
  56. if (!$info['success']){
  57. $log=new OrderSendLog();
  58. $log->log=$order->order_num.'同步订单出错';
  59. $log->order_id=$order->id;
  60. $log->save();
  61. }
  62. }
  63. // Log::error($res);
  64. }catch (\Exception $exception){
  65. Log::info($exception->getMessage());
  66. $log=new OrderSendLog();
  67. $log->log=$order->order_num.'同步订单出错';
  68. $log->order_id=$order->id;
  69. $log->info=$exception->getMessage();
  70. $log->save();
  71. }
  72. }
  73. }
  74. protected function getToekn($url,$id){
  75. $client=new Client();
  76. $re=$client->post($url.'/api/Admin/System/MainSubLogin',[
  77. 'headers' => [
  78. 'Content-Type' => 'application/json', // 设置请求头
  79. // 'Auth-AdminToken' => 'SjI5Ym5US0xZVzZjSVd4V1FheHg4VG5hL1dpWDJwVi82RDdMY09WU3NTeENIdmJUdmxxTEFVOHZSZkRiaXM1c3RuYUlZTGVIcmI2NGlUdXJ4MXZ6dkREWmRoY283UG5TclQ2RnI5amV3UGYzYjBjNkZ0RTFzUElzRzRhV2F1d0M=', // 如果需要身份验证令牌
  80. ],
  81. 'json'=>['UserName'=>'dwadmin','Password'=>'zS76UMV9']
  82. ]);
  83. $res=$re->getBody()->getContents();
  84. $info=json_decode($res,true);
  85. $urlinfo=UrlList::find($id);
  86. // Log::error($res);
  87. // Log::error($info);
  88. if (!$info['errcode']){
  89. $token=$info['data']['accessToken'];
  90. $urlinfo->token=$token;
  91. $urlinfo->time=time()+9*60;
  92. $urlinfo->save();
  93. return $token;
  94. }else{
  95. Log::error($urlinfo->name.'生成token出错===='.$info['errcode']);
  96. return 0;
  97. }
  98. }
  99. }