123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- /**
- * Created by PhpStorm.
- * User: z-yang
- * Date: 2020/9/9
- * Time: 14:20
- */
- namespace App\Console\Commands\SmartPush;
- use DB;
- use GuzzleHttp\Pool;
- use GuzzleHttp\Client;
- use Illuminate\Console\Command;
- use App\Http\Controllers\WechatController;
- use GuzzleHttp\Psr7\Request as GuzzleRequest;
- class CouponExpirePush extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'SmartPush:CouponExpirePush';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '优惠券到期推送';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $this->send();
- }
- private function send(){
- $client = new Client();
- $requests = $this->generate();
- $pool = new Pool($client, $requests, [
- 'concurrency' => 25,
- 'fulfilled' => function ($response, $index) {
- },
- 'rejected' => function ($reason, $index) {
- },
- ]);
- $promise = $pool->promise();
- $promise->wait();
- }
- private function generate(){
- $uids = DB::connection('api_mysql')->table('user_coupon')
- ->where('use_status',1)
- ->where('expire_time','>=',date('Y-m-d H:i:s',time()+86400))
- ->where('expire_time','<=',date('Y-m-d H:i:s',time()+86400+3600))
- ->select('uid')
- ->get();
- if(!$uids) return ;
- foreach ($uids as $item){
- $user = DB::connection('api_mysql')->table('temp_force_subscribe_users')
- ->select('id','uid','distribution_channel_id','openid','appid')->where('uid',$item->uid)->first();
- if(!$user) continue;
- $user_info = DB::connection('api_mysql')->table('users')->where('id',$item->uid)->select('nickname')->first();
- $nickname = '';
- if($user_info && $user_info->nickname)$nickname = $user_info->nickname;
- $content_format = "尊敬的会员:%s\r\n\r\n您有一张优惠券即将到期\r\n<a href='%s'>点击立即使用</a>";
- $link = 'https://site'.encodeDistributionChannelId($user->distribution_channel_id).'.leyuee.com/pay?fromtype=coupon_expire_push';
- $content = sprintf($content_format,$nickname,$link);
- $accecc_token = $this->getAccessToken($user->appid);
- if(!$accecc_token)continue;
- $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$accecc_token;
- $request = new GuzzleRequest('post',$url,[],\GuzzleHttp\json_encode([
- 'touser'=>$user->openid,
- 'msgtype'=>'text',
- 'text'=>['content'=>$content]
- ],JSON_UNESCAPED_UNICODE));
- yield $request;
- }
- }
- private function getAccessToken($appid){
- $WechatController = new WechatController($appid);
- $accessToken = $WechatController->app->access_token; // EasyWeChat\Core\AccessToken 实例
- $token = $accessToken->getToken(); // token 字符串
- return $token;
- }
- }
|