SendMessageCommand.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Events\SuccessSignMessageEvent;
  4. use App\Events\SuccessSignSureEvent;
  5. use App\Models\AuthManage;
  6. use App\Models\Basic;
  7. use App\Models\Code;
  8. use App\Models\User;
  9. use GuzzleHttp\Client;
  10. use Illuminate\Console\Command;
  11. use Illuminate\Support\Facades\Auth;
  12. use Illuminate\Support\Facades\Log;
  13. use Overtrue\EasySms\EasySms;
  14. class SendMessageCommand extends Command
  15. {
  16. /**
  17. * The name and signature of the console command.
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'SendMessageCommand';
  22. /**
  23. * The console command description.
  24. *
  25. * @var string
  26. */
  27. protected $description = 'Command description';
  28. /**
  29. * Create a new command instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct()
  34. {
  35. parent::__construct();
  36. }
  37. /**
  38. * Execute the console command.
  39. *
  40. * @return mixed
  41. */
  42. public function handle()
  43. {
  44. $basic=Basic::all();
  45. foreach ($basic as $k=>$v){
  46. $season[$v->keys]=$v->value;
  47. }
  48. $code=Code::where('season',$season['season'])->where('type',$season['type'])->with(['get_user'])->get();
  49. if ($code){
  50. foreach ($code as $k=>$v){
  51. $re=Code::find($v->id);
  52. if ($v->mini_message_num==0){
  53. $easySms=new EasySms(config('easysms'));
  54. $year=date('Y',$season['time_start']);
  55. $month=date('m',$season['time_start']);
  56. $day=date('d',$season['time_start']);
  57. if ($v->type==0){
  58. $mean='实战营';
  59. }elseif ($v->type==1){
  60. $mean='密训营';
  61. }
  62. try{
  63. $data=AuthManage::where('season',$v->season)->where('type',$v->type)->pluck('phone')->toArray();
  64. $phone=User::where('id',$v->user_id)->value('phone');
  65. $client=new Client();
  66. $url='http://api.admin.app.jiuweiyun.cn/api/sign_message';
  67. $array=[
  68. 'form_params'=>[
  69. 'data'=>$data,
  70. 'mobile'=>$phone,
  71. 'season'=>$v->season,
  72. 'type'=>$v->type,
  73. 'appids'=>'daweiboshi_szy',
  74. 'secret'=>'daweiboshi_szy_secret'
  75. ]
  76. ];
  77. $client->request('post',$url,$array);
  78. }catch (\Exception $exception){
  79. Log::error($exception);
  80. }
  81. }
  82. if ($v->account_message_num==0){
  83. try{
  84. $data=[
  85. 'template_id'=>'BTmiw1Lr630I5ADYVX8Pzy75e4M7OmtgUBcCPVj7CKo',
  86. 'touser'=>$v->get_user->openid,
  87. 'page'=>'',
  88. 'data'=>[
  89. 'thing5'=>[
  90. 'value'=>'第'.$v->season.'届大卫博士创业'.$mean,
  91. ],
  92. 'thing7'=>[
  93. 'value'=>$season['province'].'-'.$season['city']
  94. ],
  95. 'time6'=>[
  96. 'value'=>date('Y年m月d日',$season['time_start'])
  97. ],
  98. 'thing4'=>[
  99. 'value'=>'请提前一天前往举办地签到'
  100. ]
  101. ]
  102. ];
  103. event(new SuccessSignMessageEvent($data));
  104. }catch (\Exception $exception){
  105. Log::error('订阅消息发送失败'.$exception);
  106. }
  107. }
  108. if ($v->sms_message_num==0){
  109. try {
  110. $content="【大卫博士】恭喜你,成功报名第".$v->season.'届大卫博士创业'.$mean.'。活动将于'.$year.'年'.$month.'月'.$day.'日开始,请提前一天前往举办地签到。';
  111. $easySms->send($v->get_user->phone, ['content' =>$content]);
  112. } catch (\Overtrue\EasySms\Exceptions\NoGatewayAvailableException $exception) {
  113. $message = $exception->getException('qcloud')->getMessage();
  114. Log::error($message);
  115. }
  116. }
  117. $re->sms_message_num=1;
  118. $re->account_message_num=1;
  119. $re->mini_message_num=1;
  120. $re->save();
  121. }
  122. }
  123. }
  124. }