mirror of https://gitee.com/karson/fastadmin.git
Backend 默认CURD Traits控制器 放置钩子
parent
2000a876dd
commit
06ec9b0352
|
|
@ -1,6 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace app\admin\library\traits;
|
namespace app\admin\library\traits;
|
||||||
|
use think\Hook;
|
||||||
|
|
||||||
trait Backend
|
trait Backend
|
||||||
{
|
{
|
||||||
|
|
@ -84,7 +85,12 @@ trait Backend
|
||||||
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
|
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
|
||||||
$this->model->validate($validate);
|
$this->model->validate($validate);
|
||||||
}
|
}
|
||||||
$result = $this->model->allowField(true)->save($params);
|
if (Hook::get('common_model_add')) {
|
||||||
|
$model = $this->model;
|
||||||
|
$result = Hook::listen("common_model_add", $model, $params);
|
||||||
|
} else {
|
||||||
|
$result = $this->model->allowField(true)->save($params);
|
||||||
|
}
|
||||||
if ($result !== false) {
|
if ($result !== false) {
|
||||||
$this->success();
|
$this->success();
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -125,7 +131,11 @@ trait Backend
|
||||||
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
|
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
|
||||||
$row->validate($validate);
|
$row->validate($validate);
|
||||||
}
|
}
|
||||||
$result = $row->allowField(true)->save($params);
|
if (Hook::get('common_model_edit')) {
|
||||||
|
$result = Hook::listen("common_model_edit", $row, $params);
|
||||||
|
} else {
|
||||||
|
$result = $row->allowField(true)->save($params);
|
||||||
|
}
|
||||||
if ($result !== false) {
|
if ($result !== false) {
|
||||||
$this->success();
|
$this->success();
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -154,10 +164,15 @@ trait Backend
|
||||||
if (is_array($adminIds)) {
|
if (is_array($adminIds)) {
|
||||||
$count = $this->model->where($this->dataLimitField, 'in', $adminIds);
|
$count = $this->model->where($this->dataLimitField, 'in', $adminIds);
|
||||||
}
|
}
|
||||||
$list = $this->model->where($pk, 'in', $ids)->select();
|
if (Hook::get('common_model_del')) {
|
||||||
$count = 0;
|
$model = $this->model;
|
||||||
foreach ($list as $k => $v) {
|
$count = Hook::listen("common_model_del", $model, [$pk,$ids]);
|
||||||
$count += $v->delete();
|
} else {
|
||||||
|
$list = $this->model->where($pk, 'in', $ids)->select();
|
||||||
|
$count = 0;
|
||||||
|
foreach ($list as $k => $v) {
|
||||||
|
$count += $v->delete();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if ($count) {
|
if ($count) {
|
||||||
$this->success();
|
$this->success();
|
||||||
|
|
@ -178,13 +193,18 @@ trait Backend
|
||||||
if (is_array($adminIds)) {
|
if (is_array($adminIds)) {
|
||||||
$count = $this->model->where($this->dataLimitField, 'in', $adminIds);
|
$count = $this->model->where($this->dataLimitField, 'in', $adminIds);
|
||||||
}
|
}
|
||||||
if ($ids) {
|
if (Hook::get('common_model_destroy')) {
|
||||||
$this->model->where($pk, 'in', $ids);
|
$model = $this->model;
|
||||||
}
|
$count = Hook::listen("common_model_destroy", $model, [$pk,$ids]);
|
||||||
$count = 0;
|
} else {
|
||||||
$list = $this->model->onlyTrashed()->select();
|
if ($ids) {
|
||||||
foreach ($list as $k => $v) {
|
$this->model->where($pk, 'in', $ids);
|
||||||
$count += $v->delete(true);
|
}
|
||||||
|
$count = 0;
|
||||||
|
$list = $this->model->onlyTrashed()->select();
|
||||||
|
foreach ($list as $k => $v) {
|
||||||
|
$count += $v->delete(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if ($count) {
|
if ($count) {
|
||||||
$this->success();
|
$this->success();
|
||||||
|
|
@ -204,13 +224,18 @@ trait Backend
|
||||||
if (is_array($adminIds)) {
|
if (is_array($adminIds)) {
|
||||||
$this->model->where($this->dataLimitField, 'in', $adminIds);
|
$this->model->where($this->dataLimitField, 'in', $adminIds);
|
||||||
}
|
}
|
||||||
if ($ids) {
|
if (Hook::get('common_model_restore')) {
|
||||||
$this->model->where($pk, 'in', $ids);
|
$model = $this->model;
|
||||||
}
|
$count = Hook::listen("common_model_restore", $model, [$pk,$ids]);
|
||||||
$count = 0;
|
} else {
|
||||||
$list = $this->model->onlyTrashed()->select();
|
if ($ids) {
|
||||||
foreach ($list as $index => $item) {
|
$this->model->where($pk, 'in', $ids);
|
||||||
$count += $item->restore();
|
}
|
||||||
|
$count = 0;
|
||||||
|
$list = $this->model->onlyTrashed()->select();
|
||||||
|
foreach ($list as $index => $item) {
|
||||||
|
$count += $item->restore();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if ($count) {
|
if ($count) {
|
||||||
$this->success();
|
$this->success();
|
||||||
|
|
@ -224,6 +249,7 @@ trait Backend
|
||||||
public function multi($ids = "")
|
public function multi($ids = "")
|
||||||
{
|
{
|
||||||
$ids = $ids ? $ids : $this->request->param("ids");
|
$ids = $ids ? $ids : $this->request->param("ids");
|
||||||
|
$pk = $this->model->getPk();
|
||||||
if ($ids) {
|
if ($ids) {
|
||||||
if ($this->request->has('params')) {
|
if ($this->request->has('params')) {
|
||||||
parse_str($this->request->post("params"), $values);
|
parse_str($this->request->post("params"), $values);
|
||||||
|
|
@ -233,10 +259,15 @@ trait Backend
|
||||||
if (is_array($adminIds)) {
|
if (is_array($adminIds)) {
|
||||||
$this->model->where($this->dataLimitField, 'in', $adminIds);
|
$this->model->where($this->dataLimitField, 'in', $adminIds);
|
||||||
}
|
}
|
||||||
$count = 0;
|
if (Hook::get('common_model_multi')) {
|
||||||
$list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
|
$model = $this->model;
|
||||||
foreach ($list as $index => $item) {
|
$count = Hook::listen("common_model_multi", $model, [$pk,$ids,$values]);
|
||||||
$count += $item->allowField(true)->isUpdate(true)->save($values);
|
} else {
|
||||||
|
$count = 0;
|
||||||
|
$list = $this->model->where($pk, 'in', $ids)->select();
|
||||||
|
foreach ($list as $index => $item) {
|
||||||
|
$count += $item->allowField(true)->isUpdate(true)->save($values);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if ($count) {
|
if ($count) {
|
||||||
$this->success();
|
$this->success();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue