Переглянути джерело

refactor: unified getIPLocation function

Cat 2 роки тому
батько
коміт
2172f61887

+ 2 - 5
src/Controllers/Admin/IpController.php

@@ -7,7 +7,6 @@ namespace App\Controllers\Admin;
 use App\Controllers\BaseController;
 use App\Models\Ip;
 use App\Models\LoginIp;
-use App\Utils\QQWry;
 use App\Utils\Tools;
 use Exception;
 use Psr\Http\Message\ResponseInterface;
@@ -71,10 +70,9 @@ final class IpController extends BaseController
         $logins = LoginIp::orderBy('id', 'desc')->paginate($length, '*', '', $page);
         $total = LoginIp::count();
 
-        $QQWry = new QQWry();
         foreach ($logins as $login) {
             $login->user_name = $login->userName();
-            $login->location = $login->location($QQWry);
+            $login->location = Tools::getIpLocation($login->ip);
             $login->datetime = Tools::toDateTime((int) $login->datetime);
             $login->type = $login->type();
         }
@@ -113,12 +111,11 @@ final class IpController extends BaseController
         $alives = Ip::where('datetime', '>=', time() - 60)->orderBy('id', 'desc')->paginate($length, '*', '', $page);
         $total = count(Ip::where('datetime', '>=', time() - 60)->orderBy('id', 'desc')->get());
 
-        $QQWry = new QQWry();
         foreach ($alives as $alive) {
             $alive->user_name = $alive->userName();
             $alive->node_name = $alive->nodeName();
             $alive->ip = Tools::getRealIp($alive->ip);
-            $alive->location = $alive->location($QQWry);
+            $alive->location = Tools::getIpLocation($alive->ip);
             $alive->datetime = Tools::toDateTime((int) $alive->datetime);
         }
 

+ 2 - 3
src/Controllers/Admin/SubscribeLogController.php

@@ -6,7 +6,7 @@ namespace App\Controllers\Admin;
 
 use App\Controllers\BaseController;
 use App\Models\UserSubscribeLog;
-use App\Utils\QQWry;
+use App\Utils\Tools;
 use Exception;
 use Psr\Http\Message\ResponseInterface;
 use Slim\Http\Response;
@@ -55,9 +55,8 @@ final class SubscribeLogController extends BaseController
         $subscribes = UserSubscribeLog::orderBy('id', 'desc')->paginate($length, '*', '', $page);
         $total = UserSubscribeLog::count();
 
-        $QQWry = new QQWry();
         foreach ($subscribes as $subscribe) {
-            $subscribe->location = $subscribe->location($QQWry);
+            $subscribe->location = Tools::getIpLocation($subscribe->request_ip);
         }
 
         return $response->withJson([

+ 0 - 14
src/Models/Ip.php

@@ -5,8 +5,6 @@ declare(strict_types=1);
 namespace App\Models;
 
 use App\Services\DB;
-use App\Utils\QQWry;
-use App\Utils\Tools;
 use function strval;
 
 /**
@@ -55,18 +53,6 @@ final class Ip extends Model
         return $this->node()->name;
     }
 
-    /**
-     * 获取 IP 位置
-     */
-    public function location(?QQWry $QQWry = null): string
-    {
-        if ($QQWry === null) {
-            $QQWry = new QQWry();
-        }
-        $location = $QQWry->getlocation(Tools::getRealIp($this->ip));
-        return iconv('gbk', 'utf-8//IGNORE', $location['country'] . $location['area']);
-    }
-
     /**
      * 时间
      */

+ 0 - 14
src/Models/LoginIp.php

@@ -4,8 +4,6 @@ declare(strict_types=1);
 
 namespace App\Models;
 
-use App\Utils\QQWry;
-
 /**
  * Ip Model
  */
@@ -50,18 +48,6 @@ final class LoginIp extends Model
         return date('Y-m-d H:i:s', $this->datetime);
     }
 
-    /**
-     * 获取 IP 位置
-     */
-    public function location(?QQWry $QQWry = null): string
-    {
-        if ($QQWry === null) {
-            $QQWry = new QQWry();
-        }
-        $location = $QQWry->getlocation($this->ip);
-        return iconv('gbk', 'utf-8//IGNORE', $location['country'] . $location['area']);
-    }
-
     /**
      * 登录成功与否
      */

+ 0 - 13
src/Models/UserSubscribeLog.php

@@ -4,7 +4,6 @@ declare(strict_types=1);
 
 namespace App\Models;
 
-use App\Utils\QQWry;
 use voku\helper\AntiXSS;
 
 final class UserSubscribeLog extends Model
@@ -29,18 +28,6 @@ final class UserSubscribeLog extends Model
         return User::find($this->user_id);
     }
 
-    /**
-     * 获取 IP 位置
-     */
-    public function location(?QQWry $QQWry = null): false|string
-    {
-        if ($QQWry === null) {
-            $QQWry = new QQWry();
-        }
-        $location = $QQWry->getlocation($this->request_ip);
-        return iconv('gbk', 'utf-8//IGNORE', $location['country'] . $location['area']);
-    }
-
     /**
      * 记录订阅日志
      *

+ 6 - 10
src/Utils/Telegram/Callbacks/Callback.php

@@ -14,7 +14,6 @@ use App\Models\Payback;
 use App\Models\Setting;
 use App\Models\UserSubscribeLog;
 use App\Services\Config;
-use App\Utils\QQWry;
 use App\Utils\Telegram\Reply;
 use App\Utils\Telegram\TelegramTools;
 use App\Utils\Tools;
@@ -406,14 +405,13 @@ final class Callback
         switch ($OpEnd) {
             case 'login_log':
                 // 登录记录
-                $iplocation = new QQWry();
                 $total = LoginIp::where('userid', '=', $this->User->id)->where('type', '=', 0)->orderBy('datetime', 'desc')->take(10)->get();
                 $text = '<strong>以下是您最近 10 次的登录 IP 和地理位置:</strong>' . PHP_EOL;
                 $text .= PHP_EOL;
 
                 foreach ($total as $single) {
-                    $location = $iplocation->getlocation($single->ip);
-                    $text .= $single->ip . ' - ' . iconv('gbk', 'utf-8//IGNORE', $location['country'] . $location['area']) . PHP_EOL;
+                    $location = Tools::getIpLocation($single->ip);
+                    $text .= $single->ip . ' - ' . $location . PHP_EOL;
                 }
 
                 $text .= PHP_EOL . '<strong>注意:地理位置根据 IP 数据库预估,可能与实际位置不符,仅供参考使用</strong>' . PHP_EOL;
@@ -432,7 +430,6 @@ final class Callback
                 break;
             case 'usage_log':
                 // 使用记录
-                $iplocation = new QQWry();
                 $total = Ip::where('datetime', '>=', time() - 300)->where('userid', '=', $this->User->id)->get();
                 $text = '<strong>以下是您最近 5 分钟的使用 IP 和地理位置:</strong>' . PHP_EOL;
                 $text .= PHP_EOL;
@@ -443,8 +440,8 @@ final class Callback
                     if ($is_node) {
                         continue;
                     }
-                    $location = $iplocation->getlocation($single->ip);
-                    $text .= $single->ip . ' - ' . iconv('gbk', 'utf-8//IGNORE', $location['country'] . $location['area']) . PHP_EOL;
+                    $location = Tools::getIpLocation($single->ip);
+                    $text .= $single->ip . ' - ' . $location . PHP_EOL;
                 }
 
                 $text .= PHP_EOL . '<strong>注意:地理位置根据 IP 数据库预估,可能与实际位置不符,仅供参考使用</strong>' . PHP_EOL;
@@ -485,12 +482,11 @@ final class Callback
                 break;
             case 'subscribe_log':
                 // 订阅记录
-                $iplocation = new QQWry();
                 $logs = UserSubscribeLog::orderBy('id', 'desc')->where('user_id', $this->User->id)->take(10)->get();
                 $temp = [];
                 foreach ($logs as $log) {
-                    $location = $iplocation->getlocation($log->request_ip);
-                    $temp[] = '<code>' . $log->request_time . ' 在 [' . $log->request_ip . '] ' . iconv('gbk', 'utf-8//IGNORE', $location['country'] . $location['area']) . ' 访问了 ' . $log->subscribe_type . ' 订阅</code>';
+                    $location = Tools::getIpLocation($log->request_ip);
+                    $temp[] = '<code>' . $log->request_time . ' 在 [' . $log->request_ip . '] ' . $location . ' 访问了 ' . $log->subscribe_type . ' 订阅</code>';
                 }
                 $text = '<strong>以下是您最近 10 条订阅记录:</strong>';
                 $text .= PHP_EOL . PHP_EOL;

+ 1 - 1
src/Utils/Tools.php

@@ -21,7 +21,7 @@ final class Tools
     /**
      * 查询IP归属
      */
-    public static function getIpInfo($ip): false|string
+    public static function getIpLocation($ip): false|string
     {
         $iplocation = new QQWry();
         $location = $iplocation->getlocation($ip);