CheckPlatform.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /*
  3. * 检测refresh_token是否可用
  4. */
  5. namespace app\admin\command;
  6. use app\common\library\Redis;
  7. use app\common\library\WeChatObject;
  8. use app\common\model\Platform;
  9. use app\common\model\Ptoken;
  10. use EasyWeChat\Factory;
  11. use Symfony\Component\Cache\Simple\RedisCache;
  12. use think\Config;
  13. use think\console\Command;
  14. use think\console\input\Option;
  15. use think\console\Input;
  16. use think\console\Output;
  17. use app\common\model\AdminConfig;
  18. use app\common\model\Config as dbconfig;
  19. use think\Log;
  20. use think\Request;
  21. class CheckPlatform extends Command
  22. {
  23. protected $message = '';
  24. protected function configure()
  25. {
  26. $this->setName('CheckPlatform')
  27. ->addOption("platform","p",Option::VALUE_REQUIRED,'三方平台id')
  28. ->addOption("adminid","a",Option::VALUE_REQUIRED,'要单独检测的adminid 支持英文逗号分隔的多个')
  29. ->addOption("delay","d",Option::VALUE_REQUIRED)
  30. ->setDescription('Here is the remark ');
  31. }
  32. //获取公众号
  33. protected function execute(Input $input, Output $output)
  34. {
  35. Request::instance()->module('admin'); //cli模式下无法获取到当前的项目模块,手动指定一下
  36. $pid = $input->getOption('platform');
  37. if ($pid === null) {
  38. $output->writeln("输入参数有误");
  39. throw new \Exception('Please input correct platform type');
  40. }
  41. $pid = intval($pid);
  42. $admin_id = $input->getOption('adminid');
  43. $output->writeln("检测refresh_token---开始");
  44. Log::info("检测refresh_token---开始");
  45. $delay = $input->getOption('delay');
  46. $admin_config = new AdminConfig();
  47. if(!empty($admin_id)){
  48. $adminlist = $admin_config
  49. ->join('admin', 'admin.id=admin_config.admin_id')
  50. ->join('ptoken', 'admin.id=ptoken.admin_id and ptoken.platform_id=' . $pid)
  51. ->field('admin_config.*,admin.id,admin.username,admin.nickname,ptoken.platform_id as platid,ptoken.refresh_token as rtoken')
  52. ->where(['admin.status' => 'normal'])
  53. ->where('admin.id', 'in', $admin_id)
  54. ->select();
  55. }else {
  56. $adminlist = $admin_config
  57. ->join('admin', 'admin.id=admin_config.admin_id')
  58. ->join('ptoken', 'admin.id=ptoken.admin_id and ptoken.platform_id=' . $pid)
  59. ->field('admin_config.*,admin.id,admin.username,admin.nickname,ptoken.platform_id as platid,ptoken.refresh_token as rtoken')
  60. ->where(['admin.status' => 'normal'])
  61. ->select();
  62. }
  63. $count = 0; //服务号总数
  64. $new = 0; //服务号新增异常数量
  65. foreach ($adminlist as &$value) {
  66. if(empty($value['wx_menu']) || empty($value['appid']) || empty($value['rtoken'])){
  67. Log::info("admin_id=".$value['admin_id']."的用户未授权公众平台,跳过");
  68. continue;
  69. }
  70. $count++;
  71. $output->writeln("检测refresh_token---渠道ID:{$value['id']} 账号:{$value['username']} 昵称:{$value['nickname']} APPID:{$value['appid']}");
  72. Log::info("检测refresh_token---渠道ID:{$value['id']} 账号:{$value['username']} 昵称:{$value['nickname']} APPID:{$value['appid']}");
  73. $output->writeln("检测refresh_token---服务号授权状态:ok");
  74. Log::info("检测refresh_token---服务号授权状态:ok");
  75. $menulist = $this->emptyWechatMenu($value);
  76. if ($menulist) {
  77. if ($menulist === 1) {
  78. $errMsg = ' 错误日志:' . $this->message;
  79. } else {
  80. $errMsg = '';
  81. }
  82. $output->writeln("检测refresh_token---服务号菜单状态:异常" . $errMsg);
  83. Log::info("检测refresh_token---服务号菜单状态:异常" . $errMsg);
  84. $new++;
  85. $output->writeln("检测refresh_token---发送微信报警通知");
  86. Log::info("检测refresh_token---发送微信报警通知");
  87. $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'];
  88. if ($menulist === 1) {
  89. $msg .= "\n错误日志:" . $this->message;
  90. }
  91. $this->SendWorkChatMessage($msg);
  92. if(empty($delay)){
  93. $delay = 1;
  94. }
  95. sleep($delay); //检测一个延时1秒
  96. }else{
  97. $output->writeln("检测refresh_token---服务号菜单状态:ok");
  98. Log::info("检测refresh_token---服务号菜单状态:ok");
  99. }
  100. }
  101. $msg = date('Y-m-d H:i:s') . " refresh_token异常数量:{$new} 服务号总数:{$count}";
  102. $this->SendWorkChatMessage($msg);
  103. $output->writeln("检测refresh_token---完毕!日志->" . $msg);
  104. Log::info("检测refresh_token---完毕!日志->" . $msg);
  105. }
  106. //获取菜单
  107. public function emptyWechatMenu($config)
  108. {
  109. if (empty($config)) {
  110. return false;
  111. }
  112. try {
  113. $adminInfo = model('AdminConfig')->getAdminInfoAll($config['admin_id']);
  114. $wechat = new WeChatObject($adminInfo);
  115. $officialAccount = $wechat->getOfficialAccountByPlatform($config['platid'],$config['appid'],$config['rtoken']);
  116. $menu = $officialAccount->menu->list();//Log::write(1,'1111');
  117. if (empty($menu)) {
  118. return true;
  119. }
  120. } catch (\Exception $exception) {
  121. Log::error('检测refresh_token---微信检查脚本触发异常!config:' . json_encode($config));
  122. $this->message = $exception->getMessage();
  123. return 1;
  124. }
  125. return false;
  126. }
  127. //发企业微信
  128. public function SendWorkChatMessage($content)
  129. {
  130. if (empty($content)) {
  131. return false;
  132. }
  133. $wechat = Config::get('wechat');
  134. $wechat['http']['base_uri'] = $wechat['work']['base_uri'];
  135. $wechat['http']['timeout'] = 20;
  136. $wechat['corp_id'] = $wechat['work']['corp_id'];
  137. $wechat['secret'] = $wechat['work']['secret'];
  138. $app = Factory::work($wechat);
  139. $app['cache'] = new RedisCache(Redis::instanceCache());
  140. $res = $app->message
  141. ->message($content)
  142. ->ofAgent($wechat['work']['agent_id'])
  143. ->toParty($wechat['work']['party_id'])
  144. ->send();
  145. return $res;
  146. }
  147. }