ReportRetry.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z-yang
  5. * Date: 2020/9/30
  6. * Time: 17:09
  7. */
  8. namespace App\Console\Commands\Wechat;
  9. use DB;
  10. use Log;
  11. use Redis;
  12. use Exception;
  13. use GuzzleHttp\Client;
  14. use Illuminate\Console\Command;
  15. class ReportRetry extends Command
  16. {
  17. /**
  18. * The name and signature of the console command.
  19. *
  20. * @var string
  21. */
  22. protected $signature = 'Wechat:ReportRetry {--start_time=} {--end_time=}';
  23. /**
  24. * The console command description.
  25. *
  26. * @var string
  27. */
  28. protected $description = '异常从新上报';
  29. /**
  30. * Create a new command instance.
  31. *
  32. * @return void
  33. */
  34. public function __construct()
  35. {
  36. parent::__construct();
  37. }
  38. private $user_action_set_id = [];
  39. private $assess_token = [];
  40. private $error_msg;
  41. /**
  42. * Execute the console command.
  43. *
  44. * @return mixed
  45. */
  46. public function handle()
  47. {
  48. $start_time = $this->option('start_time');
  49. if(!$start_time){
  50. $start_time = date('Y-m-d 00:00:00');
  51. }
  52. $end_time = $this->option('end_time');
  53. if(!$end_time){
  54. $end_time = date('Y-m-d 23:59:59');
  55. }
  56. $this->startMp($start_time,$end_time);
  57. }
  58. //mp重试
  59. private function startMp($start_time,$end_time){
  60. $error_record = DB::table('wechat_advertise_report_record')
  61. ->where('created_at','>=',$start_time)
  62. ->where('created_at','<=',$end_time)
  63. ->where('result_status','FAIL')
  64. ->select('id','user_action_set_id','url','action_time','action_type','openid','appid','product_id',
  65. 'product_name','bid','value','source','claim_type','object')
  66. ->get();
  67. if(!$error_record) return ;
  68. $client = new Client();
  69. foreach ($error_record as $item){
  70. $report_info = DB::table('wechat_advertise_report_record')
  71. ->where('action_type',$item->action_type)
  72. ->where('product_id',$item->product_id)
  73. ->where('result_status','SUCCESS')->count();
  74. if($report_info) {
  75. continue;
  76. }
  77. $request_data = [
  78. 'actions'=>[
  79. [
  80. 'user_action_set_id'=>$item->user_action_set_id,
  81. 'url'=>$item->url,
  82. 'action_time'=>$item->action_time,
  83. 'action_type'=>$item->action_type,
  84. 'user_id'=>[
  85. 'wechat_app_id'=>$item->appid,
  86. 'wechat_openid'=>$item->openid
  87. ],
  88. 'action_param'=>[
  89. 'object'=>$item->object,
  90. 'product_name'=>$item->product_name,
  91. 'product_id'=>$item->bid,
  92. 'source'=>$item->source,
  93. 'wechat_app_id'=>'',
  94. 'claim_type'=>$item->claim_type,
  95. 'value'=>$item->value
  96. ]
  97. ]
  98. ]
  99. ];
  100. $token = $this->getAccessToken($item->appid);
  101. $result = $this->sendReport($client,$request_data,$token);
  102. if($result && $result['result_status'] == 'SUCCESS'){
  103. $result['updated_at'] = date('Y-m-d H:i:s');
  104. DB::table('wechat_advertise_report_record')->where('id',$item->id)->update($result);
  105. DB::connection('api_mysql')->table('distribution_channel_weixin_ad_report_orders')->where('order_id',$item->product_id)
  106. ->update(['report_status'=>1]);
  107. }
  108. }
  109. }
  110. private function sendReport(Client $client,$request_data,$token){
  111. if(!$token) return '';
  112. $url = 'https://api.weixin.qq.com/marketing/user_actions/add?version=v1.0&access_token='.$token;
  113. $data = [];
  114. try{
  115. $result = $client->request('post',$url,[
  116. 'headers'=>['Content-Type'=>'application/json'],
  117. 'body'=>\GuzzleHttp\json_encode($request_data)
  118. ])->getBody()->getContents();
  119. Log::info('user_actions/add result is: '.$result);
  120. $result_array = \GuzzleHttp\json_decode($result,1);
  121. if(isset($result_array['errcode']) && $result_array['errcode'] == 0){
  122. $data['result_status'] = 'SUCCESS';
  123. $data['result_msg'] = $result_array['errcode'];
  124. $data['result'] = $result;
  125. }else{
  126. $data['result_status'] = 'FAIL';
  127. $data['result_msg'] = isset($result_array['errcode'])?$result_array['errcode']:'unknown';
  128. $data['result'] = $result;
  129. }
  130. return $data;
  131. }catch (Exception $e){
  132. Log::error($e);
  133. }
  134. return $data;
  135. }
  136. private function getAccessToken($appid){
  137. if(isset($this->assess_token[$appid])){
  138. return $this->assess_token[$appid];
  139. }
  140. $access_token = Redis::get('Wechat:access_token:appid:'.$appid);
  141. if($access_token){
  142. $this->assess_token[$appid] = $access_token;
  143. return $this->assess_token[$appid];
  144. }
  145. $this->error_msg = $appid.',access_token fail';
  146. return '';
  147. }
  148. }