From 0251d52567819c022336e22d0c058b10e5a26678 Mon Sep 17 00:00:00 2001 From: lqmac Date: Mon, 3 Jun 2024 16:55:54 +0800 Subject: [PATCH] 1 --- app/admin/controller/Channel.php | 124 +++++++++++++++++++++++++++++++ app/admin/model/Channel.php | 116 +++++++++++++++++++++++++++++ app/common/model/Channel.php | 15 ++++ 3 files changed, 255 insertions(+) create mode 100644 app/admin/controller/Channel.php create mode 100644 app/admin/model/Channel.php diff --git a/app/admin/controller/Channel.php b/app/admin/controller/Channel.php new file mode 100644 index 00000000..c0b7db46 --- /dev/null +++ b/app/admin/controller/Channel.php @@ -0,0 +1,124 @@ + +// +---------------------------------------------------------------------- +declare (strict_types=1); + +namespace app\admin\controller; + +use think\response\Json; +use app\admin\controller\Controller; +use app\admin\model\Channel as ChannelModel; + +/** + * 商家后台菜单控制器 + * Class Menu + * @package app\store\controller + */ +class Channel extends Controller +{ + /** + * 菜单列表 + * @return Json + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function index(): Json + { + $model = new ChannelModel; + $list = $model->getList(); + return $this->renderSuccess(compact('list')); + } + + /** + * 菜单详情 + * @param int $menuId + * @return Json + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function info(int $id): Json + { + // 菜单详情 + $model = ChannelModel::detail($id); + return $this->renderSuccess(['info' => $model]); + } + + /** + * 新增菜单 + * @return Json + */ + public function add(): Json + { + // 新增记录 + $model = new ChannelModel; + if ($model->add($this->postForm())) { + return $this->renderSuccess('添加成功'); + } + return $this->renderError($model->getError() ?: '添加失败'); + } + + /** + * 编辑菜单 + * @param $menuId + * @return Json + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function edit(int $id): Json + { + // 菜单详情 + $model = ChannelModel::detail($id); + // 更新记录 + if ($model->edit($this->postForm())) { + return $this->renderSuccess('更新成功'); + } + return $this->renderError($model->getError() ?: '更新失败'); + } + + /** + * 设置菜单绑定的Api + * @param int $menuId + * @return Json + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function setApis(int $menuId): Json + { + // 菜单详情 + $model = MenuModel::detail($menuId); + // 更新记录 + if ($model->setApis($this->postForm())) { + return $this->renderSuccess('操作成功'); + } + return $this->renderError($model->getError() ?: '操作失败'); + } + + /** + * 删除菜单 + * @param int $menuId + * @return Json + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function delete(int $menuId): Json + { + // 菜单详情 + $model = MenuModel::detail($menuId); + if (!$model->remove()) { + return $this->renderError($model->getError() ?: '删除失败'); + } + return $this->renderSuccess('删除成功'); + } +} diff --git a/app/admin/model/Channel.php b/app/admin/model/Channel.php new file mode 100644 index 00000000..1c124d35 --- /dev/null +++ b/app/admin/model/Channel.php @@ -0,0 +1,116 @@ + +// +---------------------------------------------------------------------- +declare (strict_types=1); + +namespace app\admin\model; + +use app\common\model\Channel as ChannelModel; + +/** + * 商家后台菜单模型 + * Class Menu + * @package app\admin\model\store + */ +class Channel extends ChannelModel +{ + /** + * 获取列表数据 + * @param bool $isRecycle + * @return \think\Paginator + * @throws \think\db\exception\DbException + */ + public function getList(bool $isRecycle = false): \think\Paginator + { + return $this->where('status', '=', 1) + ->order(['weigh' => 'asc', 'create_time' => 'desc']) + ->paginate(15); + } + + + /** + * 新增记录 + * @param array $data + * @return bool + */ + public function add(array $data): bool + { + return $this->save($data); + } + + /** + * 更新记录 + * @param array $data + * @return bool + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function edit(array $data): bool + { + + return $this->save($data); + } + + /** + * 设置菜单的API权限 + * @param array $data + * @return bool + */ + public function setApis(array $data): bool + { + if (empty($data['apiIds'])) { + $this->error = 'API权限不能为空'; + return false; + } + // 根据菜单id批量更新API关联记录 + return (new MenuApiModel)->updateByMenuId($this['menu_id'], $data['apiIds']); + } + + /** + * 删除菜单 + * @return bool + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @throws \Exception + */ + public function remove(): bool + { + // 判断是否存在下级菜单 + if (self::detail(['parent_id' => $this['menu_id']])) { + $this->error = '当前菜单下存在子菜单或操作,请先删除'; + return false; + } + return $this->delete(); + } + + /** + * 获取所有上级id集 + * @param int $menuId + * @param null $menuList + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + private function getTopMenuIds(int $menuId, $menuList = null): array + { + static $ids = []; + is_null($menuList) && $menuList = $this->getAll(); + foreach ($menuList as $item) { + if ($item['menu_id'] == $menuId && $item['parent_id'] > 0) { + $ids[] = $item['parent_id']; + $this->getTopMenuIds($item['parent_id'], $menuList); + } + } + return $ids; + } +} diff --git a/app/common/model/Channel.php b/app/common/model/Channel.php index 65d21c65..abc355f9 100644 --- a/app/common/model/Channel.php +++ b/app/common/model/Channel.php @@ -32,4 +32,19 @@ class Channel extends BaseModel // self::$storeId = 0; // app()->request->setStoreId(0); } + + /** + * 菜单信息 + * @param int|array $where + * @return static|array|null + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public static function detail($where) + { + $query = static::withoutGlobalScope(); + is_array($where) ? $query->where($where) : $query->where('id', '=', $where); + return $query->find(); + } }