| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- namespace App\Module\Admin\Models;
- use App\Module\Admin\Enums\ADMIN_ACTION_TYPE;
- use UCore\ModelCore;
- /**
- * 管理员日志模型
- *
- * @property int $id
- * @property int|null $admin_id 管理员ID
- * @property string $admin_name 管理员名称
- * @property string $action_type 操作类型
- * @property string $description 操作描述
- * @property string|null $data 操作数据(JSON)
- * @property string $ip_address IP地址
- * @property string|null $user_agent 用户代理
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- */
- class AdminLog extends ModelCore
- {
- /**
- * 数据表名
- *
- * @var string
- */
- protected $table = 'admin_logs';
- /**
- * 可批量赋值的属性
- *
- * @var array
- */
- protected $fillable = [
- 'admin_id',
- 'admin_name',
- 'action_type',
- 'description',
- 'data',
- 'ip_address',
- 'user_agent',
- ];
- /**
- * 属性类型转换
- *
- * @var array
- */
- protected $casts = [
- 'admin_id' => 'integer',
- 'data' => 'array',
- 'created_at' => 'datetime',
- 'updated_at' => 'datetime',
- ];
- /**
- * 获取操作类型枚举
- *
- * @return ADMIN_ACTION_TYPE|null
- */
- public function getActionTypeEnumAttribute(): ?ADMIN_ACTION_TYPE
- {
- try {
- return ADMIN_ACTION_TYPE::from($this->action_type);
- } catch (\ValueError $e) {
- return null;
- }
- }
- /**
- * 获取操作类型标签
- *
- * @return string
- */
- public function getActionTypeLabelAttribute(): string
- {
- $enum = $this->getActionTypeEnumAttribute();
- return $enum ? $enum->getLabel() : $this->action_type;
- }
- /**
- * 获取操作类型颜色
- *
- * @return string
- */
- public function getActionTypeColorAttribute(): string
- {
- $enum = $this->getActionTypeEnumAttribute();
- return $enum ? $enum->getColor() : 'secondary';
- }
- /**
- * 获取操作类型图标
- *
- * @return string
- */
- public function getActionTypeIconAttribute(): string
- {
- $enum = $this->getActionTypeEnumAttribute();
- return $enum ? $enum->getIcon() : 'fa-question';
- }
- /**
- * 判断是否为危险操作
- *
- * @return bool
- */
- public function getIsDangerousAttribute(): bool
- {
- $enum = $this->getActionTypeEnumAttribute();
- return $enum ? $enum->isDangerous() : false;
- }
- /**
- * 获取格式化的数据
- *
- * @return string
- */
- public function getFormattedDataAttribute(): string
- {
- if (empty($this->data)) {
- return '';
- }
- if (is_array($this->data)) {
- return json_encode($this->data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
- }
- return (string) $this->data;
- }
- /**
- * 获取简短的用户代理
- *
- * @return string
- */
- public function getShortUserAgentAttribute(): string
- {
- if (empty($this->user_agent)) {
- return '';
- }
- // 提取浏览器信息
- if (preg_match('/Chrome\/[\d.]+/', $this->user_agent, $matches)) {
- return $matches[0];
- }
- if (preg_match('/Firefox\/[\d.]+/', $this->user_agent, $matches)) {
- return $matches[0];
- }
- if (preg_match('/Safari\/[\d.]+/', $this->user_agent, $matches)) {
- return $matches[0];
- }
- if (preg_match('/Edge\/[\d.]+/', $this->user_agent, $matches)) {
- return $matches[0];
- }
- // 如果没有匹配到,返回前50个字符
- return mb_substr($this->user_agent, 0, 50) . '...';
- }
- /**
- * 按操作类型查询
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @param string|ADMIN_ACTION_TYPE $actionType
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeByActionType($query, $actionType)
- {
- if ($actionType instanceof ADMIN_ACTION_TYPE) {
- $actionType = $actionType->value;
- }
- return $query->where('action_type', $actionType);
- }
- /**
- * 按管理员查询
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @param int $adminId
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeByAdmin($query, int $adminId)
- {
- return $query->where('admin_id', $adminId);
- }
- /**
- * 按IP地址查询
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @param string $ipAddress
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeByIpAddress($query, string $ipAddress)
- {
- return $query->where('ip_address', $ipAddress);
- }
- /**
- * 按时间范围查询
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @param string $startDate
- * @param string $endDate
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeByDateRange($query, string $startDate, string $endDate)
- {
- return $query->whereBetween('created_at', [$startDate, $endDate]);
- }
- /**
- * 查询危险操作
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeDangerousActions($query)
- {
- $dangerousTypes = [];
- foreach (ADMIN_ACTION_TYPE::cases() as $type) {
- if ($type->isDangerous()) {
- $dangerousTypes[] = $type->value;
- }
- }
- return $query->whereIn('action_type', $dangerousTypes);
- }
- /**
- * 最近的操作
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @param int $hours
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeRecent($query, int $hours = 24)
- {
- return $query->where('created_at', '>=', now()->subHours($hours));
- }
- }
|