You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

270 lines
7.7 KiB

<?php
declare (strict_types=1);
namespace app\api\model;
use service\ActionLog;
use think\App;
use think\Model;
use think\model\concern\SoftDelete;
/**
* 定义所有接口公共请求参数token【swagger ref使用】
* @OA\Parameter(
* in="header",
* name="token",
* description="用户登录返回的jwt-token值",
* required=true,
* @OA\Schema(
* type="string"
* )
* ),
*
*/
/**
* @mixin think\Model
*/
class Common extends Model
{
use SoftDelete;
protected $deleteTime = 'delete_time';
protected $readonly = ['id', 'site_id'];
public $search_arr = []; //搜索数据
/**
* Request实例
* @var \think\Request
*/
protected $request;
public function __construct(array $_data = [])
{
parent::__construct($_data);
$this->request = request();
}
/**
* 获取单条数据基类模型
* @param string $id
* @param null $_op 模型链式对象
* @return array|Model|null
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function parentRead($id = '', $_op = null)
{
if (!is_object($_op)) {
$_op = $this;
}
return $_op->where('site_id', SITE_ID)->find($id);
}
/**
* 获取数据列表基类模型
* @param null $_op 模型链式对象
* @param int $ispage 是否分页,默认分页
* @return array|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
protected function parentLists($_op = null, $ispage = 1,$issite=true)
{
//模型对象为空,设定默认查询对象
if($issite){
if (!is_object($_op) || empty($_op)) {
$_op = $this->where('site_id', SITE_ID); //没有默认where查询 这个一定要 否则获取不到下面的getOptions查询条件值
} else {
$_op->where('site_id', SITE_ID); //强制绑定站点参数
}
}
//公共搜索查询
if (count($this->search_arr) > 0) {
$_param = request()->param(); //搜索参数集合
$_op->where(function ($query) use ($_param) {
foreach ($this->search_arr as $k => $v) {
$search_arr = explode(':', $v);
$search_name = $search_arr[0];
$logic = $search_arr[1] ?? 'AND';
if (array_key_exists($search_name, $_param)) {
if (!empty($_param[$search_name])) {
$query->whereLike($search_name, "%{$_param[$search_name]}%", $logic);
}
}
}
});
}
//执行分页查询
$page = $this->request->param('page', config('page'));
$limit = $this->request->param('limit', config('limit'));
$count = $_op->count();
$this->listsOrder($_op); //统一排序
//无分页判断
$ispage != 1 ?: $_op->page((int)$page, (int)$limit);
$_data = [
'data' => $_op->select()->toArray(),
'page_total' => $count,
];
return $_data;
}
/**
* 数据排序
*/
private function listsOrder(&$_op)
{
if ($_op->getOptions('order')) {
return;
}
$sort = $this->request->get('sort');
$sort = in_array($sort, $this->checkAllowFields()) ? $sort : '';
$order = $this->request->get('order', 'DESC', 'strtoupper') == 'ASC' ? 'ASC' : 'DESC';
$sort_order = $sort . ' ' . $order;
if (empty($sort)) {
$sort_order = in_array('sort', $this->checkAllowFields()) ? 'sort DESC,create_time DESC' : 'create_time DESC';
}
$_op->order($sort_order);
}
/**
* 新增数据基类模型
* @param array $_param
* @return Common|Model
*/
public function parentAdd($_param = [])
{
if (empty($_param)) {
$_param = $this->request->post();
}
$_param['site_id'] = SITE_ID;
$_result = self::create($_param);
return $_result->id;
}
/**
* 修改基类模型
* @param array $_data
* @param null $_op 模型链式对象
* @param $id
* @return bool|\type
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function parentEdit($_data = [], $id = 0, $_op = null)
{
if (empty($_data)) {
$_data = $this->request->param();
}
if (!is_object($_op)) {
$_op = $this;
}
$_op = $_op->where('site_id', SITE_ID)->find($id);
if (empty($_op)) {
return send_http_status('', 101);
}
$_data['site_id'] = SITE_ID; //防止篡改站点
return $_op->save($_data);
}
/**
* 删除
* @param $id
* @param null $_op 模型链式对象
* @param false $true_delete 是否真删除 默认软删除
* @return bool|\type
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function parentDel($id, $_op = null, bool $true_delete = false)
{
if (!is_object($_op)) {
$_op = $this;
}
$ids = explode(',', $id);
$_op = $_op->where('id', 'in', $ids)->where('site_id', SITE_ID)->select();
if ($_op->isEmpty()) {
return send_http_status('', 101);
}
return self::destroy($ids, $true_delete); //TP软删除
}
/**
* 更改数据状态
* @param $id
* @param $status
* @param null $_op
* @return \type
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function parentStatus($id, $status, $_op = null)
{
if (!is_object($_op)) {
$_op = $this;
}
$ids = explode(',', $id);
$_op = $_op->where('id', 'in', $ids)->where('site_id', SITE_ID)->select();
if ($_op->isEmpty()) {
return send_http_status('', 101);
}
$_where = [
['id', 'in', $ids],
['site_id', '=', SITE_ID],
];
// $affected = self::where('id', 'in', $ids)->where('site_id', SITE_ID)->update(['status' => $status]);
$affected = self::update(['status' => $status], $_where);
return $affected;
}
public static function onAfterRead(Model $model)
{
$model->offsetUnset('delete_time');
}
public static function onBeforeWrite(Model $model)
{
//以下参数禁止新增或更改
$model->setAttr('copy_safe_id', (int)$model->getAttr('id')); //设定副本参数
$model->setAttr('copy_safe_site_id', (int)$model->getAttr('site_id')); //设定副本参数
$model->offsetUnset('id');
$model->offsetUnset('site_id');
$model->offsetUnset('delete_time');
$model->offsetUnset('update_time');
$model->offsetUnset('create_time');
}
public static function onAfterWrite(Model $model)
{
//增加创建和修改日志记录
ActionLog::getInstance()->write($model->getData(), $model->getOrigin(), $model->getLastSql());
}
public static function onAfterDelete(Model $model)
{
//增加删除日志记录
ActionLog::getInstance()->write($model->getData(), [], $model->getLastSql());
}
}