setName('CheckWechatForbidden') ->addOption("delay","d",Option::VALUE_REQUIRED) ->setDescription('Here is the remark '); } //获取公众号 protected function execute(Input $input, Output $output) { Request::instance()->module('admin'); //cli模式下无法获取到当前的项目模块,手动指定一下 $output->writeln("检测微信服务号状态---开始"); Log::info("检测微信服务号状态---开始"); $delay = $input->getOption('delay'); $admin_config = new AdminConfig(); $adminlist = $admin_config ->join('admin', 'admin.id=admin_config.admin_id','LEFT') ->join('admin_extend','admin_extend.admin_id=admin_config.admin_id','LEFT') ->join('auth_group_access ac','ac.uid=admin_config.admin_id','LEFT') ->field('admin_config.*,admin.id,admin.username,admin.nickname,ac.group_id,admin_extend.create_by') ->where('admin.status','=','normal') ->where('admin_config.is_fouce','=','1') ->select(); $redis = Redis::instance(); $count = 0; //服务号总数 $jmp = 0; //服务号跳过检测数量(半小时内有交互) $err = 0; //服务号历史异常数量 $new = 0; //服务号新增异常数量 $re = 0; //服务号恢复数量 $configModel = $configdb = new dbconfig(); $siteconfig = $configModel->getConfigSiteArr(); $theme = $siteconfig['theme']; switch ($theme) { case 'qy': $themeName = '袋鼠'; break; case 'sf': $themeName = '沙发'; break; case 'ms': $themeName = '美书'; break; case 'px': $themeName = '点看'; break; case 'xd': $themeName = '熊大'; break; case 'yg': $themeName = '阳光'; break; case '': $themeName = '西瓜'; break; default: $themeName = $theme; } //$output->writeln("当前平台是:".$siteconfig['theme']); foreach ($adminlist as &$value) { if(empty($value['wx_menu']) || empty($value['appid'])){ Log::info("admin_id=".$value['admin_id']."的用户未授权公众平台,跳过"); continue; } $adminInfo = $admin_config->getAdminInfoAll($value['admin_id']); if(empty($adminInfo['refresh_token'])){ //判断refresh_tokan是否为空,为空跳过 Log::info("admin_id=".$value['admin_id']."的用户refresh_token为空,跳过"); continue; } $count++; $value['refresh_token'] = $adminInfo['refresh_token']; $output->writeln("检测微信服务号状态---渠道ID:{$value['id']} 账号:{$value['username']} 昵称:{$value['nickname']} APPID:{$value['appid']}"); Log::info("检测微信服务号状态---渠道ID:{$value['id']} 账号:{$value['username']} 昵称:{$value['nickname']} APPID:{$value['appid']}"); if (!empty($value['appid']) && !empty($value['refresh_token'])) { $output->writeln("检测微信服务号状态---服务号授权状态:ok"); Log::info("检测微信服务号状态---服务号授权状态:ok"); $operate_time = $redis->hget('WEOP',$value['admin_id']); if (!$operate_time || (time() - $operate_time) > 1800) { //无交互时间 或者 交互时间大约半小时 if ($operate_time) { $redis->hdel('WEOP', $value['admin_id']); } $key2 = 'F:' . $value['admin_id']; $menulist = $this->emptyWechatMenu($value); if ($menulist) { if ($menulist === 1) { $errMsg = ' 错误日志:' . $this->message; } else { $errMsg = ''; } $output->writeln("检测微信服务号状态---服务号菜单状态:异常" . $errMsg); Log::info("检测微信服务号状态---服务号菜单状态:异常" . $errMsg); if ($redis->exists($key2)) { $err++; $output->writeln("检测微信服务号状态---报警通知:30天内已发送,跳过"); Log::info("检测微信服务号状态---报警通知:30天内已发送,跳过"); continue; } $new++; $output->writeln("检测微信服务号状态---发送微信报警通知"); Log::info("检测微信服务号状态---发送微信报警通知"); $khr = "\n"; if($value['group_id'] == 4){ $khr.="开户渠道商ID:".$value['create_by']; $admin = new Admin(); $adminInfos = $admin->join('admin_extend ae','admin.id=ae.admin_id')->field('admin.*,ae.create_by')->where('admin.id','=',$value['create_by'])->find(); $khr.="\n开户渠道商账号:".$adminInfos->username; $khr.="\n开户渠道商昵称:".$adminInfos->nickname; $madminInfos = $admin->where('id','=',$adminInfos->create_by)->find(); $khr.="\n开户管理员ID:".$adminInfos->create_by; $khr.="\n开户管理员账号:".$madminInfos->username; $khr.="\n开户管理员昵称:".$madminInfos->nickname; }elseif($value['group_id'] == 3){ $khr.="开户管理员ID:".$value['create_by']; $admin = new Admin(); $adminInfos = $admin->where('id','=',$value['create_by'])->find(); $khr.="\n开户管理员账号:".$adminInfos->username; $khr.="\n开户管理员昵称:".$adminInfos->nickname; } $msg = "平台:{$themeName} 发现异常!" . date('Y-m-d H:i:s') . "\n公众号:" . ($value['json']['authorizer_info']['nick_name']??'') . "\n原始ID:" . ($value['json']['authorizer_info']['user_name']??'') ."\nAppid:".$value['appid']. "\n后台ID:" . $value['id'] . "\n账号:" . $value['username'] . "\n昵称:" . $value['nickname'].$khr; if ($menulist === 1) { if (stripos($this->message, 'Resolving timed')) { $msg .= "\n错误说明: API超时,商务请忽略"; } if (stripos($this->message, 'user limited')) { $msg .= "\n错误说明: 公众号被封或菜单功能被封,请商务通知渠道"; } if (stripos($this->message, 'component is not authorized')) { $msg .= "\n错误说明: 公众号授权三方权限有问题,请商务通知渠道"; } $msg .= "\n错误日志:" . $this->message; } $this->SendWorkChatMessage($msg); $redis->setex($key2, 86400*30, 1); } else { $output->writeln("检测微信服务号状态---服务号菜单状态:ok"); Log::info("检测微信服务号状态---服务号菜单状态:ok"); if ($redis->exists($key2)) { $re++; $redis->del($key2); } } if(empty($delay)){ $delay = 1; } sleep($delay); //检测一个延时1秒 } else { // 存在redis hash name,且交互时间小于半小时 $jmp++; $output->writeln("检测微信服务号状态---半小时内存在交互,跳过检测"); Log::info("检测微信服务号状态---半小时内存在交互,跳过检测"); } }else{ $output->writeln("检测微信服务号状态---服务号授权状态:异常"); Log::info("检测微信服务号状态---服务号授权状态:异常"); } } $msg = "平台:{$themeName} ".date('Y-m-d H:i:s') . " 新增异常:{$new} 历史异常:{$err} 恢复正常:{$re} 跳过检测:{$jmp} 服务号总数:{$count}"; $this->SendWorkChatMessage($msg); $output->writeln("检测微信服务号状态---完毕!日志->" . $msg); Log::info("检测微信服务号状态---完毕!日志->" . $msg); } //获取菜单 public function emptyWechatMenu($config) { if (empty($config)) { return false; } try { $admin_config = new AdminConfig(); $info = $admin_config->getAdminInfoAll($config['admin_id']); $wechat = new WeChatObject($info); $officialAccount = $wechat->getOfficialAccount($config['appid'], $config['refresh_token']); $menu = $officialAccount->menu->list();//Log::write(1,'1111'); if (empty($menu)) { return true; } } catch (\Exception $exception) { Log::error('检测微信服务号状态---微信检查脚本触发异常!config:' . json_encode($config)); $this->message = $exception->getMessage(); return 1; } return false; } //发企业微信 public function SendWorkChatMessage($content) { if (empty($content)) { return false; } $wechat = Config::get('wechat'); $wechat['http']['base_uri'] = $wechat['work']['base_uri']; $wechat['http']['timeout'] = 20; $wechat['corp_id'] = $wechat['work']['corp_id']; $wechat['secret'] = $wechat['work']['secret']; $app = Factory::work($wechat); $app['cache'] = new RedisCache(Redis::instanceCache()); $res = $app->message ->message($content) ->ofAgent($wechat['work']['agent_id']) ->toParty($wechat['work']['party_id']) ->send(); return $res; } }