SystemMenus.php 7.4 KB

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