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()); } } }