Sfoglia il codice sorgente

处理账户下无域名的情况

luolongfei 3 anni fa
parent
commit
42fca012b1

+ 11 - 0
app/Console/FreeNom.php

@@ -11,6 +11,7 @@
 namespace Luolongfei\App\Console;
 
 use Luolongfei\App\Exceptions\LlfException;
+use Luolongfei\App\Exceptions\WarningException;
 use GuzzleHttp\Client;
 use GuzzleHttp\Cookie\CookieJar;
 use Luolongfei\Libs\Log;
@@ -40,6 +41,9 @@ class FreeNom extends Base
     // 匹配登录状态的正则
     const LOGIN_STATUS_REGEX = '/<li.*?Logout.*?<\/li>/i';
 
+    // 匹配无域名的正则
+    const NO_DOMAIN_REGEX = '/<tr\sclass="carttablerow"><td\scolspan="5">(?P<msg>[^<]+)<\/td><\/tr>/i';
+
     /**
      * @var Client
      */
@@ -141,9 +145,14 @@ class FreeNom extends Base
      *
      * @return array
      * @throws LlfException
+     * @throws WarningException
      */
     protected function getAllDomains(string $domainStatusPage)
     {
+        if (preg_match(self::NO_DOMAIN_REGEX, $domainStatusPage, $m)) {
+            throw new WarningException(34520014, [$this->username, $m['msg']]);
+        }
+
         if (!preg_match_all(self::DOMAIN_INFO_REGEX, $domainStatusPage, $allDomains, PREG_SET_ORDER)) {
             throw new LlfException(34520003);
         }
@@ -428,6 +437,8 @@ class FreeNom extends Base
                 $token = $this->getToken($domainStatusPage);
 
                 $this->renewAllDomains($allDomains, $token);
+            } catch (WarningException $e) {
+                system_log(sprintf(lang('100129'), $e->getMessage()));
             } catch (LlfException $e) {
                 system_log(sprintf(lang('100051'), $e->getMessage()));
                 $this->sendExceptionReport($e);

+ 29 - 0
app/Exceptions/WarningException.php

@@ -0,0 +1,29 @@
+<?php
+/**
+ * 警告
+ *
+ * @author luolongf <[email protected]>
+ * @date 2022-02-22
+ * @time 14:06
+ */
+
+namespace Luolongfei\App\Exceptions;
+
+class WarningException extends \Exception
+{
+    public function __construct($code, $additional = null, \Exception $previous = null)
+    {
+        $message = lang('exception_msg.' . $code) ?: '';
+
+        if ($additional !== null) {
+            if (is_array($additional)) {
+                array_unshift($additional, $message);
+                $message = call_user_func_array('sprintf', $additional);
+            } else if (is_string($additional)) {
+                $message = sprintf($message, $additional);
+            }
+        }
+
+        parent::__construct($message . "(Warning code: {$code})", $code, $previous);
+    }
+}

+ 1 - 1
libs/Lang.php

@@ -19,7 +19,7 @@ class Lang extends Base
     public function init()
     {
         // 读取语言包,语言包位于 resources/lang/ 目录下
-        $this->lang = require sprintf('%s/lang/%s.php', RESOURCES_PATH, env('LANGUAGE', 'zh'));
+        $this->lang = require sprintf('%s/lang/%s.php', RESOURCES_PATH, strtolower(env('LANGUAGE', 'zh')));
     }
 
     /**

+ 2 - 0
resources/lang/en.php

@@ -21,6 +21,7 @@ return [
         '34520010' => 'Missing curl module, can\'t send request, please check your php environment and add curl module at compile time',
         '34520012' => 'You have not configured an email address for incoming mail and may not receive mail. Please change the value corresponding to TO in the .env file to the email address you use most often to receive domain-related emails from the robot email address',
         '34520013' => 'Error getting domain status, error message: %s',
+        '34520014' => 'No domain name found under your account %s (%s)',
     ],
     'messages' => [
         '100001' => 'The cookie value named WHMCSZH5eHTGhfvzP could not be obtained, so this login is not valid, please check if your account or password is correct',
@@ -151,5 +152,6 @@ return [
         '100126' => 'Corporate WeChat failed to send a message: <red>%s</red>',
         '100127' => 'Corporate WeChat interface did not return the expected data response, this response data is: ',
         '100128' => 'Multiple invalid access_token prompts have been detected, possibly because the access_token was not properly obtained, please intervene to investigate: ',
+        '100129' => 'Warning: <light_yellow>%s</light_yellow>',
     ],
 ];

+ 2 - 0
resources/lang/zh.php

@@ -21,6 +21,7 @@ return [
         '34520010' => '缺少 curl 模块,无法发送请求,请检查你的 php 环境并在编译时带上 curl 模块',
         '34520012' => '你尚未配置收信邮箱,可能无法收到通知邮件。请将 .env 文件中的 TO 对应的值改为你最常用的邮箱地址,用于接收机器人邮箱发出的域名相关邮件',
         '34520013' => '获取域名状态页面出错,错误信息:%s',
+        '34520014' => '你的账户 %s 名下没有发现域名,可能不存在域名。(%s)',
     ],
     'messages' => [
         '100001' => '未能取得名为 WHMCSZH5eHTGhfvzP 的 cookie 值,故本次登录无效,请检查你的账户或密码是否正确。',
@@ -151,5 +152,6 @@ return [
         '100126' => '企业微信送信失败:<red>%s</red>',
         '100127' => '企业微信接口未返回预期的数据响应,本次响应数据为:',
         '100128' => '检测到多次提示 access_token 失效,可能是未能正确获取 access_token,请介入调查:',
+        '100129' => '警告:<light_yellow>%s</light_yellow>',
     ],
 ];