SendBatchWechatMaterial.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Jobs;
  3. use App\Http\Controllers\WechatController;
  4. use App\Jobs\Job;
  5. use Illuminate\Queue\SerializesModels;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Redis;
  9. use DB;
  10. class SendBatchWechatMaterial extends Job implements ShouldQueue
  11. {
  12. use InteractsWithQueue, SerializesModels;
  13. private $data;
  14. /**
  15. * Create a new job instance.
  16. *
  17. * @return void
  18. */
  19. public function __construct($data)
  20. {
  21. $this->data = $data;
  22. }
  23. /**
  24. * Execute the job.
  25. *
  26. * @return void
  27. */
  28. public function handle()
  29. {
  30. \Log::info('SendBatchWechatMaterial start');
  31. $param = $this->data;
  32. \Log::info($param);
  33. try{
  34. $param = $param['data'];
  35. $this->start($param);
  36. }catch(\Exception $e){
  37. \Log::error($e->getMessage());
  38. }
  39. \Log::info('SendBatchWechatMaterial end');
  40. }
  41. public function start($param)
  42. {
  43. \Log::info('$param2');\Log::info($param);
  44. //检查参数
  45. if(!($param['appid']&&$param['media_id']&&$param['openids']&&$param['task_id'])){
  46. \Log::info('params_empty');
  47. return;
  48. }
  49. // 只有1个用户,则加一个凑数
  50. if(count($param['openids']) == 1){
  51. $param['openids'][] = 'test';
  52. }
  53. //服务号高级发送接口openid限定数量为2-10000之间
  54. if(!is_array($param['openids'])||count($param['openids'])>10000||count($param['openids'])<2){
  55. \Log::info('SendBatchWechatMaterial:over_open_num_limt');
  56. return;
  57. }
  58. //调用服务号高级发送接口
  59. try{
  60. $WechatController = new WechatController($param['appid']);
  61. $res = $WechatController->app->broadcast->sendNews($param['media_id'],$param['openids']);
  62. \Log::info('sendNews_res:');\Log::info($res);
  63. //发送失败
  64. if($res['errcode']!=0){
  65. \Log::info("SendBatchWechatMateria {$param['task_id']} errcode:{$res['errcode']} errmsg:{{$res['errcode']}}");
  66. return;
  67. }
  68. //保存msg_id和msg_data_id
  69. DB::connection('api_mysql')
  70. ->table('wechat_material_send_msgs')
  71. ->where('id',$param['task_id'])
  72. ->update(['wechat_msg_id'=>$res['msg_id'],'wechat_msg_data_id'=>$res['msg_data_id']]);
  73. //发送成功更新发送人数
  74. DB::connection('api_mysql')
  75. ->table('wechat_material_send_msgs')
  76. ->where('id',$param['task_id'])
  77. ->increment('receive_user_num',count($param['openids']));
  78. //若为最后一条消息则更新发送状态为发送完成
  79. if($param['type']=='last_task'){
  80. DB::connection('api_mysql')
  81. ->table('wechat_material_send_msgs')
  82. ->where('id',$param['task_id'])
  83. ->update(['status'=>'has_send','updated_at'=>date('Y-m-d H:i:s')]);
  84. }
  85. }catch(\Exception $e){
  86. \Log::error('sendNews_ept:'.$e->getMessage());
  87. }
  88. return;
  89. }
  90. /**
  91. * The job failed to process.
  92. *
  93. * @param Exception $exception
  94. * @return void
  95. */
  96. public function failed(Exception $exception)
  97. {
  98. // Send user notification of failure, etc...
  99. v('SendBatchWechatMaterial_ept:'.$exception->getMessage().' data:'.json_encode($this->data));
  100. }
  101. }