GetAllOpenid.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /*
  3. * 检测公众号是否被封
  4. */
  5. namespace app\admin\command;
  6. use app\common\library\Redis;
  7. use app\common\library\WeChatObject;
  8. use EasyWeChat\Factory;
  9. use Symfony\Component\Cache\Simple\RedisCache;
  10. use think\Config;
  11. use think\console\Command;
  12. use think\console\Input;
  13. use think\console\input\Option;
  14. use think\console\Output;
  15. use app\common\model\AdminConfig;
  16. use app\common\model\Config as dbconfig;
  17. use think\Log;
  18. use think\Exception;
  19. use think\Request;
  20. class GetAllOpenid extends Command
  21. {
  22. protected $message = '';
  23. protected function configure()
  24. {
  25. $this->setName('GetAllOpenid')
  26. ->addOption("channel_id","c",Option::VALUE_REQUIRED)//渠道商id 必填
  27. ->addOption("next_openid","o",Option::VALUE_REQUIRED)//next_openid非必填
  28. ->setDescription('获取指定渠道商服务号下用户openid');
  29. }
  30. protected function execute(Input $input, Output $output)
  31. {
  32. Request::instance()->module('admin'); //cli模式下无法获取到当前的项目模块,手动指定
  33. $channel_id = $input->getOption('channel_id');
  34. $next_openid = $input->getOption('next_openid');
  35. if ($channel_id === null || !is_numeric($channel_id)) {
  36. throw new Exception('Please input correct find type');
  37. }
  38. try{
  39. $info = model("AdminConfig")->getAdminInfoAll($channel_id);
  40. $wechat = new WeChatObject($info);
  41. $officialAccount = $wechat->getOfficialAccount();
  42. //拉取openid列表
  43. $n = 1;
  44. $result = $officialAccount->user->list($next_openid);
  45. Log::info("GA 第{$n}次拉取开始 next_openid:{$next_openid}");
  46. $output->writeln("GA 第{$n}次拉取开始 next_openid:{$next_openid}");
  47. if(isset($result['count'])){
  48. Log::info("GA 第{$n}次拉取{$result['count']}条 成功 共:".$result['total']."条");
  49. $output->writeln("GA 第{$n}次拉取{$result['count']}条 成功 共:".$result['total']."条");
  50. if($result['count']>0){
  51. do{
  52. $dataarr = [];
  53. //循环处理openid列表 每100条入库一次
  54. $num = 0;
  55. foreach ($result['data']['openid'] as $v){
  56. $num++;
  57. $dataarr[] = ['openid'=>$v,'channel_id'=>$channel_id];
  58. $count = count($dataarr);
  59. if($count==100 || count($result['data']['openid'])==$num){
  60. $sql = model("Oldwx")->fetchSql()->insertAll($dataarr);
  61. Log::info("GA SQL:{$sql}");
  62. $rt = model("Oldwx")->execute($sql);
  63. if($rt){
  64. Log::info("GA 插入openid 成功 共:{$rt}条 final_openid:{$dataarr[$count-1]['openid']}");
  65. $output->writeln("GA 插入openid 成功 共:{$rt}条 final_openid:{$dataarr[$count-1]['openid']}");
  66. $dataarr = [];
  67. }else{
  68. Log::error("GA 插入openid 失败 first_openid:{$dataarr[0]['openid']}");
  69. $output->error("GA 插入openid 失败 first_openid:{$dataarr[0]['openid']}");
  70. die;
  71. }
  72. }
  73. }
  74. Log::info("GA 第{$n}次拉取处理完成 共:{$result['count']}条 next_openid:{$result['next_openid']}");
  75. $output->writeln("GA 第{$n}次拉取处理完成 共:{$result['count']}条 next_openid:{$result['next_openid']}");
  76. $n++;
  77. Log::info("GA 第{$n}次拉取开始 next_openid:{$result['next_openid']}");
  78. $output->writeln("GA 第{$n}次拉取开始 next_openid:{$result['next_openid']}");
  79. $result = $officialAccount->user->list($result['next_openid']);
  80. if(isset($result['count'])){
  81. Log::info("GA 第{$n}次拉取{$result['count']}条 成功 共:".$result['total']."条");
  82. $output->writeln("GA 第{$n}次拉取{$result['count']}条 成功 共:".$result['total']."条");
  83. }else{
  84. Log::error("GA 接口第{$n}拉取openid 错误!message:".$result['errmsg']);
  85. $output->writeln("GA 接口第{$n}拉取openid 错误!message:".$result['errmsg']);
  86. }
  87. }while(!empty($result['count']));//无更多数据终止
  88. }
  89. }else{
  90. Log::error("GA 接口第{$n}拉取openid 错误!message:".$result['errmsg']);
  91. $output->error("GA 接口第{$n}拉取openid 错误!message:".$result['errmsg']);
  92. }
  93. Log::info("GA 获取服务号openid 结束");
  94. $output->writeln("GA 获取服务号openid 结束");
  95. // $result = $officialAccount->user->list($next_openid);
  96. // if(isset($result['count'])){
  97. // Log::error("GA 拉取openid 开始 共:".$result['total']."条");
  98. // $output->writeln("GA 拉取openid 开始 共:".$result['total']."条");
  99. // $n = 1;
  100. // while(!empty($result['count'])){//无更多数据终止
  101. // $dataarr = [];
  102. // //循环处理openid列表 每100条入库一次
  103. // foreach ($result['data']['openid'] as $v){
  104. // $dataarr[] = ['openid'=>$v,'channel_id'=>$channel_id];
  105. // $count = count($dataarr);
  106. // if($count==100 || count($result['data']['openid'])==$count){
  107. // $rt = model("Oldwx")->insertAll($dataarr);
  108. // if($rt){
  109. // Log::info("GA 插入openid 成功 共:{$rt}条 final_openid:{$dataarr[$count-1]['openid']}");
  110. // $output->writeln("GA 插入openid 成功 共:{$rt}条final_openid:{$dataarr[$count-1]['openid']}");
  111. // $dataarr = [];
  112. // }else{
  113. // Log::error("GA 插入openid 失败 first_openid:{$dataarr[0]['openid']}");
  114. // $output->writeln("GA 插入openid 失败 first_openid:{$dataarr[0]['openid']}");
  115. // }
  116. // }
  117. // }
  118. // Log::info("GA 第{$n}次拉取处理完成 共:{$result['count']}条 next_openid:{$result['next_openid']}");
  119. // $output->writeln("GA 第{$n}次拉取处理完成 共:{$result['count']}条 next_openid:{$result['next_openid']}");
  120. // $result = $officialAccount->user->list($result['next_openid']);
  121. // $n++;
  122. // }
  123. // Log::info("GA 获取服务号openid 结束");
  124. // $output->writeln("GA 获取服务号openid 结束");
  125. // }else{
  126. // Log::error("GA 接口拉取openid 错误!message:".$result['errmsg']);
  127. // $output->writeln("GA 接口拉取openid 错误!message:".$result['errmsg']);
  128. // }
  129. }catch (Exception $e){
  130. Log::error("GA 获取服务号openid 触发异常!message:".$e->getMessage());
  131. $output->error("GA 获取服务号openid 触发异常!message:".$e->getMessage());
  132. }
  133. }
  134. }