CouponExpirePush.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: z-yang
  5. * Date: 2020/9/9
  6. * Time: 14:20
  7. */
  8. namespace App\Console\Commands\SmartPush;
  9. use DB;
  10. use GuzzleHttp\Pool;
  11. use GuzzleHttp\Client;
  12. use Illuminate\Console\Command;
  13. use App\Http\Controllers\WechatController;
  14. use GuzzleHttp\Psr7\Request as GuzzleRequest;
  15. class CouponExpirePush extends Command
  16. {
  17. /**
  18. * The name and signature of the console command.
  19. *
  20. * @var string
  21. */
  22. protected $signature = 'SmartPush:CouponExpirePush';
  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. /**
  39. * Execute the console command.
  40. *
  41. * @return mixed
  42. */
  43. public function handle()
  44. {
  45. $this->send();
  46. }
  47. private function send(){
  48. $client = new Client();
  49. $requests = $this->generate();
  50. $pool = new Pool($client, $requests, [
  51. 'concurrency' => 25,
  52. 'fulfilled' => function ($response, $index) {
  53. },
  54. 'rejected' => function ($reason, $index) {
  55. },
  56. ]);
  57. $promise = $pool->promise();
  58. $promise->wait();
  59. }
  60. private function generate(){
  61. $uids = DB::connection('api_mysql')->table('user_coupon')
  62. ->where('use_status',1)
  63. ->where('expire_time','>=',date('Y-m-d H:i:s',time()+86400))
  64. ->where('expire_time','<=',date('Y-m-d H:i:s',time()+86400+3600))
  65. ->select('uid')
  66. ->get();
  67. if(!$uids) return ;
  68. foreach ($uids as $item){
  69. $user = DB::connection('api_mysql')->table('temp_force_subscribe_users')
  70. ->select('id','uid','distribution_channel_id','openid','appid')->where('uid',$item->uid)->first();
  71. if(!$user) continue;
  72. $user_info = DB::connection('api_mysql')->table('users')->where('id',$item->uid)->select('nickname')->first();
  73. $nickname = '';
  74. if($user_info && $user_info->nickname)$nickname = $user_info->nickname;
  75. $content_format = "尊敬的会员:%s\r\n\r\n您有一张优惠券即将到期\r\n<a href='%s'>点击立即使用</a>";
  76. $link = 'https://site'.encodeDistributionChannelId($user->distribution_channel_id).'.leyuee.com/pay?fromtype=coupon_expire_push';
  77. $content = sprintf($content_format,$nickname,$link);
  78. $accecc_token = $this->getAccessToken($user->appid);
  79. if(!$accecc_token)continue;
  80. $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$accecc_token;
  81. $request = new GuzzleRequest('post',$url,[],\GuzzleHttp\json_encode([
  82. 'touser'=>$user->openid,
  83. 'msgtype'=>'text',
  84. 'text'=>['content'=>$content]
  85. ],JSON_UNESCAPED_UNICODE));
  86. yield $request;
  87. }
  88. }
  89. private function getAccessToken($appid){
  90. $WechatController = new WechatController($appid);
  91. $accessToken = $WechatController->app->access_token; // EasyWeChat\Core\AccessToken 实例
  92. $token = $accessToken->getToken(); // token 字符串
  93. return $token;
  94. }
  95. }