初始化-安装依赖
This commit is contained in:
109
server/plugin/saiadmin/basic/AbstractLogic.php
Normal file
109
server/plugin/saiadmin/basic/AbstractLogic.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: sai <1430792918@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace plugin\saiadmin\basic;
|
||||
|
||||
use plugin\saiadmin\basic\contracts\LogicInterface;
|
||||
|
||||
/**
|
||||
* 抽象逻辑层基类
|
||||
* 定义通用属性和方法签名,具体实现由各 ORM 驱动完成
|
||||
*/
|
||||
abstract class AbstractLogic implements LogicInterface
|
||||
{
|
||||
/**
|
||||
* 模型注入
|
||||
* @var object
|
||||
*/
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* 管理员信息
|
||||
* @var array
|
||||
*/
|
||||
protected array $adminInfo;
|
||||
|
||||
/**
|
||||
* 排序字段
|
||||
* @var string
|
||||
*/
|
||||
protected string $orderField = '';
|
||||
|
||||
/**
|
||||
* 排序方式
|
||||
* @var string
|
||||
*/
|
||||
protected string $orderType = 'ASC';
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* @param $user
|
||||
* @return void
|
||||
*/
|
||||
public function init($user): void
|
||||
{
|
||||
$this->adminInfo = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置排序字段
|
||||
* @param string $field
|
||||
* @return static
|
||||
*/
|
||||
public function setOrderField(string $field): static
|
||||
{
|
||||
$this->orderField = $field;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置排序方式
|
||||
* @param string $type
|
||||
* @return static
|
||||
*/
|
||||
public function setOrderType(string $type): static
|
||||
{
|
||||
$this->orderType = $type;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模型实例
|
||||
* @return object
|
||||
*/
|
||||
public function getModel(): object
|
||||
{
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上传的导入文件
|
||||
* @param $file
|
||||
* @return string
|
||||
*/
|
||||
public function getImport($file): string
|
||||
{
|
||||
$full_dir = runtime_path() . '/resource/';
|
||||
if (!is_dir($full_dir)) {
|
||||
mkdir($full_dir, 0777, true);
|
||||
}
|
||||
$ext = $file->getUploadExtension() ?: null;
|
||||
$full_path = $full_dir . md5(time()) . '.' . $ext;
|
||||
$file->move($full_path);
|
||||
return $full_path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法调用代理到模型
|
||||
* @param string $name
|
||||
* @param array $arguments
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call(string $name, array $arguments): mixed
|
||||
{
|
||||
return call_user_func_array([$this->model, $name], $arguments);
|
||||
}
|
||||
}
|
||||
74
server/plugin/saiadmin/basic/BaseController.php
Normal file
74
server/plugin/saiadmin/basic/BaseController.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: sai <1430792918@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace plugin\saiadmin\basic;
|
||||
|
||||
use plugin\saiadmin\app\cache\UserInfoCache;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
|
||||
/**
|
||||
* 基类 控制器继承此类
|
||||
*/
|
||||
class BaseController extends OpenController
|
||||
{
|
||||
|
||||
/**
|
||||
* 当前登陆管理员信息
|
||||
*/
|
||||
protected $adminInfo;
|
||||
|
||||
/**
|
||||
* 当前登陆管理员ID
|
||||
*/
|
||||
protected int $adminId;
|
||||
|
||||
/**
|
||||
* 当前登陆管理员账号
|
||||
*/
|
||||
protected string $adminName;
|
||||
|
||||
/**
|
||||
* 逻辑层注入
|
||||
*/
|
||||
protected $logic;
|
||||
|
||||
/**
|
||||
* 验证器注入
|
||||
*/
|
||||
protected $validate;
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
protected function init(): void
|
||||
{
|
||||
// 登录模式赋值
|
||||
$isLogin = request()->header('check_login', false);
|
||||
if ($isLogin) {
|
||||
$result = request()->header('check_admin');
|
||||
$this->adminId = $result['id'];
|
||||
$this->adminName = $result['username'];
|
||||
$this->adminInfo = UserInfoCache::getUserInfo($result['id']);
|
||||
|
||||
// 用户数据传递给逻辑层
|
||||
$this->logic && $this->logic->init($this->adminInfo);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证器调用
|
||||
*/
|
||||
protected function validate(string $scene, $data): bool
|
||||
{
|
||||
if ($this->validate) {
|
||||
if (!$this->validate->scene($scene)->check($data)) {
|
||||
throw new ApiException($this->validate->getError());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
77
server/plugin/saiadmin/basic/BaseValidate.php
Normal file
77
server/plugin/saiadmin/basic/BaseValidate.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: sai <1430792918@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace plugin\saiadmin\basic;
|
||||
|
||||
use think\Validate;
|
||||
|
||||
/**
|
||||
* 验证器基类
|
||||
*/
|
||||
class BaseValidate extends Validate
|
||||
{
|
||||
|
||||
/**
|
||||
* 验证是否唯一
|
||||
* @access public
|
||||
* @param mixed $value 字段值
|
||||
* @param mixed $rule 验证规则 格式:数据表,字段名,排除ID,主键名
|
||||
* @param array $data 数据
|
||||
* @param string $field 验证字段名
|
||||
* @return bool
|
||||
*/
|
||||
public function unique($value, $rule, array $data = [], string $field = ''): bool
|
||||
{
|
||||
if (is_string($rule)) {
|
||||
$rule = explode(',', $rule);
|
||||
}
|
||||
|
||||
if (str_contains($rule[0], '\\')) {
|
||||
// 指定模型类
|
||||
$db = new $rule[0];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
$key = $rule[1] ?? $field;
|
||||
$map = [];
|
||||
|
||||
if (str_contains($key, '^')) {
|
||||
// 支持多个字段验证
|
||||
$fields = explode('^', $key);
|
||||
foreach ($fields as $key) {
|
||||
if (isset($data[$key])) {
|
||||
$map[] = [$key, '=', $data[$key]];
|
||||
}
|
||||
}
|
||||
} elseif (strpos($key, '=')) {
|
||||
// 支持复杂验证
|
||||
parse_str($key, $array);
|
||||
foreach ($array as $k => $val) {
|
||||
$map[] = [$k, '=', $data[$k] ?? $val];
|
||||
}
|
||||
} elseif (isset($data[$field])) {
|
||||
$map[] = [$key, '=', $data[$field]];
|
||||
}
|
||||
|
||||
$pk = !empty($rule[3]) ? $rule[3] : $db->getPrimaryKeyName();
|
||||
|
||||
if (is_string($pk)) {
|
||||
if (isset($rule[2])) {
|
||||
$map[] = [$pk, '<>', $rule[2]];
|
||||
} elseif (isset($data[$pk])) {
|
||||
$map[] = [$pk, '<>', $data[$pk]];
|
||||
}
|
||||
}
|
||||
|
||||
if ($db->where($map)->count() > 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
60
server/plugin/saiadmin/basic/OpenController.php
Normal file
60
server/plugin/saiadmin/basic/OpenController.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: sai <1430792918@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace plugin\saiadmin\basic;
|
||||
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
|
||||
/**
|
||||
* 基类 控制器继承此类
|
||||
*/
|
||||
class OpenController
|
||||
{
|
||||
/**
|
||||
* 构造方法
|
||||
* @access public
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
// 控制器初始化
|
||||
$this->init();
|
||||
}
|
||||
|
||||
/**
|
||||
* 成功返回json内容
|
||||
* @param array|string $data
|
||||
* @param string $msg
|
||||
* @param int $option
|
||||
* @return Response
|
||||
*/
|
||||
public function success(array | string $data = [], string $msg = 'success', int $option = JSON_UNESCAPED_UNICODE): Response
|
||||
{
|
||||
if (is_string($data)) {
|
||||
$msg = $data;
|
||||
}
|
||||
return json(['code' => 200, 'message' => $msg, 'data' => $data], $option);
|
||||
}
|
||||
|
||||
/**
|
||||
* 失败返回json内容
|
||||
* @param string $msg
|
||||
* @return Response
|
||||
*/
|
||||
public function fail(string $msg = 'fail'): Response
|
||||
{
|
||||
return json(['code' => 400, 'message' => $msg]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
protected function init(): void
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
}
|
||||
79
server/plugin/saiadmin/basic/contracts/LogicInterface.php
Normal file
79
server/plugin/saiadmin/basic/contracts/LogicInterface.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: sai <1430792918@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace plugin\saiadmin\basic\contracts;
|
||||
|
||||
/**
|
||||
* Logic 接口定义
|
||||
* 所有 Logic 基类必须实现此接口
|
||||
*/
|
||||
interface LogicInterface
|
||||
{
|
||||
/**
|
||||
* 初始化
|
||||
* @param mixed $user 用户信息
|
||||
* @return void
|
||||
*/
|
||||
public function init($user): void;
|
||||
|
||||
/**
|
||||
* 添加数据
|
||||
* @param array $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function add(array $data): mixed;
|
||||
|
||||
/**
|
||||
* 修改数据
|
||||
* @param mixed $id
|
||||
* @param array $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function edit($id, array $data): mixed;
|
||||
|
||||
/**
|
||||
* 读取数据
|
||||
* @param mixed $id
|
||||
* @return mixed
|
||||
*/
|
||||
public function read($id): mixed;
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
* @param mixed $ids
|
||||
* @return bool
|
||||
*/
|
||||
public function destroy($ids): bool;
|
||||
|
||||
/**
|
||||
* 搜索器搜索
|
||||
* @param array $searchWhere
|
||||
* @return mixed
|
||||
*/
|
||||
public function search(array $searchWhere = []): mixed;
|
||||
|
||||
/**
|
||||
* 分页查询数据
|
||||
* @param mixed $query
|
||||
* @return mixed
|
||||
*/
|
||||
public function getList($query): mixed;
|
||||
|
||||
/**
|
||||
* 获取全部数据
|
||||
* @param mixed $query
|
||||
* @return mixed
|
||||
*/
|
||||
public function getAll($query): mixed;
|
||||
|
||||
/**
|
||||
* 数据库事务操作
|
||||
* @param callable $closure
|
||||
* @param bool $isTran
|
||||
* @return mixed
|
||||
*/
|
||||
public function transaction(callable $closure, bool $isTran = true): mixed;
|
||||
}
|
||||
27
server/plugin/saiadmin/basic/contracts/ModelInterface.php
Normal file
27
server/plugin/saiadmin/basic/contracts/ModelInterface.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | saiadmin [ saiadmin快速开发框架 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: sai <1430792918@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace plugin\saiadmin\basic\contracts;
|
||||
|
||||
/**
|
||||
* Model 接口定义
|
||||
* 所有 Model 基类必须实现此接口
|
||||
*/
|
||||
interface ModelInterface
|
||||
{
|
||||
/**
|
||||
* 获取表名
|
||||
* @return string
|
||||
*/
|
||||
public function getTableName(): string;
|
||||
|
||||
/**
|
||||
* 获取主键名
|
||||
* @return string
|
||||
*/
|
||||
public function getPrimaryKeyName(): string;
|
||||
|
||||
}
|
||||
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'];
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
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