FansStat.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Console\Commands\GZH;
  3. use Illuminate\Console\Command;
  4. use Redis;
  5. use WechatOP;
  6. use DB;
  7. use Log;
  8. class FansStat extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'gzh:fansStat';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '公众号后台获取粉丝数据';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. $now_date = date('Y-m-d');
  39. $now_time = date('Y-m-d H:i:s');
  40. $yesterday = date('Y-m-d',strtotime('-1 day'));
  41. print_r("====start gzh:fansStat deal {$yesterday}====".date('Y-m-d H:i:s')."\r\n");
  42. Log::info("====start gzh:fansStat deal {$yesterday}====");
  43. $official_accounts = DB::table('wangdu.official_accounts')->where('is_auth',1)->where('is_enabled',1)->get();
  44. foreach($official_accounts as $official_account)
  45. {
  46. $official_account_id = $official_account->id;
  47. if(DB::table('wangdu.official_account_stats')->where('official_account_id',$official_account_id)->where('cumulate_user_updated_at',$yesterday)->count()) continue;
  48. $appid = $official_account->appid;
  49. $redis_key = '[wechat_op.common.component_refresh_token.'.$appid.']';
  50. $component_refresh_token = Redis::Get($redis_key);
  51. $options = [
  52. 'app_id' => $appid,
  53. 'secret' => env('WECHAT_OP_SECRET'), // 仅适用于 单独配置公众号
  54. 'token' => env('WECHAT_OP_TOKEN'), // 仅适用于 单独配置公众号
  55. 'aes_key' => env('WECHAT_OP_AES_KEY'), // 仅适用于 单独配置公众号
  56. 'auth_type' => 'COMPONENT', // COMPONENT 开放平台授权公众号,MANUAL 单独配置公众号
  57. 'component_refresh_token' => $component_refresh_token, // 授权回调时获取的 authorizer_refresh_token,仅适用于 开放品台授权公众号
  58. 'cache' => [
  59. 'driver' => 'redis', // redis, filesystem, laravel
  60. 'dir' => storage_path('tmp') // 只有为filesystem时候这个目录才有效
  61. ],
  62. ];
  63. try{
  64. $app = WechatOP::app($options);
  65. $stats = $app->stats;
  66. $user_stats = $stats->userCumulate($yesterday,$yesterday);
  67. $cumulate_user_num = 0; // 初始化
  68. foreach ($user_stats->list as $item)
  69. {
  70. $cumulate_user_num = $item['cumulate_user'];
  71. $cumulate_user_updated_at = $item['ref_date'];
  72. }
  73. $updated_at = $created_at = $now_time;
  74. //更新日统计todo
  75. $stat = DB::table('wangdu.official_account_stats')->where('official_account_id',$official_account_id)->first();
  76. //更新累计统计
  77. if($stat)
  78. {
  79. DB::table('wangdu.official_account_stats')->where('official_account_id',$official_account_id)->update(compact('cumulate_user_num','cumulate_user_updated_at','updated_at'));
  80. }else{
  81. DB::table('wangdu.official_account_stats')->insert(compact('official_account_id','cumulate_user_num','cumulate_user_updated_at','updated_at','created_at'));
  82. }
  83. }catch (\Exception $e){
  84. Log::info("appid:".$appid." error:".$e->getMessage());
  85. };
  86. }
  87. print_r("====end gzh:fansStat deal {$yesterday}====".date('Y-m-d H:i:s')."\r\n");
  88. Log::info("====end gzh:fansStat deal {$yesterday}====".date('Y-m-d H:i:s'));
  89. }
  90. }