ForceUserProperty.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 ForceUserProperty extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'gzh:force_user_property';
  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. $task_start_time = time();
  39. print_r("====start gzh:force_user_property deal ====".date('Y-m-d H:i:s')."\r\n");
  40. Log::info("====start gzh:fansStat deal ====");
  41. $active_time = date('Y-m-d H:i:s', time() - 3600*52);
  42. $update_user_num = 0;
  43. $official_accounts = DB::connection('command_mysql')->select("select appid,count(1) from yueduyun.temp_force_subscribe_users t where last_interactive_time > '{$active_time}' and `is_subscribed` = 1 and not exists(select 1 from zhuishuyun_user.force_subscribe_user_properties where appid = t.appid and openid = t.openid) group by appid");
  44. foreach($official_accounts as $official_account)
  45. {
  46. print_r("====appid====".$official_account->appid."\r\n");
  47. $appid = $official_account->appid;
  48. $redis_key = '[wechat_op.common.component_refresh_token.'.$appid.']';
  49. $component_refresh_token = Redis::Get($redis_key);
  50. $options = [
  51. 'app_id' => $appid,
  52. 'secret' => env('WECHAT_OP_SECRET'), // 仅适用于 单独配置公众号
  53. 'token' => env('WECHAT_OP_TOKEN'), // 仅适用于 单独配置公众号
  54. 'aes_key' => env('WECHAT_OP_AES_KEY'), // 仅适用于 单独配置公众号
  55. 'auth_type' => 'COMPONENT', // COMPONENT 开放平台授权公众号,MANUAL 单独配置公众号
  56. 'component_refresh_token' => $component_refresh_token, // 授权回调时获取的 authorizer_refresh_token,仅适用于 开放品台授权公众号
  57. 'cache' => [
  58. 'driver' => 'redis', // redis, filesystem, laravel
  59. 'dir' => storage_path('tmp') // 只有为filesystem时候这个目录才有效
  60. ],
  61. ];
  62. $app = WechatOP::app($options);
  63. $userService = $app->user;
  64. $force_users = DB::connection('command_mysql')->select("select t.* from yueduyun.temp_force_subscribe_users t where appid = '{$appid}' and last_interactive_time > '{$active_time}' and `is_subscribed` = 1 and not exists(select 1 from zhuishuyun_user.force_subscribe_user_properties where appid = t.appid and openid=t.openid)");
  65. $total = count($force_users);
  66. $len = 100;//微信限制 最大100
  67. $save_len = 400;
  68. $openids = [];
  69. $data = [];
  70. foreach ($force_users as $k=>$_user)
  71. {
  72. $openids[] = $_user->openid;
  73. if(count($openids) == $len || $k == $total - 1)//批量获取
  74. {
  75. try{
  76. $users = $userService->batchGet($openids);
  77. $openids = [];
  78. foreach ($users->user_info_list as $item)
  79. {
  80. if(isset($item['nickname']))
  81. {
  82. $current_time = date('Y-m-d H:i:s');
  83. if($item['nickname'])
  84. {
  85. $data[] = [
  86. 'openid'=>$item['openid'],
  87. 'appid'=>$appid,
  88. 'current_nickname'=>trim($item['nickname']),
  89. 'created_at'=>$current_time,
  90. 'updated_at'=>$current_time,
  91. ];
  92. }
  93. }
  94. }
  95. }catch (\Exception $e){
  96. $error_msg = $e->getMessage();
  97. Log::error($appid .'failed '.$error_msg);
  98. if(strstr($error_msg,'component is not authorized by this account') || strstr($error_msg, 'api unauthorized'))
  99. {
  100. //未授权情况下不再采集
  101. break;
  102. }
  103. };
  104. }
  105. if(count($data) >= $save_len || $k == $total - 1)//批量存储
  106. {
  107. $update_user_num += count($data);
  108. DB::connection('command_mysql')->table('zhuishuyun_user.force_subscribe_user_properties')->insert($data);
  109. $data = [];
  110. }
  111. }
  112. }
  113. $used_time = time() - $task_start_time;
  114. print_r("====end gzh:force_user_property deal update user num {$update_user_num} used {$used_time} seconds====".date('Y-m-d H:i:s')."\r\n");
  115. Log::info("====end gzh:force_user_property deal update user num {$update_user_num} used {$used_time} seconds");
  116. }
  117. }