[菜单说明, 处理方法] * @var array */ protected $actions = [ 'stat' => ['查看群池状态', 'stat'], 'sync' => ['同步企业微信客户群,补充群池', 'syncList'], 'refresh' => ['刷新所有已分配群的二维码', 'refreshAll'], 'refreshOne' => ['刷新指定需求群的二维码', 'refreshOne'], 'recycle' => ['回收指定需求占用的群,归还群池', 'recycle'], ]; protected function configure() { $this->setName('groupChat:manage') ->addArgument('action', Argument::OPTIONAL, '要执行的功能:' . implode('|', array_keys($this->actions))) ->setDescription('企业微信沟通群管理:群池同步 / 二维码刷新 / 回收 / 状态查看'); } protected function execute(Input $input, Output $output) { $action = trim((string)$input->getArgument('action')); if ($action === '') { $this->showMenu($input, $output); return; } $this->dispatch($action, $input, $output); } /** * 交互菜单 */ protected function showMenu(Input $input, Output $output) { $choices = array_merge(array_keys($this->actions), ['exit']); while (true) { $output->writeln(''); $output->writeln('企业微信沟通群管理'); foreach ($this->actions as $key => $item) { $output->writeln(sprintf(' %-12s%s', $key, $item[0])); } $output->writeln(sprintf(' %-12s%s', 'exit', '退出')); $action = $output->choice($input, '请选择要执行的功能', $choices, 'stat'); if ($action === 'exit') { $output->writeln('已退出'); return; } $this->dispatch($action, $input, $output); } } /** * 调度到具体处理方法 */ protected function dispatch($action, Input $input, Output $output) { if (!isset($this->actions[$action])) { $output->writeln("未知功能:{$action}"); $output->writeln('可用功能:' . implode('、', array_keys($this->actions))); return; } $method = $this->actions[$action][1]; try { $this->$method($input, $output); } catch (\Exception $e) { $output->writeln('执行失败:' . $e->getMessage() . ''); } } /** * 查看群池状态 */ protected function stat(Input $input, Output $output) { $stat = DemandGroupChatService::stat(); $output->writeln(sprintf( '群总数:%d 空闲:%d 已分配:%d 无二维码:%d', $stat['total'], $stat['free'], $stat['used'], $stat['no_qrcode'] )); if ($stat['free'] < 3) { $output->writeln('提示:空闲群不足 3 个,请先在企微建群,再执行 sync 补充'); } } /** * 同步企业微信客户群 */ protected function syncList(Input $input, Output $output) { $output->writeln('正在从企业微信拉取客户群列表...'); $num = DemandGroupChatService::syncList(); $output->writeln($num > 0 ? "已新入库 {$num} 个群" : '没有新的群需要入库(群主名下暂无新增客户群)'); $this->stat($input, $output); } /** * 刷新所有已分配群的二维码 */ protected function refreshAll(Input $input, Output $output) { $output->writeln('正在刷新已分配群的二维码...'); $res = DemandGroupChatService::refreshAll(); $output->writeln(sprintf('共 %d 个:成功 %d,失败 %d', $res['total'], $res['success'], $res['fail'])); if ($res['fail']) { $output->writeln('失败的需求ID:' . implode(', ', $res['failDemandIds']) . ',详情见日志'); } } /** * 刷新指定需求群的二维码 */ protected function refreshOne(Input $input, Output $output) { $demandId = (int)$output->ask($input, '请输入需求ID', ''); if (!$demandId) { $output->writeln('需求ID不能为空'); return; } if (!DemandGroupChatService::refreshByDemand($demandId)) { $output->writeln("需求 {$demandId} 没有已分配的沟通群,或刷新失败,详情见日志"); return; } $output->writeln("需求 {$demandId} 的群二维码已刷新"); } /** * 回收指定需求占用的群 */ protected function recycle(Input $input, Output $output) { $demandId = (int)$output->ask($input, '请输入要回收的需求ID', ''); if (!$demandId) { $output->writeln('需求ID不能为空'); return; } if (!$output->confirm($input, "确认回收需求 {$demandId} 占用的沟通群?", false)) { $output->writeln('已取消'); return; } if (!DemandGroupChatService::recycle($demandId)) { $output->writeln("需求 {$demandId} 没有已分配的沟通群"); return; } $output->writeln("需求 {$demandId} 的沟通群已归还群池"); $this->stat($input, $output); } }