初始化-安装依赖
This commit is contained in:
152
server/plugin/saiadmin/basic/eloquent/BaseLogic.php
Normal file
152
server/plugin/saiadmin/basic/eloquent/BaseLogic.php
Normal file
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: sai <1430792918@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace plugin\saiadmin\basic\eloquent;
|
||||
|
||||
use support\Db;
|
||||
use plugin\saiadmin\basic\AbstractLogic;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
|
||||
/**
|
||||
* Laravel Eloquent 逻辑层基类
|
||||
*/
|
||||
class BaseLogic extends AbstractLogic
|
||||
{
|
||||
/**
|
||||
* 数据库事务操作
|
||||
* @param callable $closure
|
||||
* @param bool $isTran
|
||||
* @return mixed
|
||||
*/
|
||||
public function transaction(callable $closure, bool $isTran = true): mixed
|
||||
{
|
||||
return $isTran ? Db::transaction($closure) : $closure();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加数据
|
||||
* @param array $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function add(array $data): mixed
|
||||
{
|
||||
$model = $this->model->create($data);
|
||||
return $model->getKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改数据
|
||||
* @param mixed $id
|
||||
* @param array $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function edit($id, array $data): mixed
|
||||
{
|
||||
$model = $this->model->find($id);
|
||||
if (!$model) {
|
||||
throw new ApiException('数据不存在');
|
||||
}
|
||||
return $model->update($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取数据
|
||||
* @param mixed $id
|
||||
* @return mixed
|
||||
*/
|
||||
public function read($id): mixed
|
||||
{
|
||||
$model = $this->model->find($id);
|
||||
if (!$model) {
|
||||
throw new ApiException('数据不存在');
|
||||
}
|
||||
return $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
* @param mixed $ids
|
||||
* @return bool
|
||||
*/
|
||||
public function destroy($ids): bool
|
||||
{
|
||||
return $this->model->destroy($ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索器搜索
|
||||
* @param array $searchWhere
|
||||
* @return mixed
|
||||
*/
|
||||
public function search(array $searchWhere = []): mixed
|
||||
{
|
||||
$withSearch = array_keys($searchWhere);
|
||||
$data = [];
|
||||
foreach ($searchWhere as $key => $value) {
|
||||
if ($value !== '' && $value !== null && $value !== []) {
|
||||
$data[$key] = $value;
|
||||
}
|
||||
}
|
||||
$withSearch = array_keys($data);
|
||||
return $this->model->withSearch($withSearch, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询数据
|
||||
* @param mixed $query
|
||||
* @return mixed
|
||||
*/
|
||||
public function getList($query): mixed
|
||||
{
|
||||
$request = request();
|
||||
$saiType = $request ? $request->input('saiType', 'list') : 'list';
|
||||
$page = $request ? $request->input('page', 1) : 1;
|
||||
$limit = $request ? $request->input('limit', 10) : 10;
|
||||
$orderField = $request ? $request->input('orderField', '') : '';
|
||||
$orderType = $request ? $request->input('orderType', $this->orderType) : $this->orderType;
|
||||
|
||||
if (empty($orderField)) {
|
||||
$orderField = $this->orderField !== '' ? $this->orderField : $this->model->getKeyName();
|
||||
}
|
||||
|
||||
$query->orderBy($orderField, $orderType);
|
||||
|
||||
if ($saiType === 'all') {
|
||||
return $query->get()->toArray();
|
||||
}
|
||||
|
||||
$list = $query->paginate($limit, ['*'], 'page', $page);
|
||||
|
||||
return [
|
||||
'current_page' => $list->currentPage(),
|
||||
'per_page' => $list->perPage(),
|
||||
'last_page' => $list->lastPage(),
|
||||
'has_more' => $list->hasMorePages(),
|
||||
'total' => $list->total(),
|
||||
'data' => $list->items(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取全部数据
|
||||
* @param mixed $query
|
||||
* @return mixed
|
||||
*/
|
||||
public function getAll($query): mixed
|
||||
{
|
||||
$request = request();
|
||||
$orderField = $request ? $request->input('orderField', '') : '';
|
||||
$orderType = $request ? $request->input('orderType', $this->orderType) : $this->orderType;
|
||||
|
||||
if (empty($orderField)) {
|
||||
$orderField = $this->orderField !== '' ? $this->orderField : $this->model->getKeyName();
|
||||
}
|
||||
|
||||
$query->orderBy($orderField, $orderType);
|
||||
return $query->get()->toArray();
|
||||
}
|
||||
|
||||
}
|
||||
171
server/plugin/saiadmin/basic/eloquent/BaseModel.php
Normal file
171
server/plugin/saiadmin/basic/eloquent/BaseModel.php
Normal file
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: sai <1430792918@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace plugin\saiadmin\basic\eloquent;
|
||||
|
||||
use support\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use plugin\saiadmin\basic\contracts\ModelInterface;
|
||||
|
||||
/**
|
||||
* Laravel Eloquent 模型基类
|
||||
*/
|
||||
class BaseModel extends Model implements ModelInterface
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
/**
|
||||
* 创建时间字段
|
||||
*/
|
||||
const CREATED_AT = 'create_time';
|
||||
|
||||
/**
|
||||
* 更新时间字段
|
||||
*/
|
||||
const UPDATED_AT = 'update_time';
|
||||
|
||||
/**
|
||||
* 删除时间字段
|
||||
*/
|
||||
const DELETED_AT = 'delete_time';
|
||||
|
||||
/**
|
||||
* 隐藏字段
|
||||
* @var array
|
||||
*/
|
||||
protected $hidden = ['delete_time'];
|
||||
|
||||
/**
|
||||
* 不可批量赋值的属性 (为空表示全部可赋值)
|
||||
* @var array
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
/**
|
||||
* 类型转换
|
||||
* @return array
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'create_time' => 'datetime:Y-m-d H:i:s',
|
||||
'update_time' => 'datetime:Y-m-d H:i:s',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理时区问题
|
||||
* @param \DateTimeInterface $date
|
||||
* @return string
|
||||
*/
|
||||
protected function serializeDate(\DateTimeInterface $date): string
|
||||
{
|
||||
return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取表名
|
||||
* @return string
|
||||
*/
|
||||
public function getTableName(): string
|
||||
{
|
||||
return $this->getTable();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取主键名
|
||||
* @return string
|
||||
*/
|
||||
public function getPrimaryKeyName(): string
|
||||
{
|
||||
return $this->getKeyName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索器搜索
|
||||
* @param array $fields
|
||||
* @param array $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function withSearch(array $fields, array $data): mixed
|
||||
{
|
||||
$query = $this->newQuery();
|
||||
foreach ($fields as $field) {
|
||||
$method = 'search' . ucfirst($this->toCamelCase($field)) . 'Attr';
|
||||
if (method_exists($this, $method) && isset($data[$field]) && $data[$field] !== '') {
|
||||
$this->$method($query, $data[$field]);
|
||||
} else {
|
||||
$query->where($field, $data[$field]);
|
||||
}
|
||||
}
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将下划线命名转换为驼峰命名
|
||||
* @param string $str
|
||||
* @return string
|
||||
*/
|
||||
protected function toCamelCase(string $str): string
|
||||
{
|
||||
return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $str))));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加时间范围搜索
|
||||
* @param $query
|
||||
* @param $value
|
||||
*/
|
||||
public function searchCreateTimeAttr($query, $value)
|
||||
{
|
||||
if (is_array($value)) {
|
||||
$query->whereBetween('create_time', $value);
|
||||
} else {
|
||||
$query->where('create_time', '=', $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新时间范围搜索
|
||||
* @param mixed $query
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function searchUpdateTimeAttr($query, $value)
|
||||
{
|
||||
if (is_array($value)) {
|
||||
$query->whereBetween('update_time', $value);
|
||||
} else {
|
||||
$query->where('update_time', '=', $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 模型启动事件
|
||||
* @return void
|
||||
*/
|
||||
protected static function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
// 创建前事件
|
||||
static::creating(function ($model) {
|
||||
$info = getCurrentInfo();
|
||||
$schema = $model->getConnection()->getSchemaBuilder();
|
||||
if ($info && $schema->hasColumn($model->getTable(), 'created_by')) {
|
||||
$model->created_by = $info['id'];
|
||||
}
|
||||
});
|
||||
|
||||
// 保存前事件
|
||||
static::saving(function ($model) {
|
||||
$info = getCurrentInfo();
|
||||
$schema = $model->getConnection()->getSchemaBuilder();
|
||||
if ($info && $schema->hasColumn($model->getTable(), 'updated_by')) {
|
||||
$model->updated_by = $info['id'];
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user