testWechatMaterialMsgSend.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Console\Commands\ActionTrigger\BatchWechatMaterial;
  3. use App\Http\Controllers\WechatController;
  4. use Illuminate\Console\Command;
  5. use DB;
  6. /**
  7. * 删除微信素材
  8. *
  9. */
  10. class testWechatMaterialMsgSend extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'testWechatMaterialMsgSend';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'testWechatMaterialMsgSend';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return mixed
  37. */
  38. public function handle()
  39. {
  40. \Log::info('SendBatchWechatMaterial start');
  41. $param = [
  42. 'appid'=>'wxdbc486f1b4f6a8c3',
  43. 'media_id'=>'XYhErlq3w-hlmwqOB4O5So616GrRvIOD8MoBjI8IlSY',
  44. 'openids'=>[
  45. '0'=>'oAcqg1LRHNKN2jaEkJ5v56HOwPEQ',
  46. '1'=>'oq6ID0m4KrnpHwwZUh9BBmFsW_18',
  47. ],
  48. 'task_id'=>2,
  49. ];
  50. $this->start($param);
  51. \Log::info('SendBatchWechatMaterial end');
  52. }
  53. public function start($param)
  54. {
  55. \Log::info('$param');\Log::info($param);
  56. //检查参数
  57. if(!($param['appid']&&$param['media_id']&&$param['openids']&&$param['task_id'])){
  58. \Log::info('params_empty');
  59. return;
  60. }
  61. //服务号高级发送接口openid限定数量为2-10000之间
  62. if(!is_array($param['openids'])||count($param['openids'])>10000||count($param['openids'])<2){
  63. \Log::info('SendBatchWechatMaterial:over_open_num_limt');
  64. return;
  65. }
  66. //调用服务号高级发送接口
  67. try{
  68. $WechatController = new WechatController($param['appid']);
  69. $res = $WechatController->app->broadcast->sendNews($param['media_id'],$param['openids']);
  70. \Log::info($res);
  71. //发送失败
  72. if($res['errcode']!=0){
  73. \Log::info("SendBatchWechatMateria {$param['task_id']} errcode:{$res['errcode']} errmsg:{{$res['errcode']}}");
  74. return;
  75. }
  76. //保存msg_id和msg_data_id
  77. DB::connection('api_mysql')
  78. ->table('wechat_material_send_msgs')
  79. ->where('id',$param['task_id'])
  80. ->update(['wechat_msg_id'=>$res['msg_id'],'wechat_msg_data_id'=>$res['msg_data_id']]);
  81. //发送成功更新发送人数
  82. DB::connection('api_mysql')
  83. ->table('wechat_material_send_msgs')
  84. ->where('id',$param['task_id'])
  85. ->increment('send_user_num',count($param['openids']));
  86. //若为最后一条消息则更新发送状态为发送完成
  87. if($param['type']=='last_task'){
  88. DB::connection('api_mysql')
  89. ->table('wechat_material_send_msgs')
  90. ->where('id',$param['task_id'])
  91. ->update(['status'=>'has_send','updated_at'=>date('Y-m-d H:i:s')]);
  92. }
  93. }catch(\Exception $e){
  94. \Log::error($e->getMessage());
  95. }
  96. return;
  97. }
  98. }