mirror of
https://devops.lemonos.cn/lawson/FendxPHP.git
synced 2026-06-15 23:12:49 +08:00
325 lines
6.4 KiB
PHP
325 lines
6.4 KiB
PHP
|
|
<?php
|
|||
|
|
declare(strict_types=1);
|
|||
|
|
|
|||
|
|
namespace App\Dto;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 用户数据传输对象
|
|||
|
|
*/
|
|||
|
|
class UserDto extends BaseDto
|
|||
|
|
{
|
|||
|
|
#[Required]
|
|||
|
|
private ?int $id = null;
|
|||
|
|
|
|||
|
|
#[Required]
|
|||
|
|
#[Length(min: 2, max: 50)]
|
|||
|
|
private string $username = '';
|
|||
|
|
|
|||
|
|
#[Required]
|
|||
|
|
#[Length(min: 6, max: 100)]
|
|||
|
|
private string $email = '';
|
|||
|
|
|
|||
|
|
#[Required]
|
|||
|
|
#[Length(min: 6, max: 255)]
|
|||
|
|
private string $password = '';
|
|||
|
|
|
|||
|
|
#[Length(max: 100)]
|
|||
|
|
private string $nickname = '';
|
|||
|
|
|
|||
|
|
#[Length(max: 20)]
|
|||
|
|
private string $phone = '';
|
|||
|
|
|
|||
|
|
#[Length(max: 255)]
|
|||
|
|
private string $avatar = '';
|
|||
|
|
|
|||
|
|
private ?int $status = null;
|
|||
|
|
|
|||
|
|
private ?int $roleId = null;
|
|||
|
|
|
|||
|
|
private ?string $roleName = '';
|
|||
|
|
|
|||
|
|
private ?\DateTime $createdAt = null;
|
|||
|
|
|
|||
|
|
private ?\DateTime $updatedAt = null;
|
|||
|
|
|
|||
|
|
private ?\DateTime $lastLoginAt = null;
|
|||
|
|
|
|||
|
|
private array $permissions = [];
|
|||
|
|
|
|||
|
|
private array $roles = [];
|
|||
|
|
|
|||
|
|
public function getId(): ?int
|
|||
|
|
{
|
|||
|
|
return $this->id;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function setId(int $id): self
|
|||
|
|
{
|
|||
|
|
$this->id = $id;
|
|||
|
|
return $this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function getUsername(): string
|
|||
|
|
{
|
|||
|
|
return $this->username;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function setUsername(string $username): self
|
|||
|
|
{
|
|||
|
|
$this->username = $username;
|
|||
|
|
return $this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function getEmail(): string
|
|||
|
|
{
|
|||
|
|
return $this->email;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function setEmail(string $email): self
|
|||
|
|
{
|
|||
|
|
$this->email = $email;
|
|||
|
|
return $this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function getPassword(): string
|
|||
|
|
{
|
|||
|
|
return $this->password;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function setPassword(string $password): self
|
|||
|
|
{
|
|||
|
|
$this->password = $password;
|
|||
|
|
return $this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function getNickname(): string
|
|||
|
|
{
|
|||
|
|
return $this->nickname;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function setNickname(string $nickname): self
|
|||
|
|
{
|
|||
|
|
$this->nickname = $nickname;
|
|||
|
|
return $this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function getPhone(): string
|
|||
|
|
{
|
|||
|
|
return $this->phone;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function setPhone(string $phone): self
|
|||
|
|
{
|
|||
|
|
$this->phone = $phone;
|
|||
|
|
return $this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function getAvatar(): string
|
|||
|
|
{
|
|||
|
|
return $this->avatar;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function setAvatar(string $avatar): self
|
|||
|
|
{
|
|||
|
|
$this->avatar = $avatar;
|
|||
|
|
return $this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function getStatus(): ?int
|
|||
|
|
{
|
|||
|
|
return $this->status;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function setStatus(int $status): self
|
|||
|
|
{
|
|||
|
|
$this->status = $status;
|
|||
|
|
return $this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function getRoleId(): ?int
|
|||
|
|
{
|
|||
|
|
return $this->roleId;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function setRoleId(int $roleId): self
|
|||
|
|
{
|
|||
|
|
$this->roleId = $roleId;
|
|||
|
|
return $this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function getRoleName(): ?string
|
|||
|
|
{
|
|||
|
|
return $this->roleName;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function setRoleName(string $roleName): self
|
|||
|
|
{
|
|||
|
|
$this->roleName = $roleName;
|
|||
|
|
return $this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function getCreatedAt(): ?\DateTime
|
|||
|
|
{
|
|||
|
|
return $this->createdAt;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function setCreatedAt(\DateTime $createdAt): self
|
|||
|
|
{
|
|||
|
|
$this->createdAt = $createdAt;
|
|||
|
|
return $this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function getUpdatedAt(): ?\DateTime
|
|||
|
|
{
|
|||
|
|
return $this->updatedAt;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function setUpdatedAt(\DateTime $updatedAt): self
|
|||
|
|
{
|
|||
|
|
$this->updatedAt = $updatedAt;
|
|||
|
|
return $this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function getLastLoginAt(): ?\DateTime
|
|||
|
|
{
|
|||
|
|
return $this->lastLoginAt;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function setLastLoginAt(\DateTime $lastLoginAt): self
|
|||
|
|
{
|
|||
|
|
$this->lastLoginAt = $lastLoginAt;
|
|||
|
|
return $this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function getPermissions(): array
|
|||
|
|
{
|
|||
|
|
return $this->permissions;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function setPermissions(array $permissions): self
|
|||
|
|
{
|
|||
|
|
$this->permissions = $permissions;
|
|||
|
|
return $this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function getRoles(): array
|
|||
|
|
{
|
|||
|
|
return $this->roles;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function setRoles(array $roles): self
|
|||
|
|
{
|
|||
|
|
$this->roles = $roles;
|
|||
|
|
return $this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 添加权限
|
|||
|
|
*/
|
|||
|
|
public function addPermission(string $permission): self
|
|||
|
|
{
|
|||
|
|
if (!in_array($permission, $this->permissions)) {
|
|||
|
|
$this->permissions[] = $permission;
|
|||
|
|
}
|
|||
|
|
return $this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 添加角色
|
|||
|
|
*/
|
|||
|
|
public function addRole(string $role): self
|
|||
|
|
{
|
|||
|
|
if (!in_array($role, $this->roles)) {
|
|||
|
|
$this->roles[] = $role;
|
|||
|
|
}
|
|||
|
|
return $this;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 检查是否有指定权限
|
|||
|
|
*/
|
|||
|
|
public function hasPermission(string $permission): bool
|
|||
|
|
{
|
|||
|
|
return in_array($permission, $this->permissions);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 检查是否有指定角色
|
|||
|
|
*/
|
|||
|
|
public function hasRole(string $role): bool
|
|||
|
|
{
|
|||
|
|
return in_array($role, $this->roles);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取用于API响应的数据(隐藏敏感信息)
|
|||
|
|
*/
|
|||
|
|
public function toApiResponse(): array
|
|||
|
|
{
|
|||
|
|
$data = $this->toArray();
|
|||
|
|
|
|||
|
|
// 移除敏感信息
|
|||
|
|
unset($data['password']);
|
|||
|
|
|
|||
|
|
// 格式化日期
|
|||
|
|
if ($this->createdAt) {
|
|||
|
|
$data['created_at'] = $this->createdAt->format('Y-m-d H:i:s');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if ($this->updatedAt) {
|
|||
|
|
$data['updated_at'] = $this->updatedAt->format('Y-m-d H:i:s');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if ($this->lastLoginAt) {
|
|||
|
|
$data['last_login_at'] = $this->lastLoginAt->format('Y-m-d H:i:s');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return $data;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 创建用于登录的用户DTO
|
|||
|
|
*/
|
|||
|
|
public static function forLogin(string $username, string $password): self
|
|||
|
|
{
|
|||
|
|
return (new self())
|
|||
|
|
->setUsername($username)
|
|||
|
|
->setPassword($password);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 创建用于注册的用户DTO
|
|||
|
|
*/
|
|||
|
|
public static function forRegister(string $username, string $email, string $password): self
|
|||
|
|
{
|
|||
|
|
return (new self())
|
|||
|
|
->setUsername($username)
|
|||
|
|
->setEmail($email)
|
|||
|
|
->setPassword($password);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 验证邮箱格式
|
|||
|
|
*/
|
|||
|
|
public function validateEmail(): bool
|
|||
|
|
{
|
|||
|
|
return filter_var($this->email, FILTER_VALIDATE_EMAIL) !== false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 验证手机号格式
|
|||
|
|
*/
|
|||
|
|
public function validatePhone(): bool
|
|||
|
|
{
|
|||
|
|
return preg_match('/^1[3-9]\d{9}$/', $this->phone) === 1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 验证用户名格式
|
|||
|
|
*/
|
|||
|
|
public function validateUsername(): bool
|
|||
|
|
{
|
|||
|
|
return preg_match('/^[a-zA-Z0-9_]{2,50}$/', $this->username) === 1;
|
|||
|
|
}
|
|||
|
|
}
|