SystemMenus.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\setting;
  12. use app\adminapi\controller\AuthController;
  13. use app\services\system\SystemMenusServices;
  14. use app\services\system\SystemRouteCateServices;
  15. use app\services\system\SystemRouteServices;
  16. use think\facade\App;
  17. use think\facade\Route;
  18. /**
  19. * 菜单权限
  20. * Class SystemMenus
  21. * @package app\adminapi\controller\v1\setting
  22. */
  23. class SystemMenus extends AuthController
  24. {
  25. /**
  26. * SystemMenus constructor.
  27. * @param App $app
  28. * @param SystemMenusServices $services
  29. */
  30. public function __construct(App $app, SystemMenusServices $services)
  31. {
  32. parent::__construct($app);
  33. $this->services = $services;
  34. $this->request->filter(['addslashes', 'trim']);
  35. }
  36. /**
  37. * 菜单展示列表
  38. * @return \think\Response
  39. */
  40. public function index()
  41. {
  42. $where = $this->request->getMore([
  43. ['is_show', ''],
  44. ['keyword', ''],
  45. ]);
  46. return app('json')->success($this->services->getList($where));
  47. }
  48. /**
  49. * @return \think\Response
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. * @author 等风来
  54. * @email 136327134@qq.com
  55. * @date 2023/4/14
  56. */
  57. public function unique()
  58. {
  59. $adminInfo = $this->request->adminInfo();
  60. [$menus, $uniqueAuth] = app()->make(SystemMenusServices::class)->getMenusList($adminInfo['roles'], (int)$adminInfo['level']);
  61. return app('json')->success(compact('menus', 'uniqueAuth'));
  62. }
  63. /**
  64. * 显示创建资源表单页.
  65. *
  66. * @return \think\Response
  67. */
  68. public function create()
  69. {
  70. return app('json')->success($this->services->createMenus());
  71. }
  72. /**
  73. * 保存菜单权限
  74. * @return mixed
  75. */
  76. public function save()
  77. {
  78. $data = $this->request->getMore([
  79. ['menu_name', ''],
  80. ['controller', ''],
  81. ['module', 'admin'],
  82. ['action', ''],
  83. ['icon', ''],
  84. ['params', ''],
  85. ['path', []],
  86. ['menu_path', ''],
  87. ['api_url', ''],
  88. ['methods', ''],
  89. ['unique_auth', ''],
  90. ['header', ''],
  91. ['is_header', 0],
  92. ['pid', 0],
  93. ['sort', 0],
  94. ['auth_type', 0],
  95. ['access', 1],
  96. ['is_show', 0],
  97. ['is_show_path', 0],
  98. ]);
  99. if (!$data['menu_name'])
  100. return app('json')->fail(400198);
  101. $data['path'] = implode('/', $data['path']);
  102. if ($this->services->save($data)) {
  103. return app('json')->success(100021);
  104. } else {
  105. return app('json')->fail(100022);
  106. }
  107. }
  108. /**
  109. * 批量保存权限
  110. * @return \think\Response
  111. * @author 等风来
  112. * @email 136327134@qq.com
  113. * @date 2023/4/11
  114. */
  115. public function batchSave()
  116. {
  117. $menus = $this->request->post('menus', []);
  118. if (!$menus) {
  119. return app('json')->fail(100026);
  120. }
  121. $data = [];
  122. foreach ($menus as $menu) {
  123. if (empty($menu['menu_name'])) {
  124. return app('json')->fail(400198);
  125. }
  126. $data[] = [
  127. 'methods' => $menu['method'],
  128. 'menu_name' => $menu['menu_name'],
  129. 'unique_auth' => $menu['unique_auth'] ?? '',
  130. 'api_url' => $menu['api_url'],
  131. 'pid' => $menu['path'],
  132. 'auth_type' => 2,
  133. 'is_show' => 0,
  134. ];
  135. }
  136. $this->services->saveAll($data);
  137. return app('json')->success(100021);
  138. }
  139. /**
  140. * 获取一条菜单权限信息
  141. * @param int $id
  142. * @return \think\Response
  143. */
  144. public function read($id)
  145. {
  146. if (!$id) {
  147. return app('json')->fail(100026);
  148. }
  149. return app('json')->success($this->services->find((int)$id));
  150. }
  151. /**
  152. * 修改菜单权限表单获取
  153. * @param int $id
  154. * @return \think\Response
  155. */
  156. public function edit($id)
  157. {
  158. if (!$id) {
  159. return app('json')->fail(100100);
  160. }
  161. return app('json')->success($this->services->updateMenus((int)$id));
  162. }
  163. /**
  164. * 修改菜单
  165. * @param $id
  166. * @return mixed
  167. */
  168. public function update($id)
  169. {
  170. if (!$id || !($menu = $this->services->get($id)))
  171. return app('json')->fail(100026);
  172. $data = $this->request->postMore([
  173. 'menu_name',
  174. 'controller',
  175. ['module', 'admin'],
  176. 'action',
  177. 'params',
  178. ['icon', ''],
  179. ['menu_path', ''],
  180. ['api_url', ''],
  181. ['methods', ''],
  182. ['unique_auth', ''],
  183. ['path', []],
  184. ['sort', 0],
  185. ['pid', 0],
  186. ['is_header', 0],
  187. ['header', ''],
  188. ['auth_type', 0],
  189. ['access', 1],
  190. ['is_show', 0],
  191. ['is_show_path', 0],
  192. ]);
  193. if (!$data['menu_name'])
  194. return app('json')->fail(400198);
  195. $data['path'] = implode('/', $data['path']);
  196. if ($this->services->update($id, $data))
  197. return app('json')->success(100001);
  198. else
  199. return app('json')->fail(100007);
  200. }
  201. /**
  202. * 删除指定资源
  203. *
  204. * @param int $id
  205. * @return \think\Response
  206. */
  207. public function delete($id)
  208. {
  209. if (!$id) {
  210. return app('json')->fail(100100);
  211. }
  212. if (!$this->services->delete((int)$id)) {
  213. return app('json')->fail(100008);
  214. } else {
  215. return app('json')->success(100002);
  216. }
  217. }
  218. /**
  219. * 显示和隐藏
  220. * @param $id
  221. * @return mixed
  222. */
  223. public function show($id)
  224. {
  225. if (!$id) {
  226. return app('json')->fail(100100);
  227. }
  228. [$show] = $this->request->postMore([['is_show', 0]], true);
  229. if ($this->services->update($id, ['is_show' => $show])) {
  230. return app('json')->success(100001);
  231. } else {
  232. return app('json')->fail(100007);
  233. }
  234. }
  235. /**
  236. * 获取菜单数据
  237. * @return mixed
  238. * @throws \think\db\exception\DataNotFoundException
  239. * @throws \think\db\exception\DbException
  240. * @throws \think\db\exception\ModelNotFoundException
  241. */
  242. public function menus()
  243. {
  244. [$menus, $unique] = $this->services->getMenusList($this->adminInfo['roles'], (int)$this->adminInfo['level']);
  245. return app('json')->success(['menus' => $menus, 'unique' => $unique]);
  246. }
  247. /**
  248. * 获取路由分类
  249. * @param SystemRouteCateServices $service
  250. * @return \think\Response
  251. * @author 等风来
  252. * @email 136327134@qq.com
  253. * @date 2023/4/25
  254. */
  255. public function ruleCate(SystemRouteCateServices $service)
  256. {
  257. return app('json')->success($service->getAllList('adminapi'));
  258. }
  259. /**
  260. * 获取接口列表
  261. * @return array
  262. */
  263. public function ruleList(SystemRouteServices $services)
  264. {
  265. $cateId = request()->get('cate_id', 0);
  266. //获取所有的路由
  267. $ruleList = $services->selectList(['cate_id' => $cateId, 'app_name' => 'adminapi'])->toArray();
  268. return app('json')->success($ruleList);
  269. }
  270. }