study-api-v2/app/Models/BaseModel.php

154 lines
3.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class BaseModel extends Model
{
use SoftDeletes;
// 自定义时间戳字段
const CREATED_AT = 'create_time';
const UPDATED_AT = 'update_time';
const DELETED_AT = 'deleted';
/**
* 序列化日期为指定格式
*/
protected function serializeDate(\DateTimeInterface $date): string
{
return $date->format('Y-m-d H:i:s');
}
/**
* 查询作用域:未删除
*/
public function scopeNotDeleted($query)
{
return $query->where('deleted', 0);
}
/**
* 查询作用域:当前租户
*/
public function scopeCurrentTenant($query)
{
$tenantId = $this->getCurrentTenantId();
if ($tenantId !== null) {
return $query->where('tenant_id', $tenantId);
}
return $query;
}
/**
* 查询作用域:启用状态
*/
public function scopeActive($query)
{
return $query->where('status', 1);
}
/**
* 查询作用域:排序
*/
public function scopeOrdered($query)
{
return $query->orderBy('sort', 'asc')->orderBy('id', 'desc');
}
/**
* 查询作用域:安全查询(未删除 + 当前租户)
*/
public function scopeSafeQuery($query)
{
return $query->notDeleted()->currentTenant();
}
/**
* 获取当前租户ID
*/
protected function getCurrentTenantId(): ?int
{
$user = auth('admin')->user();
return $user ? $user->tenant_id : null;
}
/**
* 设置系统字段(创建时)
*/
public function setCreateFields(): void
{
$userId = auth('admin')->id() ?? 0;
$tenantId = $this->getCurrentTenantId() ?? 0;
$this->creator = $userId;
$this->create_time = now();
$this->tenant_id = $tenantId;
$this->deleted = 0;
}
/**
* 设置系统字段(更新时)
*/
public function setUpdateFields(): void
{
$userId = auth('admin')->id() ?? 0;
$this->updater = $userId;
$this->update_time = now();
}
/**
* 验证租户权限
*/
public function validateTenantAccess(): bool
{
$currentTenantId = $this->getCurrentTenantId();
// 如果当前用户没有租户ID不允许访问
if ($currentTenantId === null) {
return false;
}
// 如果记录没有租户ID不允许访问
if (!isset($this->tenant_id)) {
return false;
}
// 验证租户ID是否匹配
return $this->tenant_id == $currentTenantId;
}
/**
* 保存前的钩子
*/
protected static function booted()
{
// 创建时自动设置系统字段
static::creating(function ($model) {
$model->setCreateFields();
});
// 更新时自动设置系统字段
static::updating(function ($model) {
$model->setUpdateFields();
});
// 全局作用域:自动过滤租户数据
static::addGlobalScope('tenant', function ($builder) {
$model = new static;
$tenantId = $model->getCurrentTenantId();
if ($tenantId !== null) {
$builder->where('tenant_id', $tenantId);
}
});
// 全局作用域:自动过滤已删除数据
static::addGlobalScope('not_deleted', function ($builder) {
$builder->where('deleted', 0);
});
}
}