123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- namespace app\controller\admin\system\auth;
- use ln\basic\BaseController;
- use app\common\repositories\system\auth\MenuRepository;
- use app\validate\admin\MenuValidate;
- use FormBuilder\Exception\FormBuilderException;
- use think\App;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- /**
- * Class Menu
- * @package app\controller\admin\system\menu
- * @author zfy
- * @day 2020-04-08
- */
- class Menu extends BaseController
- {
- /**
- * @var int
- */
- protected $merchant;
- /**
- * @var MenuRepository
- */
- protected $repository;
- /**
- * Menu constructor.
- * @param App $app
- * @param MenuRepository $repository
- */
- public function __construct(App $app, MenuRepository $repository)
- {
- parent::__construct($app);
- $this->repository = $repository;
- $this->merchant = $this->request->param('merchant', 0) == 0 ? 0 : 1;
- }
- /**
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @author zfy
- * @day 2020-04-08
- */
- public function getList()
- {
- $data = $this->repository->getList([], $this->merchant);
- $data['list'] = formatCategory($data['list'], 'menu_id');
- return app('json')->success($data);
- }
- /**
- * 创建
- * @param MenuValidate $validate
- * @return mixed
- * @author 张先生
- * @date 2020-03-30
- */
- public function create(MenuValidate $validate)
- {
- $data = $this->checkParam($validate);
- if (!$data['pid']) $data['pid'] = 0;
- if ($data['pid'] && !$this->repository->merExists($data['pid'], $this->merchant))
- return app('json')->fail('父级分类不存在');
- $data['is_mer'] = $this->merchant;
- $this->repository->create($data);
- return app('json')->success('添加成功');
- }
- /**
- * 更新
- * @param int $id id
- * @param MenuValidate $validate
- * @return mixed
- * @throws DbException
- * @author 张先生
- * @date 2020-03-30
- */
- public function update($id, MenuValidate $validate)
- {
- $data = $this->checkParam($validate);
- if (!$data['pid']) $data['pid'] = 0;
- if (!$this->repository->merExists($id, $this->merchant))
- return app('json')->fail('数据不存在');
- if ($data['pid'] && !$this->repository->merExists($data['pid'], $this->merchant))
- return app('json')->fail('父级分类不存在');
- $this->repository->update($id, $data);
- return app('json')->success('编辑成功');
- }
- /**
- * @return mixed
- * @throws FormBuilderException
- * @author zfy
- * @day 2020-04-08
- */
- public function createForm()
- {
- return app('json')->success(formToData($this->repository->menuForm($this->merchant)));
- }
- /**
- * @param int $id
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @throws FormBuilderException
- * @author zfy
- * @day 2020-04-08
- */
- public function updateForm($id)
- {
- if (!$this->repository->merExists($id, $this->merchant))
- return app('json')->fail('数据不存在');
- return app('json')->success(formToData($this->repository->updateMenuForm($id, $this->merchant)));
- }
- /**
- *
- * @param MenuValidate $validate 验证规则
- * @return mixed
- * @author 张先生
- * @date 2020-03-30
- */
- private function checkParam(MenuValidate $validate)
- {
- $data = $this->request->params([['pid', 0], 'icon', 'menu_name', 'route', ['params', '[]'], 'sort', ['is_show', 0], ['is_menu', 0]]);
- if ($data['is_menu'] != 1) $validate->isAuth();
- $validate->check($data);
- return $data;
- }
- /**
- * @param int $id
- * @return mixed
- * @throws DbException
- * @author zfy
- * @day 2020-04-08
- */
- public function delete($id)
- {
- if ($this->repository->pidExists($id))
- return app('json')->fail('存在下级,无法删除');
- $this->repository->delete($id, $this->merchant);
- return app('json')->success('删除成功');
- }
- /**
- * @return mixed
- * @author zfy
- * @day 2020-04-10
- */
- public function menus()
- {
- $pre = '/' . config('admin.' . ($this->merchant ? 'merchant' : 'admin') . '_prefix');
- $menus = $this->request->adminInfo()->level
- ? $this->repository->ruleByMenuList($this->request->adminRule(), $this->merchant)
- : $this->repository->getValidMenuList($this->merchant);
- foreach ($menus as $k => $menu) {
- $menu['route'] = $pre . $menu['route'];
- $menus[$k] = $menu;
- }
- return app('json')->success(array_values(array_filter(formatCategory($menus, 'menu_id'), function ($v) {
- return 0 == $v['pid'];
- })));
- }
- }
|