From b9f764e4d0afd20ff6684d575f15f974d081337f Mon Sep 17 00:00:00 2001 From: wolfcode <37436228+wolf-leo@users.noreply.github.com> Date: Thu, 27 Mar 2025 18:38:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor(admin):=20=E9=87=8D=E6=9E=84=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E5=99=A8=E5=92=8C=E6=A8=A1=E5=9E=8B=E7=9A=84=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - https://github.com/top-think/think-orm/issues/704 --- app/admin/controller/Ajax.php | 27 +++++++++---------- app/admin/controller/mall/Cate.php | 2 +- app/admin/controller/mall/Goods.php | 10 +++---- app/admin/controller/system/Admin.php | 22 +++++++-------- app/admin/controller/system/Auth.php | 8 +++--- app/admin/controller/system/Config.php | 10 +++---- app/admin/controller/system/Log.php | 8 +++--- app/admin/controller/system/Menu.php | 27 +++++++++---------- app/admin/controller/system/Node.php | 22 +++++++-------- app/admin/controller/system/Quick.php | 2 +- app/admin/controller/system/Uploadfile.php | 2 +- app/admin/model/SystemAdmin.php | 6 ++--- app/admin/model/SystemAuth.php | 4 +-- app/admin/model/SystemMenu.php | 10 +++---- app/admin/model/SystemNode.php | 8 +++--- .../curd/templates/controller/controller.code | 4 +-- .../service/curd/templates/model/model.code | 2 +- app/admin/traits/Curd.php | 23 ++++++++-------- app/common/controller/AdminController.php | 8 +++--- 19 files changed, 96 insertions(+), 109 deletions(-) diff --git a/app/admin/controller/Ajax.php b/app/admin/controller/Ajax.php index c75916d..4910a43 100644 --- a/app/admin/controller/Ajax.php +++ b/app/admin/controller/Ajax.php @@ -109,24 +109,21 @@ class Ajax extends AdminController */ public function getUploadFiles(Request $request): Json { - $get = $request->get(); - $page = !empty($get['page']) ? $get['page'] : 1; - $limit = !empty($get['limit']) ? $get['limit'] : 10; - $title = !empty($get['title']) ? $get['title'] : null; - $this->model = new SystemUploadfile(); - $count = $this->model - ->where(function (Query $query) use ($title) { - !empty($title) && $query->where('original_name', 'like', "%{$title}%"); - }) + $get = $request->get(); + $page = !empty($get['page']) ? $get['page'] : 1; + $limit = !empty($get['limit']) ? $get['limit'] : 10; + $title = !empty($get['title']) ? $get['title'] : null; + $count = SystemUploadfile::where(function(Query $query) use ($title) { + !empty($title) && $query->where('original_name', 'like', "%{$title}%"); + }) ->count(); - $list = $this->model - ->where(function (Query $query) use ($title) { - !empty($title) && $query->where('original_name', 'like', "%{$title}%"); - }) + $list = SystemUploadfile::where(function(Query $query) use ($title) { + !empty($title) && $query->where('original_name', 'like', "%{$title}%"); + }) ->page($page, $limit) ->order($this->sort) ->select()->toArray(); - $data = [ + $data = [ 'code' => 0, 'msg' => '', 'count' => $count, @@ -149,7 +146,7 @@ class Ajax extends AdminController $upload_allow_size = $uploadConfig['upload_allow_size']; $_upload_allow_ext = explode(',', $uploadConfig['upload_allow_ext']); $upload_allow_ext = []; - array_map(function ($value) use (&$upload_allow_ext) { + array_map(function($value) use (&$upload_allow_ext) { $upload_allow_ext[] = '.' . $value; }, $_upload_allow_ext); $config = [ diff --git a/app/admin/controller/mall/Cate.php b/app/admin/controller/mall/Cate.php index 1967be0..519d9f4 100644 --- a/app/admin/controller/mall/Cate.php +++ b/app/admin/controller/mall/Cate.php @@ -15,7 +15,7 @@ class Cate extends AdminController public function __construct(App $app) { parent::__construct($app); - $this->model = new MallCate(); + self::$model = MallCate::class; } } \ No newline at end of file diff --git a/app/admin/controller/mall/Goods.php b/app/admin/controller/mall/Goods.php index ce4b7bc..e17de04 100644 --- a/app/admin/controller/mall/Goods.php +++ b/app/admin/controller/mall/Goods.php @@ -24,8 +24,8 @@ class Goods extends AdminController public function __construct(App $app) { parent::__construct($app); - $this->model = new MallGoods(); - $this->assign('cate', (new MallCate())->column('title', 'id')); + self::$model = MallGoods::class; + $this->assign('cate', MallCate::column('title', 'id')); } #[NodeAnnotation(title: '列表', auth: true)] @@ -34,8 +34,8 @@ class Goods extends AdminController if ($request->isAjax()) { if (input('selectFields')) return $this->selectList(); list($page, $limit, $where) = $this->buildTableParams(); - $count = $this->model->where($where)->count(); - $list = $this->model->with(['cate'])->where($where)->page($page, $limit)->order($this->sort)->select()->toArray(); + $count = self::$model::where($where)->count(); + $list = self::$model::with(['cate'])->where($where)->page($page, $limit)->order($this->sort)->select()->toArray(); $data = [ 'code' => 0, 'msg' => '', @@ -50,7 +50,7 @@ class Goods extends AdminController #[NodeAnnotation(title: '入库', auth: true)] public function stock(Request $request, $id): string { - $row = $this->model->find($id); + $row = self::$model::find($id); empty($row) && $this->error('数据不存在'); if ($request->isPost()) { $post = $request->post(); diff --git a/app/admin/controller/system/Admin.php b/app/admin/controller/system/Admin.php index 44570e6..c746429 100644 --- a/app/admin/controller/system/Admin.php +++ b/app/admin/controller/system/Admin.php @@ -24,8 +24,8 @@ class Admin extends AdminController public function __construct(App $app) { parent::__construct($app); - $this->model = new SystemAdmin(); - $this->assign('auth_list', $this->model->getAuthList()); + self::$model = SystemAdmin::class; + $this->assign('auth_list', self::$model::getAuthList()); } #[NodeAnnotation(title: '列表', auth: true)] @@ -36,11 +36,8 @@ class Admin extends AdminController return $this->selectList(); } list($page, $limit, $where) = $this->buildTableParams(); - $count = $this->model - ->where($where) - ->count(); - $list = $this->model - ->withoutField('password') + $count = self::$model::where($where)->count(); + $list = self::$model::withoutField('password') ->where($where) ->page($page, $limit) ->order($this->sort) @@ -68,7 +65,7 @@ class Admin extends AdminController if (empty($post['password'])) $post['password'] = '123456'; $post['password'] = password($post['password']); try { - $save = $this->model->save($post); + $save = self::$model::create($post); }catch (\Exception $e) { $this->error('保存失败' . $e->getMessage()); } @@ -80,7 +77,7 @@ class Admin extends AdminController #[NodeAnnotation(title: '编辑', auth: true)] public function edit(Request $request, $id = 0): string { - $row = $this->model->find($id); + $row = self::$model::find($id); empty($row) && $this->error('数据不存在'); if ($request->isPost()) { $post = $request->post(); @@ -103,7 +100,7 @@ class Admin extends AdminController #[NodeAnnotation(title: '设置密码', auth: true)] public function password(Request $request, $id): string { - $row = $this->model->find($id); + $row = self::$model::find($id); empty($row) && $this->error('数据不存在'); if ($request->isAjax()) { $post = $request->post(); @@ -124,7 +121,6 @@ class Admin extends AdminController } $save ? $this->success('保存成功') : $this->error('保存失败'); } - $row->auth_ids = explode(',', $row->auth_ids ?: ''); $this->assign('row', $row); return $this->fetch(); } @@ -134,7 +130,7 @@ class Admin extends AdminController { $this->checkPostRequest(); $id = $request->param('id'); - $row = $this->model->whereIn('id', $id)->select(); + $row = self::$model::whereIn('id', $id)->select(); $row->isEmpty() && $this->error('数据不存在'); $id == AdminConstant::SUPER_ADMIN_ID && $this->error('超级管理员不允许修改'); if (is_array($id)) { @@ -167,7 +163,7 @@ class Admin extends AdminController if ($post['id'] == AdminConstant::SUPER_ADMIN_ID && $post['field'] == 'status') { $this->error('超级管理员状态不允许修改'); } - $row = $this->model->find($post['id']); + $row = self::$model::find($post['id']); empty($row) && $this->error('数据不存在'); try { $row->save([ diff --git a/app/admin/controller/system/Auth.php b/app/admin/controller/system/Auth.php index 404c847..1ab1829 100644 --- a/app/admin/controller/system/Auth.php +++ b/app/admin/controller/system/Auth.php @@ -23,16 +23,16 @@ class Auth extends AdminController public function __construct(App $app) { parent::__construct($app); - $this->model = new SystemAuth(); + self::$model = SystemAuth::class; } #[NodeAnnotation(title: '授权', auth: true)] public function authorize(Request $request, $id): string { - $row = $this->model->find($id); + $row = self::$model::find($id); empty($row) && $this->error('数据不存在'); if ($request->isAjax()) { - $list = $this->model->getAuthorizeNodeListByAdminId($id); + $list = self::$model::getAuthorizeNodeListByAdminId($id); $this->success('获取成功', $list); } $this->assign('row', $row); @@ -46,7 +46,7 @@ class Auth extends AdminController $id = $request->post('id'); $node = $request->post('node', "[]"); $node = json_decode($node, true); - $row = $this->model->find($id); + $row = self::$model::find($id); empty($row) && $this->error('数据不存在'); try { $authNode = new SystemAuthNode(); diff --git a/app/admin/controller/system/Config.php b/app/admin/controller/system/Config.php index 6bebcdc..69a62cc 100644 --- a/app/admin/controller/system/Config.php +++ b/app/admin/controller/system/Config.php @@ -18,7 +18,7 @@ class Config extends AdminController public function __construct(App $app) { parent::__construct($app); - $this->model = new SystemConfig(); + self::$model = SystemConfig::class; $this->assign('upload_types', config('admin.upload_types')); $this->assign('editor_types', config('admin.editor_types')); } @@ -41,14 +41,14 @@ class Config extends AdminController if ($group == 'upload') { $upload_types = config('admin.upload_types'); // 兼容旧版本 - $this->model->removeOption()->where('name', 'upload_allow_type')->update(['value' => implode(',', array_keys($upload_types))]); + self::$model::where('name', 'upload_allow_type')->update(['value' => implode(',', array_keys($upload_types))]); } foreach ($post as $key => $val) { if (in_array($key, $notAddFields)) continue; - if ($this->model->removeOption()->where('name', $key)->count()) { - $this->model->removeOption()->where('name', $key)->update(['value' => $val,]); + if (self::$model::where('name', $key)->count()) { + self::$model::where('name', $key)->update(['value' => $val,]); }else { - $this->model->create( + self::$model::create( [ 'name' => $key, 'value' => $val, diff --git a/app/admin/controller/system/Log.php b/app/admin/controller/system/Log.php index 559ab15..5cfd384 100644 --- a/app/admin/controller/system/Log.php +++ b/app/admin/controller/system/Log.php @@ -22,7 +22,7 @@ class Log extends AdminController public function __construct(App $app) { parent::__construct($app); - $this->model = new SystemLog(); + self::$model = SystemLog::class; } #[NodeAnnotation(title: '列表', auth: true)] @@ -34,7 +34,7 @@ class Log extends AdminController } [$page, $limit, $where, $excludeFields] = $this->buildTableParams(['month']); $month = !empty($excludeFields['month']) ? date('Ym', strtotime($excludeFields['month'])) : date('Ym'); - $model = $this->model->setSuffix("_$month")->with('admin')->where($where); + $model = (new self::$model)->setSuffix("_$month")->with('admin')->where($where); try { $count = $model->count(); $list = $model->page($page, $limit)->order($this->sort)->select(); @@ -61,7 +61,7 @@ class Log extends AdminController } [$page, $limit, $where, $excludeFields] = $this->buildTableParams(['month']); $month = !empty($excludeFields['month']) ? date('Ym', strtotime($excludeFields['month'])) : date('Ym'); - $tableName = $this->model->setSuffix("_$month")->getName(); + $tableName = (new self::$model)->setSuffix("_$month")->getName(); $tableName = CommonTool::humpToLine(lcfirst($tableName)); $prefix = config('database.connections.mysql.prefix'); $dbList = Db::query("show full columns from {$prefix}{$tableName}"); @@ -72,7 +72,7 @@ class Log extends AdminController $header[] = [$comment, $vo['Field']]; } } - $model = $this->model->setSuffix("_$month")->with('admin')->where($where); + $model = (new self::$model)->setSuffix("_$month")->with('admin')->where($where); try { $list = $model ->where($where) diff --git a/app/admin/controller/system/Menu.php b/app/admin/controller/system/Menu.php index bc1caab..e5c0caa 100644 --- a/app/admin/controller/system/Menu.php +++ b/app/admin/controller/system/Menu.php @@ -25,7 +25,7 @@ class Menu extends AdminController public function __construct(App $app) { parent::__construct($app); - $this->model = new SystemMenu(); + self::$model = SystemMenu::class; } #[NodeAnnotation(title: '列表', auth: true)] @@ -35,8 +35,8 @@ class Menu extends AdminController if (input('selectFields')) { return $this->selectList(); } - $count = $this->model->count(); - $list = $this->model->order($this->sort)->select()->toArray(); + $count = self::$model::count(); + $list = self::$model::order($this->sort)->select()->toArray(); $data = [ 'code' => 0, 'msg' => '', @@ -52,7 +52,7 @@ class Menu extends AdminController public function add(Request $request): string { $id = $request->param('id'); - $homeId = $this->model->where(['pid' => MenuConstant::HOME_PID,])->value('id'); + $homeId = self::$model::where(['pid' => MenuConstant::HOME_PID,])->value('id'); if ($id == $homeId) { $this->error('首页不能添加子菜单'); } @@ -65,7 +65,7 @@ class Menu extends AdminController ]; $this->validate($post, $rule); try { - $save = $this->model->save($post); + $save = self::$model::save($post); }catch (\Exception $e) { $this->error('保存失败'); } @@ -76,7 +76,7 @@ class Menu extends AdminController $this->error('保存失败'); } } - $pidMenuList = $this->model->getPidMenuList(); + $pidMenuList = self::$model::getPidMenuList(); $this->assign('id', $id); $this->assign('pidMenuList', $pidMenuList); return $this->fetch(); @@ -85,7 +85,7 @@ class Menu extends AdminController #[NodeAnnotation(title: '编辑', auth: true)] public function edit(Request $request, $id = 0): string { - $row = $this->model->find($id); + $row = self::$model::find($id); empty($row) && $this->error('数据不存在'); if ($request->isPost()) { $post = $request->post(); @@ -108,7 +108,7 @@ class Menu extends AdminController $this->error('保存失败'); } } - $pidMenuList = $this->model->getPidMenuList(); + $pidMenuList = self::$model::getPidMenuList(); $this->assign([ 'id' => $id, 'pidMenuList' => $pidMenuList, @@ -122,7 +122,7 @@ class Menu extends AdminController { $this->checkPostRequest(); $id = $request->param('id'); - $row = $this->model->whereIn('id', $id)->select(); + $row = self::$model::whereIn('id', $id)->select(); empty($row) && $this->error('数据不存在'); try { $save = $row->delete(); @@ -148,17 +148,16 @@ class Menu extends AdminController 'value|值' => 'require', ]; $this->validate($post, $rule); - $row = $this->model->find($post['id']); + $row = self::$model::find($post['id']); if (!$row) { $this->error('数据不存在'); } if (!in_array($post['field'], $this->allowModifyFields)) { $this->error('该字段不允许修改:' . $post['field']); } - $homeId = $this->model - ->where([ - 'pid' => MenuConstant::HOME_PID, - ]) + $homeId = self::$model::where([ + 'pid' => MenuConstant::HOME_PID, + ]) ->value('id'); if ($post['id'] == $homeId && $post['field'] == 'status') { $this->error('首页状态不允许关闭'); diff --git a/app/admin/controller/system/Node.php b/app/admin/controller/system/Node.php index 21a144d..3d1fb9e 100644 --- a/app/admin/controller/system/Node.php +++ b/app/admin/controller/system/Node.php @@ -22,7 +22,7 @@ class Node extends AdminController public function __construct(App $app) { parent::__construct($app); - $this->model = new SystemNode(); + self::$model = SystemNode::class; } #[NodeAnnotation(title: '列表', auth: true)] @@ -32,10 +32,8 @@ class Node extends AdminController if (input('selectFields')) { return $this->selectList(); } - $count = $this->model - ->count(); - $list = $this->model - ->getNodeTreeList(); + $count = self::$model::count(); + $list = self::$model::getNodeTreeList(); $data = [ 'code' => 0, 'msg' => '', @@ -54,15 +52,14 @@ class Node extends AdminController $this->checkPostRequest(); $nodeList = (new NodeService())->getNodeList(); empty($nodeList) && $this->error('暂无需要更新的系统节点'); - $model = new SystemNode(); try { if ($force == 1) { - $updateNodeList = $model->removeOption()->whereIn('node', array_column($nodeList, 'node'))->select(); + $updateNodeList = self::$model::whereIn('node', array_column($nodeList, 'node'))->select(); $formatNodeList = array_format_key($nodeList, 'node'); foreach ($updateNodeList as $vo) { isset($formatNodeList[$vo['node']]) - && $model->removeOption()->where('id', $vo['id'])->update( + && self::$model::where('id', $vo['id'])->update( [ 'title' => $formatNodeList[$vo['node']]['title'], 'is_auth' => $formatNodeList[$vo['node']]['is_auth'], @@ -70,7 +67,7 @@ class Node extends AdminController ); } } - $existNodeList = $model->removeOption()->field('node,title,type,is_auth')->select(); + $existNodeList = self::$model::field('node,title,type,is_auth')->select(); foreach ($nodeList as $key => $vo) { foreach ($existNodeList as $v) { if ($vo['node'] == $v->node) { @@ -80,7 +77,7 @@ class Node extends AdminController } } if (!empty($nodeList)) { - $model->saveAll($nodeList); + (new self::$model)->saveAll($nodeList); TriggerService::updateNode(); } }catch (\Exception $e) { @@ -94,12 +91,11 @@ class Node extends AdminController { $this->checkPostRequest(); $nodeList = (new NodeService())->getNodeList(); - $model = new SystemNode(); try { - $existNodeList = $model->field('id,node,title,type,is_auth')->select()->toArray(); + $existNodeList = self::$model::field('id,node,title,type,is_auth')->select()->toArray(); $formatNodeList = array_format_key($nodeList, 'node'); foreach ($existNodeList as $vo) { - !isset($formatNodeList[$vo['node']]) && $model->where('id', $vo['id'])->delete(); + !isset($formatNodeList[$vo['node']]) && self::$model::where('id', $vo['id'])->delete(); } TriggerService::updateNode(); }catch (\Exception $e) { diff --git a/app/admin/controller/system/Quick.php b/app/admin/controller/system/Quick.php index 8e6efef..14f4b92 100644 --- a/app/admin/controller/system/Quick.php +++ b/app/admin/controller/system/Quick.php @@ -21,7 +21,7 @@ class Quick extends AdminController public function __construct(App $app) { parent::__construct($app); - $this->model = new SystemQuick(); + self::$model = SystemQuick::class; } } \ No newline at end of file diff --git a/app/admin/controller/system/Uploadfile.php b/app/admin/controller/system/Uploadfile.php index 40d5a66..866805a 100644 --- a/app/admin/controller/system/Uploadfile.php +++ b/app/admin/controller/system/Uploadfile.php @@ -15,7 +15,7 @@ class Uploadfile extends AdminController public function __construct(App $app) { parent::__construct($app); - $this->model = new SystemUploadfile(); + self::$model = SystemUploadfile::class; $this->assign('upload_types', config('admin.upload_types')); } diff --git a/app/admin/model/SystemAdmin.php b/app/admin/model/SystemAdmin.php index 2388e8a..d1e9ee5 100644 --- a/app/admin/model/SystemAdmin.php +++ b/app/admin/model/SystemAdmin.php @@ -22,15 +22,15 @@ class SystemAdmin extends TimeModel ], ]; - public function getAuthIdsAttr($value): array + public static function getAuthIdsAttr($value): array { if (!$value) return []; return explode(',', $value); } - public function getAuthList(): array + public static function getAuthList(): array { - return (new SystemAuth())->removeOption()->where('status', 1)->column('title', 'id'); + return SystemAuth::where('status', 1)->column('title', 'id'); } } \ No newline at end of file diff --git a/app/admin/model/SystemAuth.php b/app/admin/model/SystemAuth.php index 2657e70..775c590 100644 --- a/app/admin/model/SystemAuth.php +++ b/app/admin/model/SystemAuth.php @@ -25,13 +25,13 @@ class SystemAuth extends TimeModel * @throws DbException * @throws ModelNotFoundException */ - public function getAuthorizeNodeListByAdminId($authId): array + public static function getAuthorizeNodeListByAdminId($authId): array { $checkNodeList = (new SystemAuthNode()) ->where('auth_id', $authId) ->column('node_id'); $systemNode = new SystemNode(); - $nodeList = $systemNode + $nodeList = $systemNode ->where('is_auth', 1) ->field('id,node,title,type,is_auth') ->select() diff --git a/app/admin/model/SystemMenu.php b/app/admin/model/SystemMenu.php index d34344e..3340756 100644 --- a/app/admin/model/SystemMenu.php +++ b/app/admin/model/SystemMenu.php @@ -23,14 +23,14 @@ class SystemMenu extends TimeModel * @throws DbException * @throws DataNotFoundException */ - public function getPidMenuList(): array + public static function getPidMenuList(): array { - $list = $this->removeOption()->field('id,pid,title')->where([ + $list = self::field('id,pid,title')->where([ ['pid', '<>', MenuConstant::HOME_PID], ['status', '=', 1], ])->select()->toArray(); - $pidMenuList = $this->buildPidMenu(0, $list); + $pidMenuList = self::buildPidMenu(0, $list); return array_merge([[ 'id' => 0, 'pid' => 0, @@ -38,7 +38,7 @@ class SystemMenu extends TimeModel ]], $pidMenuList); } - protected function buildPidMenu($pid, $list, $level = 0): array + protected static function buildPidMenu($pid, $list, $level = 0): array { $newList = []; foreach ($list as $vo) { @@ -57,7 +57,7 @@ class SystemMenu extends TimeModel $vo['title'] = $markString . $vo['title']; } $newList[] = $vo; - $childList = $this->buildPidMenu($vo['id'], $list, $level); + $childList = self::buildPidMenu($vo['id'], $list, $level); !empty($childList) && $newList = array_merge($newList, $childList); } diff --git a/app/admin/model/SystemNode.php b/app/admin/model/SystemNode.php index 9b60aae..0ff2798 100644 --- a/app/admin/model/SystemNode.php +++ b/app/admin/model/SystemNode.php @@ -7,13 +7,13 @@ use app\common\model\TimeModel; class SystemNode extends TimeModel { - public function getNodeTreeList(): array + public static function getNodeTreeList(): array { - $list = $this->removeOption()->select()->toArray(); - return $this->buildNodeTree($list); + $list = self::select()->toArray(); + return self::buildNodeTree($list); } - protected function buildNodeTree($list): array + protected static function buildNodeTree($list): array { $newList = []; $repeatString = "      "; diff --git a/app/admin/service/curd/templates/controller/controller.code b/app/admin/service/curd/templates/controller/controller.code index 4adae00..009a7d3 100644 --- a/app/admin/service/curd/templates/controller/controller.code +++ b/app/admin/service/curd/templates/controller/controller.code @@ -16,8 +16,8 @@ class {{controllerName}} extends AdminController public function __construct(App $app) { parent::__construct($app); - $this->model = new {{modelFilename}}(); - $this->notes = $notes = $this->model->notes; + self::$model = {{modelFilename}}::class; + $this->notes = $notes = self::$model::$notes; {{constructRelation}} $this->assign(compact('notes')); } diff --git a/app/admin/service/curd/templates/model/model.code b/app/admin/service/curd/templates/model/model.code index 1e05b0d..25ea98a 100644 --- a/app/admin/service/curd/templates/model/model.code +++ b/app/admin/service/curd/templates/model/model.code @@ -16,6 +16,6 @@ class {{modelName}} extends TimeModel ]; } - public array $notes = {{selectArrays}}; + public static array $notes = {{selectArrays}}; } \ No newline at end of file diff --git a/app/admin/traits/Curd.php b/app/admin/traits/Curd.php index 714db7d..6d4436e 100644 --- a/app/admin/traits/Curd.php +++ b/app/admin/traits/Curd.php @@ -25,8 +25,8 @@ trait Curd return $this->selectList(); } list($page, $limit, $where) = $this->buildTableParams(); - $count = $this->model->where($where)->count(); - $list = $this->model->where($where)->page($page, $limit)->order($this->sort)->select()->toArray(); + $count = self::$model::where($where)->count(); + $list = self::$model::where($where)->page($page, $limit)->order($this->sort)->select()->toArray(); $data = [ 'code' => 0, 'msg' => '', @@ -46,8 +46,8 @@ trait Curd $rule = []; $this->validate($post, $rule); try { - Db::transaction(function () use ($post, &$save) { - $save = $this->model->save($post); + Db::transaction(function() use ($post, &$save) { + $save = self::$model::create($post); }); }catch (\Exception $e) { $this->error('新增失败:' . $e->getMessage()); @@ -60,14 +60,14 @@ trait Curd #[NodeAnnotation(title: '编辑', auth: true)] public function edit(Request $request, $id = 0): string { - $row = $this->model->find($id); + $row = self::$model::find($id); empty($row) && $this->error('数据不存在'); if ($request->isPost()) { $post = $request->post(); $rule = []; $this->validate($post, $rule); try { - Db::transaction(function () use ($post, $row, &$save) { + Db::transaction(function() use ($post, $row, &$save) { $save = $row->save($post); }); }catch (\Exception $e) { @@ -85,7 +85,7 @@ trait Curd // 如果不是id作为主键 请在对应的控制器中覆盖重写 $id = $request->param('id', []); $this->checkPostRequest(); - $row = $this->model->whereIn('id', $id)->select(); + $row = self::$model::whereIn('id', $id)->select(); $row->isEmpty() && $this->error('数据不存在'); try { $save = $row->delete(); @@ -102,7 +102,7 @@ trait Curd $this->error('演示环境下不允许操作'); } list($page, $limit, $where) = $this->buildTableParams(); - $tableName = $this->model->getName(); + $tableName = (new self::$model)->getName(); $tableName = CommonTool::humpToLine(lcfirst($tableName)); $prefix = config('database.connections.mysql.prefix'); $dbList = Db::query("show full columns from {$prefix}{$tableName}"); @@ -113,8 +113,7 @@ trait Curd $header[] = [$comment, $vo['Field']]; } } - $list = $this->model - ->where($where) + $list = self::$model::where($where) ->limit(100000) ->order('id', 'desc') ->select() @@ -134,7 +133,7 @@ trait Curd 'value|值' => 'require', ]; $this->validate($post, $rule); - $row = $this->model->find($post['id']); + $row = self::$model::find($post['id']); if (!$row) { $this->error('数据不存在'); } @@ -142,7 +141,7 @@ trait Curd $this->error('该字段不允许修改:' . $post['field']); } try { - Db::transaction(function () use ($post, $row) { + Db::transaction(function() use ($post, $row) { $row->save([ $post['field'] => $post['value'], ]); diff --git a/app/common/controller/AdminController.php b/app/common/controller/AdminController.php index ab2295c..a08e6a2 100644 --- a/app/common/controller/AdminController.php +++ b/app/common/controller/AdminController.php @@ -19,9 +19,9 @@ class AdminController extends BaseController /** * 当前模型 * @Model - * @var object + * @var mixed */ - protected object $model; + protected static mixed $model; /** * 字段排序 @@ -172,7 +172,7 @@ class AdminController extends BaseController $where = []; $excludes = []; // 判断是否关联查询 - $tableName = Str::snake(lcfirst($this->model->getName())); + $tableName = Str::snake(lcfirst((new self::$model)->getName())); foreach ($filters as $key => $val) { if (in_array($key, $excludeFields)) { $excludes[$key] = $val; @@ -218,7 +218,7 @@ class AdminController extends BaseController public function selectList(): Json { $fields = input('selectFields'); - $data = $this->model->where($this->selectWhere)->field($fields)->select()->toArray(); + $data = self::$model::where($this->selectWhere)->field($fields)->select()->toArray(); $this->success(null, $data); }