123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- /*
- * 检测公众号是否被封
- */
- namespace app\admin\command;
- use app\common\library\Redis;
- use app\common\library\WeChatObject;
- use EasyWeChat\Factory;
- use Symfony\Component\Cache\Simple\RedisCache;
- use think\Config;
- use think\console\Command;
- use think\console\Input;
- use think\console\input\Option;
- use think\console\Output;
- use app\common\model\AdminConfig;
- use app\common\model\Config as dbconfig;
- use think\Log;
- use think\Exception;
- use think\Request;
- class GetAllOpenid extends Command
- {
- protected $message = '';
- protected function configure()
- {
- $this->setName('GetAllOpenid')
- ->addOption("channel_id","c",Option::VALUE_REQUIRED)//渠道商id 必填
- ->addOption("next_openid","o",Option::VALUE_REQUIRED)//next_openid非必填
- ->setDescription('获取指定渠道商服务号下用户openid');
- }
- protected function execute(Input $input, Output $output)
- {
- Request::instance()->module('admin'); //cli模式下无法获取到当前的项目模块,手动指定
- $channel_id = $input->getOption('channel_id');
- $next_openid = $input->getOption('next_openid');
- if ($channel_id === null || !is_numeric($channel_id)) {
- throw new Exception('Please input correct find type');
- }
- try{
- $info = model("AdminConfig")->getAdminInfoAll($channel_id);
- $wechat = new WeChatObject($info);
- $officialAccount = $wechat->getOfficialAccount();
- //拉取openid列表
- $n = 1;
- $result = $officialAccount->user->list($next_openid);
- Log::info("GA 第{$n}次拉取开始 next_openid:{$next_openid}");
- $output->writeln("GA 第{$n}次拉取开始 next_openid:{$next_openid}");
- if(isset($result['count'])){
- Log::info("GA 第{$n}次拉取{$result['count']}条 成功 共:".$result['total']."条");
- $output->writeln("GA 第{$n}次拉取{$result['count']}条 成功 共:".$result['total']."条");
- if($result['count']>0){
- do{
- $dataarr = [];
- //循环处理openid列表 每100条入库一次
- $num = 0;
- foreach ($result['data']['openid'] as $v){
- $num++;
- $dataarr[] = ['openid'=>$v,'channel_id'=>$channel_id];
- $count = count($dataarr);
- if($count==100 || count($result['data']['openid'])==$num){
- $sql = model("Oldwx")->fetchSql()->insertAll($dataarr);
- Log::info("GA SQL:{$sql}");
- $rt = model("Oldwx")->execute($sql);
- if($rt){
- Log::info("GA 插入openid 成功 共:{$rt}条 final_openid:{$dataarr[$count-1]['openid']}");
- $output->writeln("GA 插入openid 成功 共:{$rt}条 final_openid:{$dataarr[$count-1]['openid']}");
- $dataarr = [];
- }else{
- Log::error("GA 插入openid 失败 first_openid:{$dataarr[0]['openid']}");
- $output->error("GA 插入openid 失败 first_openid:{$dataarr[0]['openid']}");
- die;
- }
- }
- }
- Log::info("GA 第{$n}次拉取处理完成 共:{$result['count']}条 next_openid:{$result['next_openid']}");
- $output->writeln("GA 第{$n}次拉取处理完成 共:{$result['count']}条 next_openid:{$result['next_openid']}");
-
- $n++;
- Log::info("GA 第{$n}次拉取开始 next_openid:{$result['next_openid']}");
- $output->writeln("GA 第{$n}次拉取开始 next_openid:{$result['next_openid']}");
- $result = $officialAccount->user->list($result['next_openid']);
- if(isset($result['count'])){
- Log::info("GA 第{$n}次拉取{$result['count']}条 成功 共:".$result['total']."条");
- $output->writeln("GA 第{$n}次拉取{$result['count']}条 成功 共:".$result['total']."条");
- }else{
- Log::error("GA 接口第{$n}拉取openid 错误!message:".$result['errmsg']);
- $output->writeln("GA 接口第{$n}拉取openid 错误!message:".$result['errmsg']);
- }
- }while(!empty($result['count']));//无更多数据终止
- }
- }else{
- Log::error("GA 接口第{$n}拉取openid 错误!message:".$result['errmsg']);
- $output->error("GA 接口第{$n}拉取openid 错误!message:".$result['errmsg']);
- }
- Log::info("GA 获取服务号openid 结束");
- $output->writeln("GA 获取服务号openid 结束");
- // $result = $officialAccount->user->list($next_openid);
- // if(isset($result['count'])){
- // Log::error("GA 拉取openid 开始 共:".$result['total']."条");
- // $output->writeln("GA 拉取openid 开始 共:".$result['total']."条");
- // $n = 1;
- // while(!empty($result['count'])){//无更多数据终止
- // $dataarr = [];
- // //循环处理openid列表 每100条入库一次
- // foreach ($result['data']['openid'] as $v){
- // $dataarr[] = ['openid'=>$v,'channel_id'=>$channel_id];
- // $count = count($dataarr);
- // if($count==100 || count($result['data']['openid'])==$count){
- // $rt = model("Oldwx")->insertAll($dataarr);
- // if($rt){
- // Log::info("GA 插入openid 成功 共:{$rt}条 final_openid:{$dataarr[$count-1]['openid']}");
- // $output->writeln("GA 插入openid 成功 共:{$rt}条final_openid:{$dataarr[$count-1]['openid']}");
- // $dataarr = [];
- // }else{
- // Log::error("GA 插入openid 失败 first_openid:{$dataarr[0]['openid']}");
- // $output->writeln("GA 插入openid 失败 first_openid:{$dataarr[0]['openid']}");
- // }
- // }
- // }
- // Log::info("GA 第{$n}次拉取处理完成 共:{$result['count']}条 next_openid:{$result['next_openid']}");
- // $output->writeln("GA 第{$n}次拉取处理完成 共:{$result['count']}条 next_openid:{$result['next_openid']}");
- // $result = $officialAccount->user->list($result['next_openid']);
- // $n++;
- // }
- // Log::info("GA 获取服务号openid 结束");
- // $output->writeln("GA 获取服务号openid 结束");
- // }else{
- // Log::error("GA 接口拉取openid 错误!message:".$result['errmsg']);
- // $output->writeln("GA 接口拉取openid 错误!message:".$result['errmsg']);
- // }
- }catch (Exception $e){
- Log::error("GA 获取服务号openid 触发异常!message:".$e->getMessage());
- $output->error("GA 获取服务号openid 触发异常!message:".$e->getMessage());
- }
- }
- }
|