123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- /*
- * 检测refresh_token是否可用
- */
- namespace app\admin\command;
- use app\common\library\Redis;
- use app\common\library\WeChatObject;
- use app\common\model\Platform;
- use app\common\model\Ptoken;
- use EasyWeChat\Factory;
- use Symfony\Component\Cache\Simple\RedisCache;
- use think\Config;
- use think\console\Command;
- use think\console\input\Option;
- use think\console\Input;
- use think\console\Output;
- use app\common\model\AdminConfig;
- use app\common\model\Config as dbconfig;
- use think\Log;
- use think\Request;
- class CheckPlatform extends Command
- {
- protected $message = '';
- protected function configure()
- {
- $this->setName('CheckPlatform')
- ->addOption("platform","p",Option::VALUE_REQUIRED,'三方平台id')
- ->addOption("adminid","a",Option::VALUE_REQUIRED,'要单独检测的adminid 支持英文逗号分隔的多个')
- ->addOption("delay","d",Option::VALUE_REQUIRED)
- ->setDescription('Here is the remark ');
- }
- //获取公众号
- protected function execute(Input $input, Output $output)
- {
- Request::instance()->module('admin'); //cli模式下无法获取到当前的项目模块,手动指定一下
- $pid = $input->getOption('platform');
- if ($pid === null) {
- $output->writeln("输入参数有误");
- throw new \Exception('Please input correct platform type');
- }
- $pid = intval($pid);
- $admin_id = $input->getOption('adminid');
- $output->writeln("检测refresh_token---开始");
- Log::info("检测refresh_token---开始");
- $delay = $input->getOption('delay');
- $admin_config = new AdminConfig();
- if(!empty($admin_id)){
- $adminlist = $admin_config
- ->join('admin', 'admin.id=admin_config.admin_id')
- ->join('ptoken', 'admin.id=ptoken.admin_id and ptoken.platform_id=' . $pid)
- ->field('admin_config.*,admin.id,admin.username,admin.nickname,ptoken.platform_id as platid,ptoken.refresh_token as rtoken')
- ->where(['admin.status' => 'normal'])
- ->where('admin.id', 'in', $admin_id)
- ->select();
- }else {
- $adminlist = $admin_config
- ->join('admin', 'admin.id=admin_config.admin_id')
- ->join('ptoken', 'admin.id=ptoken.admin_id and ptoken.platform_id=' . $pid)
- ->field('admin_config.*,admin.id,admin.username,admin.nickname,ptoken.platform_id as platid,ptoken.refresh_token as rtoken')
- ->where(['admin.status' => 'normal'])
- ->select();
- }
- $count = 0; //服务号总数
- $new = 0; //服务号新增异常数量
- foreach ($adminlist as &$value) {
- if(empty($value['wx_menu']) || empty($value['appid']) || empty($value['rtoken'])){
- Log::info("admin_id=".$value['admin_id']."的用户未授权公众平台,跳过");
- continue;
- }
- $count++;
- $output->writeln("检测refresh_token---渠道ID:{$value['id']} 账号:{$value['username']} 昵称:{$value['nickname']} APPID:{$value['appid']}");
- Log::info("检测refresh_token---渠道ID:{$value['id']} 账号:{$value['username']} 昵称:{$value['nickname']} APPID:{$value['appid']}");
- $output->writeln("检测refresh_token---服务号授权状态:ok");
- Log::info("检测refresh_token---服务号授权状态:ok");
- $menulist = $this->emptyWechatMenu($value);
- if ($menulist) {
- if ($menulist === 1) {
- $errMsg = ' 错误日志:' . $this->message;
- } else {
- $errMsg = '';
- }
- $output->writeln("检测refresh_token---服务号菜单状态:异常" . $errMsg);
- Log::info("检测refresh_token---服务号菜单状态:异常" . $errMsg);
- $new++;
- $output->writeln("检测refresh_token---发送微信报警通知");
- Log::info("检测refresh_token---发送微信报警通知");
- $msg = "检测refresh_token--发现异常!" . date('Y-m-d H:i:s') . "\n公众号:" . ($value['json']['authorizer_info']['nick_name']??'') . "\n原始ID:" . ($value['json']['authorizer_info']['user_name']??'') . "\n渠道ID:" . $value['id'] . "\n账号:" . $value['username'] . "\n昵称:" . $value['nickname'];
- if ($menulist === 1) {
- $msg .= "\n错误日志:" . $this->message;
- }
- $this->SendWorkChatMessage($msg);
- if(empty($delay)){
- $delay = 1;
- }
- sleep($delay); //检测一个延时1秒
- }else{
- $output->writeln("检测refresh_token---服务号菜单状态:ok");
- Log::info("检测refresh_token---服务号菜单状态:ok");
- }
- }
- $msg = date('Y-m-d H:i:s') . " refresh_token异常数量:{$new} 服务号总数:{$count}";
- $this->SendWorkChatMessage($msg);
- $output->writeln("检测refresh_token---完毕!日志->" . $msg);
- Log::info("检测refresh_token---完毕!日志->" . $msg);
- }
- //获取菜单
- public function emptyWechatMenu($config)
- {
- if (empty($config)) {
- return false;
- }
- try {
- $adminInfo = model('AdminConfig')->getAdminInfoAll($config['admin_id']);
- $wechat = new WeChatObject($adminInfo);
- $officialAccount = $wechat->getOfficialAccountByPlatform($config['platid'],$config['appid'],$config['rtoken']);
- $menu = $officialAccount->menu->list();//Log::write(1,'1111');
- if (empty($menu)) {
- return true;
- }
- } catch (\Exception $exception) {
- Log::error('检测refresh_token---微信检查脚本触发异常!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;
- }
- }
|