浏览代码

chore: user model clean up

M1Screw 1 年之前
父节点
当前提交
97e2902178

+ 2 - 1
src/Controllers/User/InfoController.php

@@ -6,6 +6,7 @@ namespace App\Controllers\User;
 
 use App\Controllers\BaseController;
 use App\Models\Config;
+use App\Models\InviteCode;
 use App\Models\User;
 use App\Services\Auth;
 use App\Services\Cache;
@@ -240,7 +241,7 @@ final class InfoController extends BaseController
     {
         $user = $this->user;
         $user->clearInviteCodes();
-        $code = $this->user->addInviteCode();
+        $code = (new InviteCode())->add($user->id);
 
         return $response->withJson([
             'ret' => 1,

+ 3 - 2
src/Controllers/User/RateController.php

@@ -6,6 +6,7 @@ namespace App\Controllers\User;
 
 use App\Controllers\BaseController;
 use App\Services\DynamicRate;
+use App\Services\Subscribe;
 use App\Utils\ResponseHelper;
 use Exception;
 use Psr\Http\Message\ResponseInterface;
@@ -21,7 +22,7 @@ final class RateController extends BaseController
      */
     public function index(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
     {
-        $nodes = $this->user->getUserFrontEndNodes();
+        $nodes = Subscribe::getUserNodes($this->user);
         $node_list = [];
 
         foreach ($nodes as $node) {
@@ -40,7 +41,7 @@ final class RateController extends BaseController
 
     public function ajax(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
     {
-        $nodes = $this->user->getUserFrontEndNodes();
+        $nodes = Subscribe::getUserNodes($this->user);
         $node = $nodes->find($request->getParam('node_id'));
 
         if ($node === null) {

+ 2 - 1
src/Controllers/User/ServerController.php

@@ -5,6 +5,7 @@ declare(strict_types=1);
 namespace App\Controllers\User;
 
 use App\Controllers\BaseController;
+use App\Services\Subscribe;
 use App\Utils\Tools;
 use Exception;
 use Psr\Http\Message\ResponseInterface;
@@ -18,7 +19,7 @@ final class ServerController extends BaseController
      */
     public function index(ServerRequest $request, Response $response, array $args): ResponseInterface
     {
-        $nodes = $this->user->getUserFrontEndNodes();
+        $nodes = Subscribe::getUserNodes($this->user, true);
         $node_list = [];
 
         foreach ($nodes as $node) {

+ 1 - 1
src/Controllers/UserController.php

@@ -108,7 +108,7 @@ final class UserController extends BaseController
         $code = (new InviteCode())->where('user_id', $this->user->id)->first()?->code;
 
         if ($code === null) {
-            $code = $this->user->addInviteCode();
+            $code = (new InviteCode())->add($this->user->id);
         }
 
         $paybacks = (new Payback())->where('ref_by', $this->user->id)

+ 10 - 0
src/Models/InviteCode.php

@@ -4,6 +4,7 @@ declare(strict_types=1);
 
 namespace App\Models;
 
+use App\Utils\Tools;
 use Illuminate\Database\Query\Builder;
 
 /**
@@ -17,4 +18,13 @@ final class InviteCode extends Model
 {
     protected $connection = 'default';
     protected $table = 'user_invite_code';
+
+    public function add(int $user_id): string
+    {
+        $this->code = Tools::genRandomChar(10);
+        $this->user_id = $user_id;
+        $this->save();
+
+        return $this->code;
+    }
 }

+ 1 - 63
src/Models/User.php

@@ -8,15 +8,12 @@ use App\Services\IM;
 use App\Utils\Hash;
 use App\Utils\Tools;
 use GuzzleHttp\Exception\GuzzleException;
-use Illuminate\Database\Eloquent\Collection;
 use Illuminate\Database\Query\Builder;
 use Ramsey\Uuid\Uuid;
 use Telegram\Bot\Exceptions\TelegramSDKException;
 use function date;
-use function is_null;
 use function md5;
 use function round;
-use function time;
 use const PHP_EOL;
 
 /**
@@ -107,23 +104,6 @@ final class User extends Model
         return Tools::genSs2022UserPk($this->passwd, $len);
     }
 
-    public function getUserFrontEndNodes(): Collection
-    {
-        $query = Node::query();
-        $query->where('type', 1);
-
-        if (! $this->is_admin) {
-            $group = ($this->node_group !== 0 ? [0, $this->node_group] : [0]);
-            $query->whereIn('node_group', $group);
-        }
-
-        return $query->where(static function ($query): void {
-            $query->where('node_bandwidth_limit', '=', 0)->orWhereRaw('node_bandwidth < node_bandwidth_limit');
-        })->orderBy('node_class')
-            ->orderBy('name')
-            ->get();
-    }
-
     /**
      * DiceBear 头像
      */
@@ -170,19 +150,6 @@ final class User extends Model
         return $this->save();
     }
 
-    /**
-     * 生成邀请码
-     */
-    public function addInviteCode(): string
-    {
-        $code = new InviteCode();
-        $code->code = Tools::genRandomChar(10);
-        $code->user_id = $this->id;
-        $code->save();
-
-        return $code->code;
-    }
-
     /**
      * 生成新的 API Token
      */
@@ -217,17 +184,6 @@ final class User extends Model
         return Tools::autoBytes($this->transfer_total);
     }
 
-    /*
-     * 已用流量占总流量的百分比
-     */
-    public function trafficUsagePercent(): float
-    {
-        return $this->transfer_enable === 0 ?
-            0
-            :
-            round(($this->u + $this->d) / $this->transfer_enable, 2) * 100;
-    }
-
     /*
      * 剩余流量[自动单位]
      */
@@ -309,25 +265,6 @@ final class User extends Model
         (new InviteCode())->where('user_id', $this->id)->delete();
     }
 
-    /**
-     * 累计充值金额
-     */
-    public function getTopUp(): float
-    {
-        $number = (new Paylist())->where('userid', $this->id)->sum('number');
-        return is_null($number) ? 0.00 : round((float) $number, 2);
-    }
-
-    /**
-     * 在线 IP 个数
-     */
-    public function onlineIpCount(): int
-    {
-        return (new OnlineLog())->where('user_id', $this->id)
-            ->where('last_time', '>', time() - 90)
-            ->count();
-    }
-
     /**
      * 销户
      */
@@ -350,6 +287,7 @@ final class User extends Model
     {
         $this->im_type = 0;
         $this->im_value = '';
+
         return $this->save();
     }
 

+ 1 - 2
src/Services/Bot/Telegram/Callback.php

@@ -937,8 +937,7 @@ final class Callback
             $code = (new InviteCode())->where('user_id', $this->user->id)->first();
 
             if ($code === null) {
-                $this->user->addInviteCode();
-                $code = (new InviteCode())->where('user_id', $this->user->id)->first();
+                $code = (new InviteCode())->add($this->user->id);
             }
 
             $inviteUrl = $_ENV['baseUrl'] . '/auth/register?code=' . $code->code;

+ 0 - 2
src/Services/Bot/Telegram/Message.php

@@ -182,8 +182,6 @@ final class Message
     {
         $text = [
             '当前余额:' . $user->money,
-            '在线 IP 数:' . ($user->node_iplimit !== 0 ? $user->onlineIpCount() .
-                ' / ' . $user->node_iplimit : $user->onlineIpCount() . ' / 不限制'),
             '端口速率:' . ($user->node_speedlimit > 0 ? $user->node_speedlimit . 'Mbps' : '不限制'),
             '上次使用:' . $user->lastUseTime(),
             '过期时间:' . $user->class_expire,

+ 7 - 2
src/Services/Subscribe.php

@@ -37,13 +37,18 @@ final class Subscribe
 
     /**
      * @param $user
+     * @param bool $show_all_nodes
      *
      * @return Collection
      */
-    public static function getUserSubNodes($user): Collection
+    public static function getUserNodes($user, bool $show_all_nodes = false): Collection
     {
         $query = Node::query();
-        $query->where('type', 1)->where('node_class', '<=', $user->class);
+        $query->where('type', 1);
+
+        if (! $show_all_nodes) {
+            $query->where('node_class', '<=', $user->class);
+        }
 
         if (! $user->is_admin) {
             $group = ($user->node_group !== 0 ? [0, $user->node_group] : [0]);

+ 1 - 1
src/Services/Subscribe/Clash.php

@@ -18,7 +18,7 @@ final class Clash extends Base
         $clash_config = $_ENV['Clash_Config'];
         $clash_group_indexes = $_ENV['Clash_Group_Indexes'];
         $clash_group_config = $_ENV['Clash_Group_Config'];
-        $nodes_raw = Subscribe::getUserSubNodes($user);
+        $nodes_raw = Subscribe::getUserNodes($user);
 
         foreach ($nodes_raw as $node_raw) {
             $node_custom_config = json_decode($node_raw->custom_config, true);

+ 1 - 1
src/Services/Subscribe/SIP002.php

@@ -19,7 +19,7 @@ final class SIP002 extends Base
             return $links;
         }
 
-        $nodes_raw = Subscribe::getUserSubNodes($user);
+        $nodes_raw = Subscribe::getUserNodes($user);
 
         foreach ($nodes_raw as $node_raw) {
             $node_custom_config = json_decode($node_raw->custom_config, true);

+ 1 - 1
src/Services/Subscribe/SIP008.php

@@ -19,7 +19,7 @@ final class SIP008 extends Base
             return '';
         }
 
-        $nodes_raw = Subscribe::getUserSubNodes($user);
+        $nodes_raw = Subscribe::getUserNodes($user);
 
         foreach ($nodes_raw as $node_raw) {
             $node_custom_config = json_decode($node_raw->custom_config, true);

+ 1 - 1
src/Services/Subscribe/SS.php

@@ -19,7 +19,7 @@ final class SS extends Base
             return $links;
         }
 
-        $nodes_raw = Subscribe::getUserSubNodes($user);
+        $nodes_raw = Subscribe::getUserNodes($user);
 
         foreach ($nodes_raw as $node_raw) {
             if ((int) $node_raw->sort === 0) {

+ 1 - 1
src/Services/Subscribe/SingBox.php

@@ -16,7 +16,7 @@ final class SingBox extends Base
     {
         $nodes = [];
         $singbox_config = $_ENV['SingBox_Config'];
-        $nodes_raw = Subscribe::getUserSubNodes($user);
+        $nodes_raw = Subscribe::getUserNodes($user);
 
         foreach ($nodes_raw as $node_raw) {
             $node_custom_config = json_decode($node_raw->custom_config, true);

+ 1 - 1
src/Services/Subscribe/Trojan.php

@@ -19,7 +19,7 @@ final class Trojan extends Base
             return $links;
         }
 
-        $nodes_raw = Subscribe::getUserSubNodes($user);
+        $nodes_raw = Subscribe::getUserNodes($user);
 
         foreach ($nodes_raw as $node_raw) {
             $node_custom_config = json_decode($node_raw->custom_config, true);

+ 1 - 1
src/Services/Subscribe/V2Ray.php

@@ -21,7 +21,7 @@ final class V2Ray extends Base
             return $links;
         }
 
-        $nodes_raw = Subscribe::getUserSubNodes($user);
+        $nodes_raw = Subscribe::getUserNodes($user);
 
         foreach ($nodes_raw as $node_raw) {
             $node_custom_config = json_decode($node_raw->custom_config, true);

+ 1 - 1
src/Services/Subscribe/V2RayJson.php

@@ -16,7 +16,7 @@ final class V2RayJson extends Base
     {
         $nodes = [];
         $v2rayjson_config = $_ENV['V2RayJson_Config'];
-        $nodes_raw = Subscribe::getUserSubNodes($user);
+        $nodes_raw = Subscribe::getUserNodes($user);
 
         foreach ($nodes_raw as $node_raw) {
             $node_custom_config = json_decode($node_raw->custom_config, true);