初始化-安装依赖
This commit is contained in:
142
server/plugin/saiadmin/basic/think/BaseLogic.php
Normal file
142
server/plugin/saiadmin/basic/think/BaseLogic.php
Normal file
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: sai <1430792918@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace plugin\saiadmin\basic\think;
|
||||
|
||||
use support\think\Db;
|
||||
use plugin\saiadmin\basic\AbstractLogic;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
|
||||
/**
|
||||
* ThinkORM 逻辑层基类
|
||||
*/
|
||||
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->findOrEmpty($id);
|
||||
if ($model->isEmpty()) {
|
||||
throw new ApiException('数据不存在');
|
||||
}
|
||||
return $model->save($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取数据
|
||||
* @param mixed $id
|
||||
* @return mixed
|
||||
*/
|
||||
public function read($id): mixed
|
||||
{
|
||||
$model = $this->model->findOrEmpty($id);
|
||||
if ($model->isEmpty()) {
|
||||
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->getPk();
|
||||
}
|
||||
|
||||
$query->order($orderField, $orderType);
|
||||
|
||||
if ($saiType === 'all') {
|
||||
return $query->select()->toArray();
|
||||
}
|
||||
|
||||
return $query->paginate($limit, false, ['page' => $page])->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取全部数据
|
||||
* @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->getPk();
|
||||
}
|
||||
|
||||
$query->order($orderField, $orderType);
|
||||
return $query->select()->toArray();
|
||||
}
|
||||
}
|
||||
117
server/plugin/saiadmin/basic/think/BaseModel.php
Normal file
117
server/plugin/saiadmin/basic/think/BaseModel.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: sai <1430792918@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace plugin\saiadmin\basic\think;
|
||||
|
||||
use support\think\Model;
|
||||
use think\model\concern\SoftDelete;
|
||||
use plugin\saiadmin\basic\contracts\ModelInterface;
|
||||
|
||||
/**
|
||||
* ThinkORM 模型基类
|
||||
*/
|
||||
class BaseModel extends Model implements ModelInterface
|
||||
{
|
||||
use SoftDelete;
|
||||
|
||||
/**
|
||||
* 删除时间字段
|
||||
* @var string
|
||||
*/
|
||||
protected $deleteTime = 'delete_time';
|
||||
|
||||
/**
|
||||
* 创建时间字段
|
||||
* @var string
|
||||
*/
|
||||
protected $createTime = 'create_time';
|
||||
|
||||
/**
|
||||
* 更新时间字段
|
||||
* @var string
|
||||
*/
|
||||
protected $updateTime = 'update_time';
|
||||
|
||||
/**
|
||||
* 隐藏字段
|
||||
* @var array
|
||||
*/
|
||||
protected $hidden = ['delete_time'];
|
||||
|
||||
/**
|
||||
* 只读字段
|
||||
* @var array
|
||||
*/
|
||||
protected $readonly = ['created_by', 'create_time'];
|
||||
|
||||
/**
|
||||
* 获取表名
|
||||
* @return string
|
||||
*/
|
||||
public function getTableName(): string
|
||||
{
|
||||
return $this->getTable();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取主键名
|
||||
* @return string
|
||||
*/
|
||||
public function getPrimaryKeyName(): string
|
||||
{
|
||||
return $this->getPk();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加时间范围搜索
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增前事件
|
||||
* @param Model $model
|
||||
* @return void
|
||||
*/
|
||||
public static function onBeforeInsert($model): void
|
||||
{
|
||||
$info = getCurrentInfo();
|
||||
$info && $model->setAttr('created_by', $info['id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入前事件
|
||||
* @param Model $model
|
||||
* @return void
|
||||
*/
|
||||
public static function onBeforeWrite($model): void
|
||||
{
|
||||
$info = getCurrentInfo();
|
||||
$info && $model->setAttr('updated_by', $info['id']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user