Menu.php 7.1 KB

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