123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- namespace App\Console\Commands\GZH;
- use Illuminate\Console\Command;
- use Redis;
- use WechatOP;
- use DB;
- use Log;
- class FansStat extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'gzh:fansStat';
- /**
- * 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()
- {
- $now_date = date('Y-m-d');
- $now_time = date('Y-m-d H:i:s');
- $yesterday = date('Y-m-d',strtotime('-1 day'));
- print_r("====start gzh:fansStat deal {$yesterday}====".date('Y-m-d H:i:s')."\r\n");
- Log::info("====start gzh:fansStat deal {$yesterday}====");
- $official_accounts = DB::table('wangdu.official_accounts')->where('is_auth',1)->where('is_enabled',1)->get();
- foreach($official_accounts as $official_account)
- {
- $official_account_id = $official_account->id;
- if(DB::table('wangdu.official_account_stats')->where('official_account_id',$official_account_id)->where('cumulate_user_updated_at',$yesterday)->count()) continue;
- $appid = $official_account->appid;
- $redis_key = '[wechat_op.common.component_refresh_token.'.$appid.']';
- $component_refresh_token = Redis::Get($redis_key);
- $options = [
- 'app_id' => $appid,
- 'secret' => env('WECHAT_OP_SECRET'), // 仅适用于 单独配置公众号
- 'token' => env('WECHAT_OP_TOKEN'), // 仅适用于 单独配置公众号
- 'aes_key' => env('WECHAT_OP_AES_KEY'), // 仅适用于 单独配置公众号
- 'auth_type' => 'COMPONENT', // COMPONENT 开放平台授权公众号,MANUAL 单独配置公众号
- 'component_refresh_token' => $component_refresh_token, // 授权回调时获取的 authorizer_refresh_token,仅适用于 开放品台授权公众号
- 'cache' => [
- 'driver' => 'redis', // redis, filesystem, laravel
- 'dir' => storage_path('tmp') // 只有为filesystem时候这个目录才有效
- ],
- ];
- try{
- $app = WechatOP::app($options);
- $stats = $app->stats;
- $user_stats = $stats->userCumulate($yesterday,$yesterday);
-
- $cumulate_user_num = 0; // 初始化
- foreach ($user_stats->list as $item)
- {
- $cumulate_user_num = $item['cumulate_user'];
- $cumulate_user_updated_at = $item['ref_date'];
- }
- $updated_at = $created_at = $now_time;
- //更新日统计todo
- $stat = DB::table('wangdu.official_account_stats')->where('official_account_id',$official_account_id)->first();
- //更新累计统计
- if($stat)
- {
- DB::table('wangdu.official_account_stats')->where('official_account_id',$official_account_id)->update(compact('cumulate_user_num','cumulate_user_updated_at','updated_at'));
- }else{
- DB::table('wangdu.official_account_stats')->insert(compact('official_account_id','cumulate_user_num','cumulate_user_updated_at','updated_at','created_at'));
- }
- }catch (\Exception $e){
- Log::info("appid:".$appid." error:".$e->getMessage());
- };
- }
- print_r("====end gzh:fansStat deal {$yesterday}====".date('Y-m-d H:i:s')."\r\n");
- Log::info("====end gzh:fansStat deal {$yesterday}====".date('Y-m-d H:i:s'));
- }
- }
|