123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- <?php
- // +----------------------------------------------------------------------
- // | Yzncms [ 御宅男工作室 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2018 http://yzncms.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: 御宅男 <530765310@qq.com>
- // +----------------------------------------------------------------------
-
- // +----------------------------------------------------------------------
- // | Trait Curd
- // +----------------------------------------------------------------------
- namespace app\admin\library\traits;
-
- use Exception;
- use think\Db;
- use think\exception\PDOException;
- use think\exception\ValidateException;
-
- trait Curd
- {
- /**
- * 排除前台提交过来的字段
- * @param $params
- * @return array
- */
- protected function preExcludeFields($params)
- {
- if (is_array($this->excludeFields)) {
- foreach ($this->excludeFields as $field) {
- if (key_exists($field, $params)) {
- unset($params[$field]);
- }
- }
- } else {
- if (key_exists($this->excludeFields, $params)) {
- unset($params[$this->excludeFields]);
- }
- }
- return $params;
- }
-
- /**
- * 查看
- */
- public function index()
- {
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- [$page, $limit, $where, $sort, $order] = $this->buildTableParames();
-
- $count = $this->modelClass
- ->where($where)
- ->order($sort, $order)
- ->count();
-
- $data = $this->modelClass
- ->where($where)
- ->order($sort, $order)
- ->page($page, $limit)
- ->select();
- $result = ["code" => 0, 'count' => $count, 'data' => $data];
- return json($result);
- }
- return $this->fetch();
- }
-
- /**
- * 回收站
- */
- public function recyclebin()
- {
- if ($this->request->isAjax()) {
- [$page, $limit, $where, $sort, $order] = $this->buildTableParames();
-
- $count = $this->modelClass
- ->onlyTrashed()
- ->where($where)
- ->order($sort, $order)
- ->count();
-
- $data = $this->modelClass
- ->onlyTrashed()
- ->where($where)
- ->order($sort, $order)
- ->page($page, $limit)
- ->select();
- $result = ["code" => 0, 'count' => $count, 'data' => $data];
- return json($result);
- }
- return $this->fetch();
-
- }
-
- /**
- * 添加
- */
- public function add()
- {
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- $params = $this->preExcludeFields($params);
-
- if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- $result = false;
- Db::startTrans();
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->modelClass));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
- $this->validateFailException(true)->validate($params, $validate);
- }
- $result = $this->modelClass->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException | PDOException | Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success('新增成功');
- } else {
- $this->error('未插入任何行');
- }
- }
- $this->error('参数不能为空');
- }
- return $this->fetch();
- }
-
- /**
- * 编辑
- */
- public function edit()
- {
- $id = $this->request->param('id/d', 0);
- $row = $this->modelClass->get($id);
- if (!$row) {
- $this->error('记录未找到');
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- if (!in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error('你没有权限访问');
- }
- }
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- $params = $this->preExcludeFields($params);
- $result = false;
- Db::startTrans();
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->modelClass));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
- $this->validateFailException(true)->validate($params, $validate);
- }
- $result = $row->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException | PDOException | Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success('修改成功');
- } else {
- $this->error('未更新任何行');
- }
- }
- $this->error('参数不能为空');
- }
- $this->view->assign("data", $row);
- return $this->fetch();
- }
-
- /**
- * 删除
- */
- public function del()
- {
- if (false === $this->request->isPost()) {
- $this->error('未知参数');
- }
- $ids = $this->request->param('id/a', null);
- if (empty($ids)) {
- $this->error('参数错误!');
- }
- if (!is_array($ids)) {
- $ids = [0 => $ids];
- }
- $pk = $this->modelClass->getPk();
- $adminIds = $this->getDataLimitAdminIds();
- $where = [];
- if (is_array($adminIds)) {
- $where[] = [$this->dataLimitField, 'in', $adminIds];
- }
- $where[] = [$pk, 'in', $ids];
- $list = $this->modelClass->where($where)->select();
- $count = 0;
- Db::startTrans();
- try {
- foreach ($list as $k => $v) {
- $count += $v->delete();
- }
- Db::commit();
- } catch (PDOException | Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($count) {
- $this->success("操作成功!");
- }
- $this->error('没有数据删除!');
- }
-
- /**
- * 真实删除
- */
- public function destroy()
- {
- if (false === $this->request->isPost()) {
- $this->error('未知参数');
- }
- $ids = $this->request->param('id/a', null);
- if ($ids && !is_array($ids)) {
- $ids = [0 => $ids];
- }
- $pk = $this->modelClass->getPk();
- $adminIds = $this->getDataLimitAdminIds();
- $where = [];
- if (is_array($adminIds)) {
- $where[] = [$this->dataLimitField, 'in', $adminIds];
- }
- if ($ids) {
- $where[] = [$pk, 'in', $ids];
- }
- $count = 0;
- Db::startTrans();
- try {
- $list = $this->modelClass->onlyTrashed()->where($where)->select();
- foreach ($list as $item) {
- $count += $item->delete(true);
- }
- Db::commit();
- } catch (PDOException | Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($count) {
- $this->success("操作成功!");
- }
- $this->error('没有数据删除!');
- }
-
- /**
- * 还原
- */
- public function restore()
- {
- if (false === $this->request->isPost()) {
- $this->error('未知参数');
- }
- $ids = $this->request->param('id/a', null);
- if ($ids && !is_array($ids)) {
- $ids = [0 => $ids];
- }
- $pk = $this->modelClass->getPk();
- $adminIds = $this->getDataLimitAdminIds();
- $where = [];
- if (is_array($adminIds)) {
- $where[] = [$this->dataLimitField, 'in', $adminIds];
- }
- if ($ids) {
- $where[] = [$pk, 'in', $ids];
- }
- $count = 0;
- Db::startTrans();
- try {
- $list = $this->modelClass->onlyTrashed()->where($where)->select();
- foreach ($list as $item) {
- $count += $item->restore();
- }
- Db::commit();
- } catch (PDOException | Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($count) {
- $this->success("操作成功!");
- }
- $this->error('未更新任何行');
- }
-
- /**
- * 批量更新
- */
- public function multi()
- {
- if (false === $this->request->isPost()) {
- $this->error('未知参数');
- }
- $ids = $this->request->param('id/a', null);
- if (empty($ids)) {
- $this->error('参数错误!');
- }
- if (!is_array($ids)) {
- $ids = [0 => $ids];
- }
- $value = $this->request->param('value/d', 0);
- if ($this->request->has('param')) {
- $param = $this->request->param('param/s');
- $param = $this->auth->isAdministrator() ? $param : (in_array($param, (is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields))) ? $param : '');
- if ($param) {
- $pk = $this->modelClass->getPk();
- $adminIds = $this->getDataLimitAdminIds();
- $where = [];
- if (is_array($adminIds)) {
- $where[] = [$this->dataLimitField, 'in', $adminIds];
- }
- $where[] = [$pk, 'in', $ids];
- $count = 0;
- Db::startTrans();
- try {
- $list = $this->modelClass->where($where)->select();
- foreach ($list as $item) {
- $item->{$param} = $value;
- $count += $item->save();
- }
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($count) {
- $this->success("操作成功!");
- }
- $this->error('未更新任何行');
- } else {
- $this->error('你没有权限操作!');
- }
- }
- $this->error('Param参数不能为空');
- }
-
- }
|