格式化与整理代码
This commit is contained in:
parent
089f97ff79
commit
f974d3f0f3
@ -112,6 +112,18 @@ private function handleIncomingRequest(array $message): string
|
||||
return $msg;
|
||||
}
|
||||
|
||||
private function appendUser(array $message): string
|
||||
{
|
||||
$msg = '';
|
||||
if ($message['user']) {
|
||||
$msg .= "{$message['user']['name']}#{$message['user']['id']} ";
|
||||
} else {
|
||||
$msg .= 'Guest ';
|
||||
}
|
||||
|
||||
return $msg;
|
||||
}
|
||||
|
||||
private function handleOutgoingRequest(array $message): string
|
||||
{
|
||||
$msg = $this->appendUser($message);
|
||||
@ -132,16 +144,4 @@ private function handleWeightUpdated(array $message): string
|
||||
|
||||
return $msg;
|
||||
}
|
||||
|
||||
private function appendUser(array $message): string
|
||||
{
|
||||
$msg = '';
|
||||
if ($message['user']) {
|
||||
$msg .= "{$message['user']['name']}#{$message['user']['id']} ";
|
||||
} else {
|
||||
$msg .= 'Guest ';
|
||||
}
|
||||
|
||||
return $msg;
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@
|
||||
use App\Support\ClusterSupport;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
@ -45,7 +46,7 @@ public function update(Request $request, string $node): JsonResponse
|
||||
return $this->success('Updated');
|
||||
}
|
||||
|
||||
public function event(Request $request): \Illuminate\Http\RedirectResponse
|
||||
public function event(Request $request): RedirectResponse
|
||||
{
|
||||
$request->validate([
|
||||
'restart' => 'nullable|string|max:10|in:web,queue',
|
||||
|
@ -23,15 +23,6 @@ public function verify(Request $request): JsonResponse
|
||||
: $this->failed();
|
||||
}
|
||||
|
||||
public function process(Request $request): View
|
||||
{
|
||||
Log::debug('实名认证回调', $request->all());
|
||||
|
||||
return $this->validateOrSave($request)
|
||||
? view('real_name.success')
|
||||
: view('real_name.failed');
|
||||
}
|
||||
|
||||
public function validateOrSave(Request $request): bool
|
||||
{
|
||||
Log::debug('实名认证回调', $request->all());
|
||||
@ -64,4 +55,13 @@ public function validateOrSave(Request $request): bool
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function process(Request $request): View
|
||||
{
|
||||
Log::debug('实名认证回调', $request->all());
|
||||
|
||||
return $this->validateOrSave($request)
|
||||
? view('real_name.success')
|
||||
: view('real_name.failed');
|
||||
}
|
||||
}
|
||||
|
@ -26,26 +26,24 @@ public function renew(Host $host): RedirectResponse
|
||||
$price = $host->getRenewPrice();
|
||||
|
||||
if ($price > auth()->user()->balance) {
|
||||
return back()->with('error', '余额不足,续费需要:' . $price . ' 元,您还需要充值:' . ($price - auth()->user()->balance) . ' 元');
|
||||
return back()->with('error', '余额不足,续费需要:'.$price.' 元,您还需要充值:'.($price - auth()->user()->balance).' 元');
|
||||
}
|
||||
|
||||
if (!$host->isCycle()) {
|
||||
if (! $host->isCycle()) {
|
||||
return back()->with('error', '该主机不是周期性付费,无法续费。');
|
||||
}
|
||||
|
||||
if ($host->renew()) {
|
||||
return back()->with('success', '续费成功,新的到期时间为:' . $host->next_due_at . '。');
|
||||
return back()->with('success', '续费成功,新的到期时间为:'.$host->next_due_at.'。');
|
||||
}
|
||||
|
||||
|
||||
return back()->with('error', '续费失败,请检查是否有足够的余额。');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param Host $host
|
||||
*
|
||||
* @param Host $host
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public function destroy(Host $host): RedirectResponse
|
||||
|
@ -36,7 +36,7 @@ public function __construct($minute, Host $host = null)
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
if (!$this->host) {
|
||||
if (! $this->host) {
|
||||
$host = new Host();
|
||||
|
||||
if (app()->environment() != 'local') {
|
||||
|
@ -32,7 +32,7 @@ public function __construct(?Host $host = null)
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
if (!$this->host) {
|
||||
if (! $this->host) {
|
||||
(new Host)->where('next_due_at', '<', now())
|
||||
->where('status', '!=', 'suspended')
|
||||
->chunk(100, function ($hosts) {
|
||||
|
@ -110,20 +110,15 @@ public function isStopped(): bool
|
||||
return $this->status === 'stopped';
|
||||
}
|
||||
|
||||
public function isSuspended(): bool
|
||||
{
|
||||
return $this->status === 'suspended';
|
||||
}
|
||||
|
||||
public function renew(): bool
|
||||
{
|
||||
if (!$this->isCycle()) {
|
||||
if (! $this->isCycle()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$price = $this->getRenewPrice();
|
||||
|
||||
$description = '续费 ' . $this->name . ' 到 ' . $this->next_due_at . ' 价格:' . $price . ' 元。';
|
||||
$description = '续费 '.$this->name.' 到 '.$this->next_due_at.' 价格:'.$price.' 元。';
|
||||
|
||||
try {
|
||||
$this->user->reduce($price, $description, true, [
|
||||
@ -180,7 +175,7 @@ public function addLog(string $amount = '0'): bool
|
||||
$current_month = now()->month;
|
||||
$current_year = now()->year;
|
||||
|
||||
$cache_key = 'module_earning_' . $this->module_id;
|
||||
$cache_key = 'module_earning_'.$this->module_id;
|
||||
|
||||
// 应支付的提成
|
||||
$commission = config('settings.billing.commission');
|
||||
@ -195,7 +190,7 @@ public function addLog(string $amount = '0'): bool
|
||||
|
||||
$earnings = Cache::get($cache_key, []);
|
||||
|
||||
if (!isset($earnings[$current_year])) {
|
||||
if (! isset($earnings[$current_year])) {
|
||||
$earnings[$current_year] = [];
|
||||
}
|
||||
|
||||
@ -240,6 +235,20 @@ public function getNewDueDate(): string
|
||||
};
|
||||
}
|
||||
|
||||
public function isSuspended(): bool
|
||||
{
|
||||
return $this->status === 'suspended';
|
||||
}
|
||||
|
||||
public function run(): bool
|
||||
{
|
||||
$this->update([
|
||||
'status' => 'running',
|
||||
]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function isOverdue(): bool
|
||||
{
|
||||
return now()->gt($this->next_due_at);
|
||||
@ -248,7 +257,7 @@ public function isOverdue(): bool
|
||||
public function safeDelete(): bool
|
||||
{
|
||||
// 如果创建时间大于大于 1 小时
|
||||
if (!$this->isCycle() && $this->created_at->diffInHours(now()) > 1) {
|
||||
if (! $this->isCycle() && $this->created_at->diffInHours(now()) > 1) {
|
||||
// 如果当前时间比扣费时间小,则说明没有扣费。执行扣费。
|
||||
if (now()->minute < $this->minute_at) {
|
||||
$this->cost();
|
||||
@ -275,7 +284,7 @@ public function cost(string $amount = null, $auto = true, $description = null):
|
||||
|
||||
$real_price = $amount ?? $this->price;
|
||||
|
||||
if (!$amount) {
|
||||
if (! $amount) {
|
||||
if ($this->managed_price) {
|
||||
$real_price = $this->managed_price;
|
||||
}
|
||||
@ -286,7 +295,7 @@ public function cost(string $amount = null, $auto = true, $description = null):
|
||||
if ($user_group->discount !== 100 && $user_group->discount !== null) {
|
||||
$real_price = $user_group->getCostPrice($real_price);
|
||||
|
||||
$append_description = ' (折扣 ' . $user_group->discount . '%)';
|
||||
$append_description = ' (折扣 '.$user_group->discount.'%)';
|
||||
}
|
||||
}
|
||||
|
||||
@ -300,7 +309,7 @@ public function cost(string $amount = null, $auto = true, $description = null):
|
||||
}
|
||||
|
||||
if ($real_price == 0) {
|
||||
echo '价格为 0,不扣费' . PHP_EOL;
|
||||
echo '价格为 0,不扣费'.PHP_EOL;
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -314,7 +323,7 @@ public function cost(string $amount = null, $auto = true, $description = null):
|
||||
|
||||
$month = now()->month;
|
||||
|
||||
$month_cache_key = 'user_' . $this->user_id . '_month_' . $month . '_hosts_balances';
|
||||
$month_cache_key = 'user_'.$this->user_id.'_month_'.$month.'_hosts_balances';
|
||||
$hosts_balances = Cache::get($month_cache_key, []);
|
||||
|
||||
// 统计 Host 消耗的 Balance
|
||||
@ -328,7 +337,7 @@ public function cost(string $amount = null, $auto = true, $description = null):
|
||||
|
||||
Cache::put($month_cache_key, $hosts_balances, 604800);
|
||||
|
||||
if (!$description) {
|
||||
if (! $description) {
|
||||
$description = '模块发起的扣费。';
|
||||
}
|
||||
|
||||
@ -373,13 +382,4 @@ public function suspend(): bool
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function run(): bool
|
||||
{
|
||||
$this->update([
|
||||
'status' => 'running',
|
||||
]);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -163,17 +163,6 @@ public function startTransfer(self $to, string $amount, string|null $description
|
||||
return $this->balance;
|
||||
}
|
||||
|
||||
public function getCostPrice(string $price): string
|
||||
{
|
||||
$this->load('user_group');
|
||||
|
||||
if (! $this->user_group) {
|
||||
return $price;
|
||||
}
|
||||
|
||||
return $this->user_group->getCostPrice($price);
|
||||
}
|
||||
|
||||
/**
|
||||
* 扣除费用
|
||||
*
|
||||
@ -269,6 +258,17 @@ public function charge(string|null $amount = '0', string $payment = 'console', s
|
||||
return $this->balance;
|
||||
}
|
||||
|
||||
public function getCostPrice(string $price): string
|
||||
{
|
||||
$this->load('user_group');
|
||||
|
||||
if (! $this->user_group) {
|
||||
return $price;
|
||||
}
|
||||
|
||||
return $this->user_group->getCostPrice($price);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户的身份证号
|
||||
*
|
||||
|
@ -79,9 +79,12 @@ public static function update($node_id, $data = []): void
|
||||
ClusterSupport::hset('nodes', $node_id, $node);
|
||||
}
|
||||
|
||||
public static function removeNode($node_id): void
|
||||
public static function hget($key, $hashKey, $default = []): string|array|null
|
||||
{
|
||||
self::hdel('nodes', $node_id);
|
||||
/** @noinspection PhpUndefinedMethodInspection */
|
||||
$value = Redis::hget(self::$prefix.$key, $hashKey);
|
||||
|
||||
return $value ?: $default;
|
||||
}
|
||||
|
||||
public static function hset($key, $value, $data = []): void
|
||||
@ -90,6 +93,11 @@ public static function hset($key, $value, $data = []): void
|
||||
Redis::hset(self::$prefix.$key, $value, json_encode($data));
|
||||
}
|
||||
|
||||
public static function removeNode($node_id): void
|
||||
{
|
||||
self::hdel('nodes', $node_id);
|
||||
}
|
||||
|
||||
public static function hdel($key, $hash_key): void
|
||||
{
|
||||
/** @noinspection PhpUndefinedMethodInspection */
|
||||
@ -133,13 +141,14 @@ public static function get($key, $default = null): string|array|null
|
||||
return Redis::get(self::$prefix.$key, $default);
|
||||
}
|
||||
|
||||
// forever
|
||||
|
||||
public static function forget($key): void
|
||||
{
|
||||
/** @noinspection PhpUndefinedMethodInspection */
|
||||
Redis::forget(self::$prefix.$key);
|
||||
}
|
||||
|
||||
// forever
|
||||
public static function forever($key, $value): void
|
||||
{
|
||||
self::set($key, $value, -1);
|
||||
@ -151,14 +160,6 @@ public static function set($key, $value, $ttl = null): void
|
||||
Redis::set(self::$prefix.$key, $value, $ttl);
|
||||
}
|
||||
|
||||
public static function hget($key, $hashKey, $default = []): string|array|null
|
||||
{
|
||||
/** @noinspection PhpUndefinedMethodInspection */
|
||||
$value = Redis::hget(self::$prefix.$key, $hashKey);
|
||||
|
||||
return $value ?: $default;
|
||||
}
|
||||
|
||||
public static function nodes($hide_ip = false, $type = 'all'): array
|
||||
{
|
||||
$nodes = self::hgetAll('nodes');
|
||||
|
Loading…
Reference in New Issue
Block a user