Menu.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\system\auth;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\system\auth\MenuRepository;
  14. use app\validate\admin\MenuValidate;
  15. use FormBuilder\Exception\FormBuilderException;
  16. use think\App;
  17. use think\db\exception\DataNotFoundException;
  18. use think\db\exception\DbException;
  19. use think\db\exception\ModelNotFoundException;
  20. /**
  21. * Class Menu
  22. * @package app\controller\admin\system\menu
  23. * @author xaboy
  24. * @day 2020-04-08
  25. */
  26. class Menu extends BaseController
  27. {
  28. /**
  29. * @var int
  30. */
  31. protected $merchant;
  32. /**
  33. * @var MenuRepository
  34. */
  35. protected $repository;
  36. /**
  37. * Menu constructor.
  38. * @param App $app
  39. * @param MenuRepository $repository
  40. */
  41. public function __construct(App $app, MenuRepository $repository)
  42. {
  43. parent::__construct($app);
  44. $this->repository = $repository;
  45. $this->merchant = $this->request->param('merchant', 0) == 0 ? 0 : 1;
  46. }
  47. /**
  48. * @return mixed
  49. * @throws DataNotFoundException
  50. * @throws DbException
  51. * @throws ModelNotFoundException
  52. * @author xaboy
  53. * @day 2020-04-08
  54. */
  55. public function getList()
  56. {
  57. $data = $this->repository->getList([], $this->merchant);
  58. $data['list'] = formatCategory($data['list'], 'menu_id');
  59. return app('json')->success($data);
  60. }
  61. /**
  62. * 创建
  63. * @param MenuValidate $validate
  64. * @return mixed
  65. * @author 张先生
  66. * @date 2020-03-30
  67. */
  68. public function create(MenuValidate $validate)
  69. {
  70. $data = $this->checkParam($validate);
  71. if (!$data['pid']) $data['pid'] = 0;
  72. if ($data['pid'] && !$this->repository->merExists($data['pid'], $this->merchant))
  73. return app('json')->fail('父级分类不存在');
  74. $data['is_mer'] = $this->merchant;
  75. $this->repository->create($data);
  76. return app('json')->success('添加成功');
  77. }
  78. /**
  79. * 更新
  80. * @param int $id id
  81. * @param MenuValidate $validate
  82. * @return mixed
  83. * @throws DbException
  84. * @author 张先生
  85. * @date 2020-03-30
  86. */
  87. public function update($id, MenuValidate $validate)
  88. {
  89. $data = $this->checkParam($validate);
  90. if (!$data['pid']) $data['pid'] = 0;
  91. if (!$this->repository->merExists($id, $this->merchant))
  92. return app('json')->fail('数据不存在');
  93. if ($data['pid'] && !$this->repository->merExists($data['pid'], $this->merchant))
  94. return app('json')->fail('父级分类不存在');
  95. $this->repository->update($id, $data);
  96. return app('json')->success('编辑成功');
  97. }
  98. /**
  99. * @return mixed
  100. * @throws FormBuilderException
  101. * @author xaboy
  102. * @day 2020-04-08
  103. */
  104. public function createForm()
  105. {
  106. return app('json')->success(formToData($this->repository->menuForm($this->merchant)));
  107. }
  108. /**
  109. * @param int $id
  110. * @return mixed
  111. * @throws DataNotFoundException
  112. * @throws DbException
  113. * @throws ModelNotFoundException
  114. * @throws FormBuilderException
  115. * @author xaboy
  116. * @day 2020-04-08
  117. */
  118. public function updateForm($id)
  119. {
  120. if (!$this->repository->merExists($id, $this->merchant))
  121. return app('json')->fail('数据不存在');
  122. return app('json')->success(formToData($this->repository->updateMenuForm($id, $this->merchant)));
  123. }
  124. /**
  125. *
  126. * @param MenuValidate $validate 验证规则
  127. * @return mixed
  128. * @author 张先生
  129. * @date 2020-03-30
  130. */
  131. private function checkParam(MenuValidate $validate)
  132. {
  133. $data = $this->request->params([['pid', 0], 'icon', 'menu_name', 'route', ['params', '[]'], 'sort', ['is_show', 0], ['is_menu', 0]]);
  134. if ($data['is_menu'] != 1) $validate->isAuth();
  135. $validate->check($data);
  136. return $data;
  137. }
  138. /**
  139. * @param int $id
  140. * @return mixed
  141. * @throws DbException
  142. * @author xaboy
  143. * @day 2020-04-08
  144. */
  145. public function delete($id)
  146. {
  147. if ($this->repository->pidExists($id))
  148. return app('json')->fail('存在下级,无法删除');
  149. $this->repository->delete($id, $this->merchant);
  150. return app('json')->success('删除成功');
  151. }
  152. /**
  153. * @return mixed
  154. * @author xaboy
  155. * @day 2020-04-10
  156. */
  157. public function menus()
  158. {
  159. $pre = '/' . config('admin.' . ($this->merchant ? 'merchant' : 'admin') . '_prefix');
  160. $menus = $this->request->adminInfo()->level
  161. ? $this->repository->ruleByMenuList($this->request->adminRule(), $this->merchant)
  162. : $this->repository->getValidMenuList($this->merchant);
  163. foreach ($menus as $k => $menu) {
  164. $menu['route'] = $pre . $menu['route'];
  165. $menus[$k] = $menu;
  166. }
  167. return app('json')->success(array_values(array_filter(formatCategory($menus, 'menu_id'), function ($v) {
  168. return 0 == $v['pid'];
  169. })));
  170. }
  171. /**
  172. * @return mixed
  173. * @author xaboy
  174. * @day 2020-04-10
  175. */
  176. public function merchantMenus()
  177. {
  178. $pre = '/' . config('admin.merchant_prefix');
  179. $merchant = $this->request->merchant();
  180. $menus = $this->request->adminInfo()->level
  181. ? $this->repository->ruleByMenuList($this->request->adminRule(), $this->merchant)
  182. : ($merchant->type_id ? $this->repository->typesByValidMenuList($merchant->type_id) : $this->repository->getValidMenuList($this->merchant));
  183. foreach ($menus as $k => $menu) {
  184. $menu['route'] = $pre . $menu['route'];
  185. $menus[$k] = $menu;
  186. }
  187. return app('json')->success(array_values(array_filter(formatCategory($menus, 'menu_id'), function ($v) {
  188. return 0 == $v['pid'];
  189. })));
  190. }
  191. }