Menu.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\controller\admin\system\auth;
  3. use ln\basic\BaseController;
  4. use app\common\repositories\system\auth\MenuRepository;
  5. use app\validate\admin\MenuValidate;
  6. use FormBuilder\Exception\FormBuilderException;
  7. use think\App;
  8. use think\db\exception\DataNotFoundException;
  9. use think\db\exception\DbException;
  10. use think\db\exception\ModelNotFoundException;
  11. /**
  12. * Class Menu
  13. * @package app\controller\admin\system\menu
  14. * @author zfy
  15. * @day 2020-04-08
  16. */
  17. class Menu extends BaseController
  18. {
  19. /**
  20. * @var int
  21. */
  22. protected $merchant;
  23. /**
  24. * @var MenuRepository
  25. */
  26. protected $repository;
  27. /**
  28. * Menu constructor.
  29. * @param App $app
  30. * @param MenuRepository $repository
  31. */
  32. public function __construct(App $app, MenuRepository $repository)
  33. {
  34. parent::__construct($app);
  35. $this->repository = $repository;
  36. $this->merchant = $this->request->param('merchant', 0) == 0 ? 0 : 1;
  37. }
  38. /**
  39. * @return mixed
  40. * @throws DataNotFoundException
  41. * @throws DbException
  42. * @throws ModelNotFoundException
  43. * @author zfy
  44. * @day 2020-04-08
  45. */
  46. public function getList()
  47. {
  48. $data = $this->repository->getList([], $this->merchant);
  49. $data['list'] = formatCategory($data['list'], 'menu_id');
  50. return app('json')->success($data);
  51. }
  52. /**
  53. * 创建
  54. * @param MenuValidate $validate
  55. * @return mixed
  56. * @author 张先生
  57. * @date 2020-03-30
  58. */
  59. public function create(MenuValidate $validate)
  60. {
  61. $data = $this->checkParam($validate);
  62. if (!$data['pid']) $data['pid'] = 0;
  63. if ($data['pid'] && !$this->repository->merExists($data['pid'], $this->merchant))
  64. return app('json')->fail('父级分类不存在');
  65. $data['is_mer'] = $this->merchant;
  66. $this->repository->create($data);
  67. return app('json')->success('添加成功');
  68. }
  69. /**
  70. * 更新
  71. * @param int $id id
  72. * @param MenuValidate $validate
  73. * @return mixed
  74. * @throws DbException
  75. * @author 张先生
  76. * @date 2020-03-30
  77. */
  78. public function update($id, MenuValidate $validate)
  79. {
  80. $data = $this->checkParam($validate);
  81. if (!$data['pid']) $data['pid'] = 0;
  82. if (!$this->repository->merExists($id, $this->merchant))
  83. return app('json')->fail('数据不存在');
  84. if ($data['pid'] && !$this->repository->merExists($data['pid'], $this->merchant))
  85. return app('json')->fail('父级分类不存在');
  86. $this->repository->update($id, $data);
  87. return app('json')->success('编辑成功');
  88. }
  89. /**
  90. * @return mixed
  91. * @throws FormBuilderException
  92. * @author zfy
  93. * @day 2020-04-08
  94. */
  95. public function createForm()
  96. {
  97. return app('json')->success(formToData($this->repository->menuForm($this->merchant)));
  98. }
  99. /**
  100. * @param int $id
  101. * @return mixed
  102. * @throws DataNotFoundException
  103. * @throws DbException
  104. * @throws ModelNotFoundException
  105. * @throws FormBuilderException
  106. * @author zfy
  107. * @day 2020-04-08
  108. */
  109. public function updateForm($id)
  110. {
  111. if (!$this->repository->merExists($id, $this->merchant))
  112. return app('json')->fail('数据不存在');
  113. return app('json')->success(formToData($this->repository->updateMenuForm($id, $this->merchant)));
  114. }
  115. /**
  116. *
  117. * @param MenuValidate $validate 验证规则
  118. * @return mixed
  119. * @author 张先生
  120. * @date 2020-03-30
  121. */
  122. private function checkParam(MenuValidate $validate)
  123. {
  124. $data = $this->request->params([['pid', 0], 'icon', 'menu_name', 'route', ['params', '[]'], 'sort', ['is_show', 0], ['is_menu', 0]]);
  125. if ($data['is_menu'] != 1) $validate->isAuth();
  126. $validate->check($data);
  127. return $data;
  128. }
  129. /**
  130. * @param int $id
  131. * @return mixed
  132. * @throws DbException
  133. * @author zfy
  134. * @day 2020-04-08
  135. */
  136. public function delete($id)
  137. {
  138. if ($this->repository->pidExists($id))
  139. return app('json')->fail('存在下级,无法删除');
  140. $this->repository->delete($id, $this->merchant);
  141. return app('json')->success('删除成功');
  142. }
  143. /**
  144. * @return mixed
  145. * @author zfy
  146. * @day 2020-04-10
  147. */
  148. public function menus()
  149. {
  150. $pre = '/' . config('admin.' . ($this->merchant ? 'merchant' : 'admin') . '_prefix');
  151. $menus = $this->request->adminInfo()->level
  152. ? $this->repository->ruleByMenuList($this->request->adminRule(), $this->merchant)
  153. : $this->repository->getValidMenuList($this->merchant);
  154. foreach ($menus as $k => $menu) {
  155. $menu['route'] = $pre . $menu['route'];
  156. $menus[$k] = $menu;
  157. }
  158. return app('json')->success(array_values(array_filter(formatCategory($menus, 'menu_id'), function ($v) {
  159. return 0 == $v['pid'];
  160. })));
  161. }
  162. }