123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <?php
- /**
- * Created by PhpStorm.
- * User: z-yang
- * Date: 2019/8/20
- * Time: 14:00
- */
- namespace App\Console\Commands\SmartPush;
- use Illuminate\Console\Command;
- use DB;
- use Redis;
- use App\Modules\Book\Services\BookConfigService;
- use GuzzleHttp\Client;
- use App\Http\Controllers\WechatController;
- use GuzzleHttp\Psr7\Request as GuzzleRequest;
- use Hashids;
- use GuzzleHttp\Pool;
- class KeepContinueRead extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'SmartPush:KeepContinueRead';
- /**
- * 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 generateRequest()
- {
- $switch_array = [];
- $now_hour = date('G');
- $temp = 0;
- $continueReadUrlFormat = 'https://site%s.%s.com%s';
- while (true){
- $user = DB::connection('api_mysql')->table('temp_force_subscribe_users')
- ->select('id','uid','distribution_channel_id','openid','appid')
- ->where('id','>',$temp)
- ->orderBy('id')
- ->limit(1000)
- ->get();
- if(!$user) break;
- foreach ($user as $item){
- /*if(!in_array($item->distribution_channel_id,[5,8,123,146,155,160,211,255,256,691,695,722,4025,4053,4174,4236,4237,4241,4326,4334,4364,4426,4427,4487,4488,4556,4593,4742,4889,4891,5204,5611,6122,6123,6124,6460,6907,6929,6987])){
- continue;
- }*/
-
- \Log::info( '---------------------------start--start-------------------------------------------' );
- //\Log::info('$item->distribution_channel_id is :'.$item->distribution_channel_id );
- \Log::info( '$item->id is :'.$item->id );
- if(!isset($switch_array[$item->distribution_channel_id])){
- $switch = DB::connection('api_mysql')->table('custom_msg_switchs_msgs')
- ->where('distribution_channel_id',$item->distribution_channel_id)
- ->where('custom_category','continue_read')
- ->select('status')
- ->first();
- if($switch){
- $switch_array[$item->distribution_channel_id] = $switch->status;
- }else{
- $switch_array[$item->distribution_channel_id] = 1;
- }
- }
- $switch_status = $switch_array[$item->distribution_channel_id];
- if($switch_status == 2) continue;
- //\Log::info( '$item->distribution_channel_id in is :'.$item->distribution_channel_id );
- $read_info = $this->getFirstReadRecord($item->uid);
- //\Log::info( $read_info );
- \Log::info( 'uid is :'.$item->uid );
- if(!$read_info) continue;
- //获取本次发送的时间间隔
- $this_push_hour = Redis::hget('book_read:'.$item->uid,'next_push_hour');
- //上次发送时间
- $prev_send_info = DB::connection('api_mysql')->table('custom_push_keep_continue')->where('uid',$item->uid)->select('send_time')->orderBy('id','desc')->first();
- $diff = $this_push_hour?$this_push_hour:8;
- if($diff == 8){
- if((time()-$read_info['time']) > 3600*8){
- $send_hour = $now_hour;
- }else{
- $send_hour = -1;
- }
- }else{
- if($prev_send_info){
- //$send_hour = date('G',strtotime($prev_send_info->send_time)+$diff*3600);
- if((time()-strtotime($prev_send_info->send_time)) >$diff*3600){
- $send_hour = $now_hour;
- }else{
- $send_hour = -1;
- }
- }else{
- $send_hour = $now_hour;
- }
- }
- if($send_hour != $now_hour ) continue;
- $openid = $item->openid;
- $appid = $item->appid;
- $content_format = "您看的小说有更新\r\n\r\n<a href='%s'>《%s》剧情已更新,点击继续阅读</a> \r\n\r\n记得点击菜单栏签到领书币哦";
- $content = sprintf($content_format,
- sprintf($continueReadUrlFormat,
- encodeDistributionChannelId($item->distribution_channel_id),
- env('CUSTOM_HOST'),
- $read_info['url']
- ),
- $read_info['book_name']
- );
- $accecc_token = $this->getAccessToken($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'=>$openid,
- 'msgtype'=>'text',
- 'text'=>['content'=>$content]
- ],JSON_UNESCAPED_UNICODE));
- DB::connection('api_mysql')->table('custom_push_keep_continue')->insert([
- 'uid'=>$item->uid,
- 'time_diff'=>$diff,
- 'send_time'=>date('Y-m-d H:i:s'),
- 'day'=>date('Y-m-d'),
- 'created_at'=>date('Y-m-d H:i:s'),
- 'updated_at'=>date('Y-m-d H:i:s')
- ]);
- if($diff == 8){
- $next = 5;
- }else{
- $next = 12;
- }
- Redis::hset('book_read:'.$item->uid,'next_push_hour',$next);
- \Log::info( '----------------------------end-end-end-------------------------------------------' );
- yield $request;
- }
- $temp = $item->id;
- }
- }
- private function getFirstReadRecord($uid){
- //Redis::hget('book_base:' . $uid, 'last_read', "{$bid}_{$cid}_{$book_name}_{$chapter_name}_" . time());
- $record = Redis::hget('book_read:' . $uid, 'last_read');
- if($record){
- $record_arr = explode('_',$record);
- $bid = $record_arr[0];
- $book_name = $this->bid2BookName($bid);
- $bid = Hashids::encode($bid);
- $cid = $record_arr[1];
- $time = $record_arr[2];
- $res = [
- 'url' => '/reader?bid='.$bid.'&cid='.$cid.'&fromtype=continue_read',
- 'book_name'=>$book_name,
- 'bid'=>$bid,
- 'time'=>$time
- ];
- return $res;
- }
- return [];
- }
- private function bid2BookName($bid){
- $book_name = null;
- if(is_null($book_name)){
- $book_key = 'wap:string:book:'.$bid;
- $book_name = Redis::get($book_key);
- Redis::EXPIRE($book_key,3600);
- if(!$book_name){
- $book_name = '';
- $book_info = BookConfigService::getBookById($bid);
- if($book_info && isset($book_info->book_name)){
- $book_name = $book_info->book_name;
- }
- }
- }
- return $book_name;
- }
- private function getAccessToken($appid){
- $WechatController = new WechatController($appid);
- $accessToken = $WechatController->app->access_token; // EasyWeChat\Core\AccessToken 实例
- $token = $accessToken->getToken(); // token 字符串
- return $token;
- }
- private function send(){
- $client = new Client();
- $requests = $this->generateRequest();
- $pool = new Pool($client, $requests, [
- 'concurrency' => 25,
- 'fulfilled' => function ($response, $index) {
- },
- 'rejected' => function ($reason, $index) {
- },
- ]);
- $promise = $pool->promise();
- $promise->wait();
- }
- }
|