2026-02-26 12:02:00 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
2026-02-26 13:35:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 文件功能:主用户模型
|
|
|
|
|
|
*
|
|
|
|
|
|
* 对应原 ASP 文件:user 表
|
|
|
|
|
|
*
|
|
|
|
|
|
* @author ChatRoom Laravel
|
|
|
|
|
|
*
|
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2026-02-26 12:02:00 +08:00
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
2026-02-26 21:10:34 +08:00
|
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2026-02-26 12:02:00 +08:00
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2026-02-26 21:30:07 +08:00
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2026-02-28 23:44:38 +08:00
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
2026-02-26 12:02:00 +08:00
|
|
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
|
|
|
|
use Illuminate\Notifications\Notifiable;
|
|
|
|
|
|
|
|
|
|
|
|
class User extends Authenticatable
|
|
|
|
|
|
{
|
|
|
|
|
|
use HasFactory, Notifiable;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
|
*
|
2026-02-26 13:35:38 +08:00
|
|
|
|
* @var array<int, string>
|
2026-02-26 12:02:00 +08:00
|
|
|
|
*/
|
|
|
|
|
|
protected $fillable = [
|
2026-02-26 13:35:38 +08:00
|
|
|
|
'username',
|
2026-02-26 12:02:00 +08:00
|
|
|
|
'password',
|
2026-02-26 13:35:38 +08:00
|
|
|
|
'email',
|
|
|
|
|
|
'sex',
|
2026-02-26 22:50:35 +08:00
|
|
|
|
'sign',
|
2026-02-26 13:35:38 +08:00
|
|
|
|
'user_level',
|
2026-03-12 09:33:38 +08:00
|
|
|
|
'inviter_id',
|
2026-02-26 13:35:38 +08:00
|
|
|
|
'room_id',
|
|
|
|
|
|
'first_ip',
|
2026-03-09 11:53:58 +08:00
|
|
|
|
'previous_ip',
|
2026-02-26 13:35:38 +08:00
|
|
|
|
'last_ip',
|
2026-02-26 21:10:34 +08:00
|
|
|
|
'usersf',
|
2026-02-26 21:30:07 +08:00
|
|
|
|
'vip_level_id',
|
|
|
|
|
|
'hy_time',
|
2026-02-26 22:50:35 +08:00
|
|
|
|
'question',
|
|
|
|
|
|
'answer',
|
2026-02-27 17:21:33 +08:00
|
|
|
|
'has_received_new_gift',
|
2026-03-01 00:10:44 +08:00
|
|
|
|
'in_time', // 进房时间(用于勤务日志 login_at 基准)
|
|
|
|
|
|
'out_time', // 离房时间
|
2026-02-26 12:02:00 +08:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* The attributes that should be hidden for serialization.
|
|
|
|
|
|
*
|
2026-02-26 13:35:38 +08:00
|
|
|
|
* @var array<int, string>
|
2026-02-26 12:02:00 +08:00
|
|
|
|
*/
|
|
|
|
|
|
protected $hidden = [
|
|
|
|
|
|
'password',
|
|
|
|
|
|
'remember_token',
|
2026-02-26 13:35:38 +08:00
|
|
|
|
'temppass',
|
|
|
|
|
|
'ppass',
|
|
|
|
|
|
'userpassword',
|
2026-02-26 12:02:00 +08:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Get the attributes that should be cast.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return array<string, string>
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function casts(): array
|
|
|
|
|
|
{
|
|
|
|
|
|
return [
|
|
|
|
|
|
'email_verified_at' => 'datetime',
|
|
|
|
|
|
'password' => 'hashed',
|
2026-02-26 13:35:38 +08:00
|
|
|
|
'log_time' => 'datetime',
|
|
|
|
|
|
'in_time' => 'datetime',
|
|
|
|
|
|
'out_time' => 'datetime',
|
|
|
|
|
|
'hy_time' => 'datetime',
|
|
|
|
|
|
'xr_time' => 'datetime',
|
|
|
|
|
|
'yx_time' => 'datetime',
|
|
|
|
|
|
'sj' => 'datetime',
|
|
|
|
|
|
'q3_time' => 'datetime',
|
2026-02-27 17:21:33 +08:00
|
|
|
|
'has_received_new_gift' => 'boolean',
|
2026-02-26 12:02:00 +08:00
|
|
|
|
];
|
|
|
|
|
|
}
|
2026-02-26 21:10:34 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 头像文件名访问器
|
|
|
|
|
|
*
|
2026-02-26 23:27:35 +08:00
|
|
|
|
* 原 ASP 系统的头像文件名存储在 usersf 字段中(如 "75.gif"),
|
2026-03-12 15:26:54 +08:00
|
|
|
|
* 同时也支持用户自定义上传的头像,保存在 Laravel Storage 的 public 磁盘下。
|
|
|
|
|
|
* 此 accessor 将 headface 属性映射到 usersf 字段,如果包含 storage/ 则当作独立路径,自动转换旧版后缀小写。
|
2026-02-26 21:10:34 +08:00
|
|
|
|
*/
|
|
|
|
|
|
protected function headface(): Attribute
|
|
|
|
|
|
{
|
|
|
|
|
|
return Attribute::make(
|
2026-03-12 15:26:54 +08:00
|
|
|
|
get: function () {
|
|
|
|
|
|
$val = $this->usersf ?: '1.gif';
|
|
|
|
|
|
if (str_starts_with($val, 'storage/')) {
|
|
|
|
|
|
return $val;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 仅对非 storage 下的旧头像做小写处理,兼容旧库数据
|
|
|
|
|
|
return strtolower($val);
|
|
|
|
|
|
},
|
|
|
|
|
|
set: function ($value) {
|
|
|
|
|
|
if (str_starts_with($value, 'storage/')) {
|
|
|
|
|
|
return tap($value, fn () => $this->attributes['usersf'] = $value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return tap(strtolower($value), fn () => $this->attributes['usersf'] = strtolower($value));
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取带前缀的完整头像 URL
|
|
|
|
|
|
* 避免前端多处硬编码 '/images/headface/'
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function headfaceUrl(): Attribute
|
|
|
|
|
|
{
|
|
|
|
|
|
return Attribute::make(
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
$hf = $this->headface;
|
|
|
|
|
|
if (str_starts_with((string) $hf, 'storage/')) {
|
|
|
|
|
|
return '/'.$hf;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return '/images/headface/'.$hf;
|
|
|
|
|
|
}
|
2026-02-26 21:10:34 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
2026-02-26 21:30:07 +08:00
|
|
|
|
|
2026-03-12 15:26:54 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 如果当前头像是自定义上传的图片,则从本地存储中删除此文件
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function deleteCustomAvatar(): void
|
|
|
|
|
|
{
|
|
|
|
|
|
$hf = (string) $this->usersf;
|
|
|
|
|
|
if (str_starts_with($hf, 'storage/')) {
|
|
|
|
|
|
$path = substr($hf, 8); // 去除 'storage/' 前缀
|
|
|
|
|
|
\Illuminate\Support\Facades\Storage::disk('public')->delete($path);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-26 21:30:07 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 关联:用户所属的 VIP 会员等级
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function vipLevel(): BelongsTo
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->belongsTo(VipLevel::class, 'vip_level_id');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 判断用户是否为有效 VIP(有等级且未过期)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function isVip(): bool
|
|
|
|
|
|
{
|
|
|
|
|
|
if (! $this->vip_level_id) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// hy_time 为 null 表示永久会员
|
|
|
|
|
|
if (! $this->hy_time) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $this->hy_time->isFuture();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取 VIP 会员名称(无效则返回空字符串)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function vipName(): string
|
|
|
|
|
|
{
|
|
|
|
|
|
if (! $this->isVip()) {
|
|
|
|
|
|
return '';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $this->vipLevel?->name ?? '';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取 VIP 会员图标(无效则返回空字符串)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function vipIcon(): string
|
|
|
|
|
|
{
|
|
|
|
|
|
if (! $this->isVip()) {
|
|
|
|
|
|
return '';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $this->vipLevel?->icon ?? '';
|
|
|
|
|
|
}
|
2026-02-28 23:44:38 +08:00
|
|
|
|
|
|
|
|
|
|
// ── 职务相关关联 ──────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 全部猎务履历(包括历史记录)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function positions(): HasMany
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->hasMany(UserPosition::class)->with(['position.department'])->orderByDesc('appointed_at');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 当前在职职务记录(HasOne,最多一条)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function activePosition(): HasOne
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->hasOne(UserPosition::class)->where('is_active', true)->with(['position.department']);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 该用户在职期间的权限操作日志
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function authorityLogs(): HasMany
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->hasMany(PositionAuthorityLog::class)->orderByDesc('created_at');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 判断用户是否有当前在职职务
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function hasActivePosition(): bool
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->activePosition()->exists();
|
|
|
|
|
|
}
|
2026-03-12 09:33:38 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 关联:邀请当前用户的人
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function inviter(): BelongsTo
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->belongsTo(self::class, 'inviter_id');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 关联:当前用户邀请的所有人
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function invitees(): HasMany
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->hasMany(self::class, 'inviter_id');
|
|
|
|
|
|
}
|
2026-02-26 12:02:00 +08:00
|
|
|
|
}
|