Backend 默认CURD Traits控制器 放置钩子

pull/88/head
paul 2018-11-27 11:24:25 +08:00
parent 2000a876dd
commit 06ec9b0352
1 changed files with 55 additions and 24 deletions

View File

@ -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);
} }
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); $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);
} }
if (Hook::get('common_model_edit')) {
$result = Hook::listen("common_model_edit", $row, $params);
} else {
$result = $row->allowField(true)->save($params); $result = $row->allowField(true)->save($params);
}
if ($result !== false) { if ($result !== false) {
$this->success(); $this->success();
} else { } else {
@ -154,11 +164,16 @@ 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 (Hook::get('common_model_del')) {
$model = $this->model;
$count = Hook::listen("common_model_del", $model, [$pk,$ids]);
} else {
$list = $this->model->where($pk, 'in', $ids)->select(); $list = $this->model->where($pk, 'in', $ids)->select();
$count = 0; $count = 0;
foreach ($list as $k => $v) { foreach ($list as $k => $v) {
$count += $v->delete(); $count += $v->delete();
} }
}
if ($count) { if ($count) {
$this->success(); $this->success();
} else { } else {
@ -178,6 +193,10 @@ 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 (Hook::get('common_model_destroy')) {
$model = $this->model;
$count = Hook::listen("common_model_destroy", $model, [$pk,$ids]);
} else {
if ($ids) { if ($ids) {
$this->model->where($pk, 'in', $ids); $this->model->where($pk, 'in', $ids);
} }
@ -186,6 +205,7 @@ trait Backend
foreach ($list as $k => $v) { foreach ($list as $k => $v) {
$count += $v->delete(true); $count += $v->delete(true);
} }
}
if ($count) { if ($count) {
$this->success(); $this->success();
} else { } else {
@ -204,6 +224,10 @@ 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 (Hook::get('common_model_restore')) {
$model = $this->model;
$count = Hook::listen("common_model_restore", $model, [$pk,$ids]);
} else {
if ($ids) { if ($ids) {
$this->model->where($pk, 'in', $ids); $this->model->where($pk, 'in', $ids);
} }
@ -212,6 +236,7 @@ trait Backend
foreach ($list as $index => $item) { foreach ($list as $index => $item) {
$count += $item->restore(); $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,11 +259,16 @@ 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 (Hook::get('common_model_multi')) {
$model = $this->model;
$count = Hook::listen("common_model_multi", $model, [$pk,$ids,$values]);
} else {
$count = 0; $count = 0;
$list = $this->model->where($this->model->getPk(), 'in', $ids)->select(); $list = $this->model->where($pk, 'in', $ids)->select();
foreach ($list as $index => $item) { foreach ($list as $index => $item) {
$count += $item->allowField(true)->isUpdate(true)->save($values); $count += $item->allowField(true)->isUpdate(true)->save($values);
} }
}
if ($count) { if ($count) {
$this->success(); $this->success();
} else { } else {