更新基础架构

This commit is contained in:
技术老胡
2026-01-27 09:07:38 +08:00
parent 28f1a2855c
commit 4a34feec54
36 changed files with 1289 additions and 540 deletions

View File

@@ -0,0 +1,42 @@
<?php
namespace app\common\base;
use support\Response;
use support\Request;
/**
* 控制器基础类
* - 提供统一的 success/fail 响应封装
*
* 约定:
* - 控制器统一通过 $this->request->* 获取请求数据
* - 为避免每个控制器构造函数重复注入 Request本类通过 __get('request') 返回当前请求对象
*/
abstract class BaseController
{
/**
* 成功响应
*/
protected function success(mixed $data = null, string $message = 'success', int $code = 200): Response
{
return json([
'code' => $code,
'message' => $message,
'data' => $data,
]);
}
/**
* 失败响应
*/
protected function fail(string $message = 'fail', int $code = 500, mixed $data = null): Response
{
return json([
'code' => $code,
'message' => $message,
'data' => $data,
]);
}
}

164
app/common/base/BaseDao.php Normal file
View File

@@ -0,0 +1,164 @@
<?php
namespace app\common\base;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Query\Builder;
use support\Db;
/**
* DAO 基础类
* - 封装数据库连接和基础 CRUD 操作
* - 提供查询构造器访问
*/
abstract class BaseDao
{
/**
* 数据库连接名称(子类可覆盖)
*/
protected string $connection = 'default';
/**
* 表名(子类必须定义)
*/
protected string $table = '';
/**
* 获取数据库连接
*/
protected function connection()
{
return Db::connection($this->connection);
}
/**
* 获取查询构造器
*/
protected function query(): Builder
{
return Db::connection($this->connection)->table($this->table);
}
/**
* 根据 ID 查找单条记录
*/
public function findById(int $id, array $columns = ['*']): ?array
{
$result = $this->query()->where('id', $id)->first($columns);
return $result ? (array)$result : null;
}
/**
* 根据条件查找单条记录
*/
public function findOne(array $where, array $columns = ['*']): ?array
{
$query = $this->query();
foreach ($where as $key => $value) {
$query->where($key, $value);
}
$result = $query->first($columns);
return $result ? (array)$result : null;
}
/**
* 根据条件查找多条记录
*/
public function findMany(array $where = [], array $columns = ['*'], array $orderBy = [], int $limit = 0): array
{
$query = $this->query();
foreach ($where as $key => $value) {
if (is_array($value)) {
$query->whereIn($key, $value);
} else {
$query->where($key, $value);
}
}
foreach ($orderBy as $column => $direction) {
$query->orderBy($column, $direction);
}
if ($limit > 0) {
$query->limit($limit);
}
$results = $query->get($columns);
return array_map(fn($item) => (array)$item, $results->toArray());
}
/**
* 插入单条记录
*/
public function insert(array $data): int
{
return $this->query()->insertGetId($data);
}
/**
* 批量插入
*/
public function insertBatch(array $data): bool
{
return $this->query()->insert($data);
}
/**
* 根据 ID 更新记录
*/
public function updateById(int $id, array $data): int
{
return $this->query()->where('id', $id)->update($data);
}
/**
* 根据条件更新记录
*/
public function update(array $where, array $data): int
{
$query = $this->query();
foreach ($where as $key => $value) {
$query->where($key, $value);
}
return $query->update($data);
}
/**
* 根据 ID 删除记录
*/
public function deleteById(int $id): int
{
return $this->query()->where('id', $id)->delete();
}
/**
* 根据条件删除记录
*/
public function delete(array $where): int
{
$query = $this->query();
foreach ($where as $key => $value) {
$query->where($key, $value);
}
return $query->delete();
}
/**
* 统计记录数
*/
public function count(array $where = []): int
{
$query = $this->query();
foreach ($where as $key => $value) {
$query->where($key, $value);
}
return $query->count();
}
/**
* 判断记录是否存在
*/
public function exists(array $where): bool
{
return $this->count($where) > 0;
}
}

View File

@@ -0,0 +1,101 @@
<?php
namespace app\common\base;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
/**
* 模型基础类
* - 统一禁用时间戳(如需要可在子类开启)
* - 提供常用查询作用域和便捷方法
*/
abstract class BaseModel extends Model
{
/**
* 禁用时间戳(默认)
*/
public $timestamps = false;
/**
* 允许批量赋值的字段(子类可覆盖)
*/
protected $guarded = [];
/**
* 连接名称(默认使用配置的 default
*/
protected $connection = 'default';
/**
* 根据 ID 查找(返回数组格式)
*/
public static function findById(int $id): ?array
{
$model = static::find($id);
return $model ? $model->toArray() : null;
}
/**
* 根据条件查找单条(返回数组格式)
*/
public static function findOne(array $where): ?array
{
$query = static::query();
foreach ($where as $key => $value) {
$query->where($key, $value);
}
$model = $query->first();
return $model ? $model->toArray() : null;
}
/**
* 根据条件查找多条(返回数组格式)
*/
public static function findMany(array $where = [], array $orderBy = [], int $limit = 0): array
{
$query = static::query();
foreach ($where as $key => $value) {
if (is_array($value)) {
$query->whereIn($key, $value);
} else {
$query->where($key, $value);
}
}
foreach ($orderBy as $column => $direction) {
$query->orderBy($column, $direction);
}
if ($limit > 0) {
$query->limit($limit);
}
return $query->get()->map(fn($item) => $item->toArray())->toArray();
}
/**
* 启用状态作用域
*/
public function scopeEnabled(Builder $query): Builder
{
return $query->where('status', 1);
}
/**
* 禁用状态作用域
*/
public function scopeDisabled(Builder $query): Builder
{
return $query->where('status', 0);
}
/**
* 转换为数组(统一处理 null 值)
*/
public function toArray(): array
{
$array = parent::toArray();
// 将 null 值转换为空字符串(可选,根据业务需求调整)
return array_map(fn($value) => $value === null ? '' : $value, $array);
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace app\common\base;
/**
* 仓库基础类
* - 支持注入 DAO 依赖
* - 通过魔术方法代理 DAO 的方法调用
* - 提供通用的数据访问封装
*/
abstract class BaseRepository
{
/**
* DAO 实例(可选,子类通过构造函数注入)
*/
protected ?BaseDao $dao = null;
/**
* 构造函数,子类可注入 DAO
*/
public function __construct(?BaseDao $dao = null)
{
$this->dao = $dao;
}
/**
* 魔术方法:代理 DAO 的方法调用
* 如果仓库自身没有该方法,且存在 DAO 实例,则调用 DAO 的对应方法
*/
public function __call(string $method, array $arguments)
{
if ($this->dao && method_exists($this->dao, $method)) {
return $this->dao->{$method}(...$arguments);
}
throw new \BadMethodCallException(
sprintf('Method %s::%s does not exist', static::class, $method)
);
}
/**
* 检查 DAO 是否已注入
*/
protected function hasDao(): bool
{
return $this->dao !== null;
}
/**
* 获取 DAO 实例
*/
protected function getDao(): ?BaseDao
{
return $this->dao;
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace app\common\base;
use support\Log;
/**
* 服务基础类
* - 提供日志记录能力
* - 预留事务、事件发布等扩展点
*/
abstract class BaseService
{
/**
* 记录日志
*/
protected function log(string $level, string $message, array $context = []): void
{
$logMessage = sprintf('[%s] %s', static::class, $message);
Log::log($level, $logMessage, $context);
}
/**
* 记录信息日志
*/
protected function info(string $message, array $context = []): void
{
$this->log('info', $message, $context);
}
/**
* 记录警告日志
*/
protected function warning(string $message, array $context = []): void
{
$this->log('warning', $message, $context);
}
/**
* 记录错误日志
*/
protected function error(string $message, array $context = []): void
{
$this->log('error', $message, $context);
}
/**
* 记录调试日志
*/
protected function debug(string $message, array $context = []): void
{
$this->log('debug', $message, $context);
}
}