初始化项目

This commit is contained in:
2026-02-08 22:38:13 +08:00
commit 334d2c6312
201 changed files with 32724 additions and 0 deletions
+53
View File
@@ -0,0 +1,53 @@
<?php
namespace App\Models\Auth;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class Department extends Model
{
use SoftDeletes;
protected $table = 'auth_departments';
protected $fillable = [
'name',
'parent_id',
'leader',
'phone',
'sort',
'status',
];
protected $casts = [
'parent_id' => 'integer',
'sort' => 'integer',
'status' => 'integer',
];
/**
* 获取子部门
*/
public function children(): HasMany
{
return $this->hasMany(Department::class, 'parent_id');
}
/**
* 获取父部门
*/
public function parent()
{
return $this->belongsTo(Department::class, 'parent_id');
}
/**
* 获取部门下的用户
*/
public function users(): HasMany
{
return $this->hasMany(User::class, 'department_id');
}
}
+58
View File
@@ -0,0 +1,58 @@
<?php
namespace App\Models\Auth;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class Permission extends Model
{
use SoftDeletes;
protected $table = 'auth_permissions';
protected $fillable = [
'name',
'code',
'type',
'parent_id',
'route',
'component',
'meta',
'sort',
'status',
];
protected $casts = [
'parent_id' => 'integer',
'meta' => 'array',
'sort' => 'integer',
'status' => 'integer',
];
/**
* 关联角色
*/
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class, 'auth_role_permission', 'permission_id', 'role_id')
->withTimestamps();
}
/**
* 获取子权限
*/
public function children()
{
return $this->hasMany(Permission::class, 'parent_id');
}
/**
* 获取父权限
*/
public function parent()
{
return $this->belongsTo(Permission::class, 'parent_id');
}
}
+45
View File
@@ -0,0 +1,45 @@
<?php
namespace App\Models\Auth;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class Role extends Model
{
use SoftDeletes;
protected $table = 'auth_roles';
protected $fillable = [
'name',
'code',
'description',
'sort',
'status',
];
protected $casts = [
'sort' => 'integer',
'status' => 'integer',
];
/**
* 关联权限
*/
public function permissions(): BelongsToMany
{
return $this->belongsToMany(Permission::class, 'auth_role_permission', 'role_id', 'permission_id')
->withTimestamps();
}
/**
* 关联用户
*/
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class, 'auth_user_role', 'role_id', 'user_id')
->withTimestamps();
}
}
+109
View File
@@ -0,0 +1,109 @@
<?php
namespace App\Models\Auth;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Authenticatable implements JWTSubject
{
use SoftDeletes;
protected $table = 'auth_users';
protected $fillable = [
'username',
'password',
'real_name',
'email',
'phone',
'department_id',
'avatar',
'status',
'last_login_at',
'last_login_ip',
];
protected $hidden = [
'password',
'deleted_at',
];
protected $casts = [
'status' => 'integer',
'department_id' => 'integer',
'last_login_at' => 'datetime',
];
/**
* 关联部门
*/
public function department(): BelongsTo
{
return $this->belongsTo(Department::class, 'department_id');
}
/**
* 关联角色
*/
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class, 'auth_user_role', 'user_id', 'role_id')
->withTimestamps();
}
/**
* 获取用户的所有权限
*/
public function permissions()
{
return $this->roles()->with('permissions');
}
/**
* 检查用户是否有指定权限
*/
public function hasPermission(string $permissionCode): bool
{
foreach ($this->roles as $role) {
foreach ($role->permissions as $permission) {
if ($permission->code === $permissionCode) {
return true;
}
}
}
return false;
}
/**
* 检查用户是否有指定角色
*/
public function hasRole(string $roleCode): bool
{
return $this->roles()->where('code', $roleCode)->exists();
}
/**
* 获取 JWT 标识符
*
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}
/**
* 获取 JWT 自定义声明
*
* @return array
*/
public function getJWTCustomClaims()
{
return [];
}
}