增加 状态判断函数

This commit is contained in:
iVampireSP.com 2023-02-13 15:26:33 +08:00
parent 803cb019b2
commit ac6112ac20
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
2 changed files with 47 additions and 19 deletions

View File

@ -110,6 +110,16 @@ public function isStopped(): bool
return $this->status === 'stopped'; return $this->status === 'stopped';
} }
public function isPending(): bool
{
return $this->status === 'pending';
}
public function isUnavailable(): bool
{
return $this->status === 'unavailable';
}
public function renew(): bool public function renew(): bool
{ {
if (! $this->isCycle()) { if (! $this->isCycle()) {

View File

@ -48,14 +48,14 @@ class Module extends Authenticatable
// post, get, patch, delete 等请求 // post, get, patch, delete 等请求
public function remote($func, $requests): array public function remote($func, $requests): array
{ {
$response = $this->http()->post('functions/'.$func, $requests); $response = $this->http()->post('functions/' . $func, $requests);
return $this->getResponse($response); return $this->getResponse($response);
} }
public function http($files = []): PendingRequest public function http($files = []): PendingRequest
{ {
$http = Http::module($this->api_token, $this->url.'/remote'); $http = Http::module($this->api_token, $this->url . '/remote');
if ($files) { if ($files) {
$http->asMultipart(); $http->asMultipart();
@ -68,7 +68,7 @@ public function http($files = []): PendingRequest
if ($this->ip_port) { if ($this->ip_port) {
// 如果设置了 ip_port 则使用 ip_port // 如果设置了 ip_port 则使用 ip_port
$http->baseUrl($this->ip_port.'/remote'); $http->baseUrl($this->ip_port . '/remote');
// 添加 Host 头 // 添加 Host 头
$http->withHeaders([ $http->withHeaders([
@ -107,10 +107,11 @@ private function getResponse(Response $response): array
} }
/** /**
* @param $method * @param $method
* @param $path * @param $path
* @param $requests * @param $requests
* @param array $files * @param array $files
*
* @return array * @return array
*/ */
public function request($method, $path, $requests, array $files = []): array public function request($method, $path, $requests, array $files = []): array
@ -118,7 +119,7 @@ public function request($method, $path, $requests, array $files = []): array
try { try {
return $this->baseRequest($method, "functions/$path", $requests, $files); return $this->baseRequest($method, "functions/$path", $requests, $files);
} /** @noinspection PhpRedundantCatchClauseInspection */ catch (ConnectException|ConnectionException $e) { } /** @noinspection PhpRedundantCatchClauseInspection */ catch (ConnectException|ConnectionException $e) {
Log::error('在执行 call '.$method.' '.$path.' 时发生错误: '.$e->getMessage()); Log::error('在执行 call ' . $method . ' ' . $path . ' 时发生错误: ' . $e->getMessage());
return [ return [
'body' => null, 'body' => null,
@ -194,7 +195,7 @@ public function check(): bool
#[ArrayShape(['transactions' => 'array'])] #[ArrayShape(['transactions' => 'array'])]
public function calculate(): array public function calculate(): array
{ {
$cache_key = 'module_earning_'.$this->id; $cache_key = 'module_earning_' . $this->id;
return Cache::get($cache_key, []); return Cache::get($cache_key, []);
} }
@ -202,10 +203,11 @@ public function calculate(): array
/** /**
* 扣除费用 * 扣除费用
* *
* @param string|null $amount * @param string|null $amount
* @param string|null $description * @param string|null $description
* @param bool $fail * @param bool $fail
* @param array $options * @param array $options
*
* @return string * @return string
*/ */
public function reduce(string|null $amount = '0', string|null $description = '消费', bool $fail = false, array $options = []): string public function reduce(string|null $amount = '0', string|null $description = '消费', bool $fail = false, array $options = []): string
@ -214,7 +216,7 @@ public function reduce(string|null $amount = '0', string|null $description = '
return $this->balance; return $this->balance;
} }
Cache::lock('module_balance_'.$this->id, 10)->block(10, function () use ($amount, $fail, $description, $options) { Cache::lock('module_balance_' . $this->id, 10)->block(10, function () use ($amount, $fail, $description, $options) {
$this->refresh(); $this->refresh();
if ($this->balance < $amount) { if ($this->balance < $amount) {
@ -249,10 +251,11 @@ public function reduce(string|null $amount = '0', string|null $description = '
/** /**
* 增加余额 * 增加余额
* *
* @param string|null $amount * @param string|null $amount
* @param string $payment * @param string $payment
* @param string|null $description * @param string|null $description
* @param array $options * @param array $options
*
* @return string * @return string
*/ */
public function charge(string|null $amount = '0', string $payment = 'console', string|null $description = '充值', array $options = []): string public function charge(string|null $amount = '0', string $payment = 'console', string|null $description = '充值', array $options = []): string
@ -261,7 +264,7 @@ public function charge(string|null $amount = '0', string $payment = 'console', s
return $this->balance; return $this->balance;
} }
Cache::lock('module_balance_'.$this->id, 10)->block(10, function () use ($amount, $description, $payment, $options) { Cache::lock('module_balance_' . $this->id, 10)->block(10, function () use ($amount, $description, $payment, $options) {
$this->refresh(); $this->refresh();
$this->balance = bcadd($this->balance, $amount, 4); $this->balance = bcadd($this->balance, $amount, 4);
@ -297,4 +300,19 @@ public function whereHasBalance(string $amount = '0'): self|Builder|CachedBuilde
{ {
return $this->where('balance', '>=', $amount); return $this->where('balance', '>=', $amount);
} }
public function isUp(): bool
{
return $this->status === 'up';
}
public function isDown(): bool
{
return $this->status === 'down';
}
public function isMaintenance(): bool
{
return $this->status === 'maintenance';
}
} }