CompanyAccount::logs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SimpleCMS\Company\Models;
4
5
use Carbon\Carbon;
6
use Laravel\Sanctum\HasApiTokens;
7
use Illuminate\Database\Eloquent\Collection;
8
use SimpleCMS\Framework\Contracts\SimpleMedia;
9
use SimpleCMS\Framework\Traits\MediaAttributeTrait;
10
use SimpleCMS\Framework\Traits\PrimaryKeyUuidTrait;
11
use Illuminate\Database\Eloquent\Factories\HasFactory;
12
use Spatie\MediaLibrary\MediaCollections\Models\Media;
13
use Illuminate\Foundation\Auth\User as Authenticatable;
14
15
/**
16
 * Company Model
17
 * @author Dennis Lui <[email protected]>
18
 *
19
 * @property string $id 主键
20
 * @property string $company_id 企业ID
21
 * @property string $uid 暴露UID
22
 * @property string $account 登录账号
23
 * @property string $name 名称
24
 * @property ?array $roles 权限
25
 * @property string $password 密码
26
 * @property bool $is_valid 是否可用
27
 * @property bool $is_founder 是否管理员
28
 * @property ?Carbon $last_login 最后登录时间
29
 * @property ?string $last_ip 最后登录IP
30
 * @property ?string $register_ip 注册IP
31
 * @property ?string $register_finger 设备指纹
32
 * @property int $failed_count 失败次数
33
 * @property-read ?Carbon $created_at 创建时间
34
 * @property-read ?Carbon $updated_at 更新时间
35
 * @property-read ?Collection<CompanySafe> $safes 安全信息
36
 * @property-read ?Collection<CompanyLog> $logs 请求日志
37
 * @property-read ?Company $company 企业
38
 * @property-read ?Collection<Media> $media 附件
39
 * @property-read ?array<string,string> $avatar 头像
40
 */
41
class CompanyAccount extends Authenticatable implements SimpleMedia
42
{
43
    use HasFactory, PrimaryKeyUuidTrait, MediaAttributeTrait, HasApiTokens;
44
    /**
45
     * Avatar
46
     */
47
    const MEDIA_AVATAR = 'avatar';
48
49
    const HAS_ONE_MEDIA = ['avatar'];
50
51
    /**
52
     * 可输入字段
53
     */
54
    protected $fillable = [
55
        'id',
56
        'company_id',
57
        'uid',
58
        'account',
59
        'name',
60
        'roles',
61
        'password',
62
        'is_valid',
63
        'is_founder',
64
        'last_login',
65
        'last_ip',
66
        'register_ip',
67
        'register_finger',
68
        'failed_count'
69
    ];
70
    /**
71
     * 显示字段类型
72
     */
73
    public $casts = [
74
        'roles' => 'array',
75
        'is_valid' => 'boolean',
76
        'is_founder' => 'boolean',
77
        'success_at' => 'datetime',
78
        'reject_at' => 'datetime',
79
        'created_at' => 'datetime',
80
        'updated_at' => 'datetime'
81
    ];
82
83
    /**
84
     * 追加字段
85
     */
86
    public $appends = ['avatar'];
87
88
    /**
89
     * 隐藏关系
90
     */
91
    public $hidden = ['media'];
92
93
    public function getAvatarAttribute()
94
    {
95
        if (!$media = $this->getFirstMedia(self::MEDIA_AVATAR))
96
            return [];
97
        return [
98
            'name' => $media->file_name,
99
            'url' => $media->original_url,
100
            'uuid' => $media->uuid
101
        ];
102
    }
103
104
    public function company()
105
    {
106
        return $this->belongsTo(Company::class);
107
    }
108
109
    public function logs()
110
    {
111
        return $this->hasMany(CompanyLog::class);
112
    }
113
114
    public function safes()
115
    {
116
        return $this->hasMany(CompanySafe::class);
117
    }
118
}
119