mirror of
https://github.com/ProxyPanel/ProxyPanel.git
synced 2026-04-02 18:48:36 +00:00
🧹Clean code
This commit is contained in:
@@ -21,12 +21,12 @@ trait ClientApiResponse
|
||||
self::$client = $client;
|
||||
}
|
||||
|
||||
public function succeed(?array $data = null, ?array $addition = null, array $codeResponse = ResponseEnum::HTTP_OK): JsonResponse
|
||||
public function succeed(array $data = null, array $addition = null, array $codeResponse = ResponseEnum::HTTP_OK): JsonResponse
|
||||
{
|
||||
return $this->jsonResponse(1, $codeResponse, $data, $addition);
|
||||
}
|
||||
|
||||
private function jsonResponse(int $status, array $codeResponse, array|string|null $data = null, array|null $addition = null): JsonResponse
|
||||
private function jsonResponse(int $status, array $codeResponse, array|string $data = null, array $addition = null): JsonResponse
|
||||
{
|
||||
[$code, $message] = $codeResponse;
|
||||
$code = $code > 1000 ? (int) ($code / 1000) : $code;
|
||||
@@ -47,7 +47,7 @@ trait ClientApiResponse
|
||||
return response()->json($result, $code, ['content-type' => 'application/json']);
|
||||
}
|
||||
|
||||
public function failed(array $codeResponse = ResponseEnum::HTTP_ERROR, array|string|null $data = null): JsonResponse
|
||||
public function failed(array $codeResponse = ResponseEnum::HTTP_ERROR, array|string $data = null): JsonResponse
|
||||
{
|
||||
return $this->jsonResponse(0, $codeResponse, is_array($data) ? $data[0] : $data);
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ trait ClientConfig
|
||||
return $encode ? base64_encode($uri) : $uri;
|
||||
}
|
||||
|
||||
private function clash(?string $client = null): string
|
||||
private function clash(string $client = null): string
|
||||
{
|
||||
$user = $this->getUser();
|
||||
$webName = sysConfig('website_name');
|
||||
|
||||
@@ -6,12 +6,12 @@ use Illuminate\Http\JsonResponse;
|
||||
|
||||
trait WebApiResponse
|
||||
{
|
||||
public function succeed(?array $data = null, ?array $addition = null, array $codeResponse = ResponseEnum::HTTP_OK): JsonResponse // 成功
|
||||
public function succeed(array $data = null, array $addition = null, array $codeResponse = ResponseEnum::HTTP_OK): JsonResponse // 成功
|
||||
{
|
||||
return $this->jsonResponse('success', $codeResponse, $data, $addition);
|
||||
}
|
||||
|
||||
private function jsonResponse(string $status, array $codeResponse, ?array $data = null, ?array $addition = null): JsonResponse // 返回数据
|
||||
private function jsonResponse(string $status, array $codeResponse, array $data = null, array $addition = null): JsonResponse // 返回数据
|
||||
{
|
||||
[$code, $message] = $codeResponse;
|
||||
if ($status === 'success') {
|
||||
@@ -42,7 +42,7 @@ trait WebApiResponse
|
||||
return $etag;
|
||||
}
|
||||
|
||||
public function failed(array $codeResponse = ResponseEnum::HTTP_ERROR, ?array $data = null): JsonResponse // 失败
|
||||
public function failed(array $codeResponse = ResponseEnum::HTTP_ERROR, array $data = null): JsonResponse // 失败
|
||||
{
|
||||
return $this->jsonResponse('fail', $codeResponse, $data);
|
||||
}
|
||||
|
||||
@@ -142,7 +142,7 @@ class LogsController extends Controller
|
||||
}
|
||||
|
||||
// 在线IP监控(实时)
|
||||
public function onlineIPMonitor(Request $request, ?int $id = null)
|
||||
public function onlineIPMonitor(Request $request, int $id = null)
|
||||
{
|
||||
$query = NodeOnlineIp::with(['node:id,name', 'user:id,username'])->where('created_at', '>=', strtotime('-2 minutes'));
|
||||
|
||||
|
||||
@@ -247,7 +247,7 @@ class ClientController extends Controller
|
||||
return $this->succeed(null, ['config' => $config]);
|
||||
}
|
||||
|
||||
private function clientConfig(?string $key = null)
|
||||
private function clientConfig(string $key = null)
|
||||
{
|
||||
if (! config('client')) {
|
||||
Artisan::call('config:cache');
|
||||
|
||||
@@ -25,7 +25,7 @@ class Article extends Model
|
||||
return $query->whereType($type);
|
||||
}
|
||||
|
||||
public function scopeLang(Builder $query, ?string $language = null): Builder
|
||||
public function scopeLang(Builder $query, string $language = null): Builder
|
||||
{
|
||||
return $query->whereLanguage($language ?? app()->getLocale());
|
||||
}
|
||||
|
||||
@@ -223,7 +223,7 @@ class User extends Authenticatable
|
||||
return $query->where('status', '<>', -1)->whereEnable(0);
|
||||
}
|
||||
|
||||
public function nodes(?int $userLevel = null, ?int $userGroupId = null): Node|Builder|BelongsToMany
|
||||
public function nodes(int $userLevel = null, int $userGroupId = null): Node|Builder|BelongsToMany
|
||||
{
|
||||
if ($userGroupId === null && $this->user_group_id) { // 使用默认的用户分组
|
||||
$query = $this->userGroup->nodes();
|
||||
|
||||
@@ -100,7 +100,7 @@ class OrderService
|
||||
return false;
|
||||
}
|
||||
|
||||
public function resetTimeAndData(?string $expired_at = null): array
|
||||
public function resetTimeAndData(string $expired_at = null): array
|
||||
{ // 计算下次重置与账号过期时间
|
||||
if (! $expired_at) { // 账号有效期
|
||||
$expired_at = $this->getFinallyExpiredTime();
|
||||
|
||||
@@ -17,17 +17,17 @@ class ProxyService
|
||||
|
||||
private static array $servers;
|
||||
|
||||
public function __construct(User|null $user = null)
|
||||
public function __construct(User $user = null)
|
||||
{
|
||||
$this->setUser($user ?? auth()->user());
|
||||
}
|
||||
|
||||
public function setUser(User|null $user): void
|
||||
public function setUser(User $user = null): void
|
||||
{
|
||||
self::$user = $user;
|
||||
}
|
||||
|
||||
public function getUser(): User
|
||||
public function getUser(): ?User
|
||||
{
|
||||
return self::$user;
|
||||
}
|
||||
@@ -37,7 +37,7 @@ class ProxyService
|
||||
return self::$servers;
|
||||
}
|
||||
|
||||
public function getProxyText(string $target, ?int $type = null): string
|
||||
public function getProxyText(string $target, int $type = null): string
|
||||
{
|
||||
$servers = $this->getNodeList($type);
|
||||
if (empty($servers)) {
|
||||
@@ -58,7 +58,7 @@ class ProxyService
|
||||
return $this->clientConfig($target);
|
||||
}
|
||||
|
||||
public function getNodeList(?int $type = null, bool $isConfig = true): array
|
||||
public function getNodeList(int $type = null, bool $isConfig = true): array
|
||||
{
|
||||
$query = self::$user->nodes()->whereIn('is_display', [2, 3]); // 获取这个账号可用节点
|
||||
|
||||
@@ -162,7 +162,7 @@ class ProxyService
|
||||
return match ($type) {
|
||||
1 => 'vmess://'.base64url_encode(json_encode(['v' => '2', 'ps' => $text, 'add' => $url, 'port' => 0, 'id' => 0, 'aid' => 0, 'net' => 'tcp', 'type' => 'none', 'host' => $url, 'path' => '/', 'tls' => 'tls'], JSON_PRETTY_PRINT)),
|
||||
2 => 'trojan://0@0.0.0.0:0?peer=0.0.0.0#'.rawurlencode($text),
|
||||
default => 'ssr://'.base64url_encode('0.0.0.0:0:origin:none:plain:'.base64url_encode('0000').'/?obfsparam=&protoparam=&remarks='.base64url_encode($text).'&group='.base64url_encode(sysConfig('website_name')).'&udpport=0&uot=0'),
|
||||
default => 'ssr://'.base64url_encode('0.0.0.0:0:origin:none:plain:MDAwMA/?obfsparam=&protoparam=&remarks='.base64url_encode($text).'&group='.base64url_encode(sysConfig('website_name')).'&udpport=0&uot=0'),
|
||||
}.PHP_EOL;
|
||||
}
|
||||
|
||||
@@ -171,7 +171,7 @@ class ProxyService
|
||||
self::$servers = $servers;
|
||||
}
|
||||
|
||||
public function getProxyCode(string $target, ?int $type = null): ?string
|
||||
public function getProxyCode(string $target, int $type = null): ?string
|
||||
{// 客户端用代理信息
|
||||
$servers = $this->getNodeList($type);
|
||||
if (empty($servers)) {
|
||||
|
||||
@@ -8,7 +8,7 @@ class TelegramService
|
||||
{
|
||||
private static string $api;
|
||||
|
||||
public function __construct(?string $token = null)
|
||||
public function __construct(string $token = null)
|
||||
{
|
||||
self::$api = 'https://api.telegram.org/bot'.($token ?? sysConfig('telegram_token')).'/';
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ class UserService
|
||||
{
|
||||
private static User $user;
|
||||
|
||||
public function __construct(?User $user = null)
|
||||
public function __construct(User $user = null)
|
||||
{
|
||||
self::$user = $user ?? auth()->user();
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ class CloudFlare implements DNS
|
||||
exit(400);
|
||||
}
|
||||
|
||||
private function send(string $action, array $parameters = [], ?string $identifier = null): array
|
||||
private function send(string $action, array $parameters = [], string $identifier = null): array
|
||||
{
|
||||
$client = Http::timeout(10)->retry(3, 1000)->withHeaders($this->auth)->baseUrl($this->apiHost)->asJson();
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ class Helpers
|
||||
* @param int|null $inviter_id 邀请人
|
||||
* @param string|null $nickname 昵称
|
||||
*/
|
||||
public static function addUser(string $username, string $password, int $transfer_enable, ?string $date = null, ?int $inviter_id = null, ?string $nickname = null): User
|
||||
public static function addUser(string $username, string $password, int $transfer_enable, string $date = null, int $inviter_id = null, string $nickname = null): User
|
||||
{
|
||||
return User::create([
|
||||
'nickname' => $nickname ?? $username,
|
||||
@@ -133,7 +133,7 @@ class Helpers
|
||||
* @param string|null $msgId 对公查询ID
|
||||
* @param string $address 收信方
|
||||
*/
|
||||
public static function addNotificationLog(string $title, string $content, int $type, int $status = 1, ?string $error = null, ?string $msgId = null, string $address = 'admin'): int
|
||||
public static function addNotificationLog(string $title, string $content, int $type, int $status = 1, string $error = null, string $msgId = null, string $address = 'admin'): int
|
||||
{
|
||||
$log = new NotificationLog();
|
||||
$log->type = $type;
|
||||
@@ -156,7 +156,7 @@ class Helpers
|
||||
* @param int|null $goodsId 商品ID
|
||||
* @param int|null $orderId 订单ID
|
||||
*/
|
||||
public static function addCouponLog(string $description, int $couponId, ?int $goodsId = null, ?int $orderId = null): bool
|
||||
public static function addCouponLog(string $description, int $couponId, int $goodsId = null, int $orderId = null): bool
|
||||
{
|
||||
$log = new CouponLog();
|
||||
$log->coupon_id = $couponId;
|
||||
@@ -177,7 +177,7 @@ class Helpers
|
||||
* @param int $amount 发生金额
|
||||
* @param string|null $description 描述
|
||||
*/
|
||||
public static function addUserCreditLog(int $userId, ?int $orderId, int $before, int $after, int $amount, ?string $description = null): bool
|
||||
public static function addUserCreditLog(int $userId, ?int $orderId, int $before, int $after, int $amount, string $description = null): bool
|
||||
{
|
||||
$log = new UserCreditLog();
|
||||
$log->user_id = $userId;
|
||||
@@ -200,7 +200,7 @@ class Helpers
|
||||
* @param string|null $description 描述
|
||||
* @param int|null $orderId 订单ID
|
||||
*/
|
||||
public static function addUserTrafficModifyLog(int $userId, int $before, int $after, ?string $description = null, ?int $orderId = null): bool
|
||||
public static function addUserTrafficModifyLog(int $userId, int $before, int $after, string $description = null, int $orderId = null): bool
|
||||
{
|
||||
$log = new UserDataModifyLog();
|
||||
$log->user_id = $userId;
|
||||
@@ -222,7 +222,7 @@ class Helpers
|
||||
* @param int $status 状态
|
||||
* @param string|null $error 报错
|
||||
*/
|
||||
public static function addMarketing(string $receiver, int $type, string $title, string $content, int $status = 1, ?string $error = null): bool
|
||||
public static function addMarketing(string $receiver, int $type, string $title, string $content, int $status = 1, string $error = null): bool
|
||||
{
|
||||
$marketing = new Marketing();
|
||||
$marketing->type = $type;
|
||||
|
||||
@@ -27,7 +27,7 @@ if (! function_exists('base64url_decode')) {
|
||||
|
||||
// 根据流量值自动转换单位输出
|
||||
if (! function_exists('formatBytes')) {
|
||||
function formatBytes(int $bytes, ?string $base = null, int $precision = 2): string
|
||||
function formatBytes(int $bytes, string $base = null, int $precision = 2): string
|
||||
{
|
||||
$units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
|
||||
$bytes = max($bytes, 0);
|
||||
@@ -70,7 +70,7 @@ if (! function_exists('formatTime')) {
|
||||
|
||||
// 获取系统设置
|
||||
if (! function_exists('sysConfig')) {
|
||||
function sysConfig(?string $key = null, ?string $default = null): array|string|null
|
||||
function sysConfig(string $key = null, string $default = null): array|string|null
|
||||
{
|
||||
return $key ? config("settings.$key", $default) : config('settings');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user