增加 状态判断函数

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';
}
public function isPending(): bool
{
return $this->status === 'pending';
}
public function isUnavailable(): bool
{
return $this->status === 'unavailable';
}
public function renew(): bool
{
if (! $this->isCycle()) {

View File

@ -48,14 +48,14 @@ class Module extends Authenticatable
// post, get, patch, delete 等请求
public function remote($func, $requests): array
{
$response = $this->http()->post('functions/'.$func, $requests);
$response = $this->http()->post('functions/' . $func, $requests);
return $this->getResponse($response);
}
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) {
$http->asMultipart();
@ -68,7 +68,7 @@ public function http($files = []): PendingRequest
if ($this->ip_port) {
// 如果设置了 ip_port 则使用 ip_port
$http->baseUrl($this->ip_port.'/remote');
$http->baseUrl($this->ip_port . '/remote');
// 添加 Host 头
$http->withHeaders([
@ -111,6 +111,7 @@ private function getResponse(Response $response): array
* @param $path
* @param $requests
* @param array $files
*
* @return array
*/
public function request($method, $path, $requests, array $files = []): array
@ -118,7 +119,7 @@ public function request($method, $path, $requests, array $files = []): array
try {
return $this->baseRequest($method, "functions/$path", $requests, $files);
} /** @noinspection PhpRedundantCatchClauseInspection */ catch (ConnectException|ConnectionException $e) {
Log::error('在执行 call '.$method.' '.$path.' 时发生错误: '.$e->getMessage());
Log::error('在执行 call ' . $method . ' ' . $path . ' 时发生错误: ' . $e->getMessage());
return [
'body' => null,
@ -194,7 +195,7 @@ public function check(): bool
#[ArrayShape(['transactions' => 'array'])]
public function calculate(): array
{
$cache_key = 'module_earning_'.$this->id;
$cache_key = 'module_earning_' . $this->id;
return Cache::get($cache_key, []);
}
@ -206,6 +207,7 @@ public function calculate(): array
* @param string|null $description
* @param bool $fail
* @param array $options
*
* @return 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;
}
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();
if ($this->balance < $amount) {
@ -253,6 +255,7 @@ public function reduce(string|null $amount = '0', string|null $description = '
* @param string $payment
* @param string|null $description
* @param array $options
*
* @return 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;
}
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->balance = bcadd($this->balance, $amount, 4);
@ -297,4 +300,19 @@ public function whereHasBalance(string $amount = '0'): self|Builder|CachedBuilde
{
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';
}
}