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. 'methods' => $menu['method'],
  112. 'menu_name' => $menu['menu_name'],
  113. 'unique_auth' => $menu['unique_auth'] ?? '',
  114. 'api_url' => $menu['api_url'],
  115. 'pid' => $menu['path'],
  116. ];
  117. }
  118. $this->services->saveAll($data);
  119. return app('json')->success(100021);
  120. }
  121. /**
  122. * 获取一条菜单权限信息
  123. * @param int $id
  124. * @return \think\Response
  125. */
  126. public function read($id)
  127. {
  128. if (!$id) {
  129. return app('json')->fail(100026);
  130. }
  131. return app('json')->success($this->services->find((int)$id));
  132. }
  133. /**
  134. * 修改菜单权限表单获取
  135. * @param int $id
  136. * @return \think\Response
  137. */
  138. public function edit($id)
  139. {
  140. if (!$id) {
  141. return app('json')->fail(100100);
  142. }
  143. return app('json')->success($this->services->updateMenus((int)$id));
  144. }
  145. /**
  146. * 修改菜单
  147. * @param $id
  148. * @return mixed
  149. */
  150. public function update($id)
  151. {
  152. if (!$id || !($menu = $this->services->get($id)))
  153. return app('json')->fail(100026);
  154. $data = $this->request->postMore([
  155. 'menu_name',
  156. 'controller',
  157. ['module', 'admin'],
  158. 'action',
  159. 'params',
  160. ['icon', ''],
  161. ['menu_path', ''],
  162. ['api_url', ''],
  163. ['methods', ''],
  164. ['unique_auth', ''],
  165. ['path', []],
  166. ['sort', 0],
  167. ['pid', 0],
  168. ['is_header', 0],
  169. ['header', ''],
  170. ['auth_type', 0],
  171. ['access', 1],
  172. ['is_show', 0],
  173. ['is_show_path', 0],
  174. ]);
  175. if (!$data['menu_name'])
  176. return app('json')->fail(400198);
  177. $data['path'] = implode('/', $data['path']);
  178. if ($this->services->update($id, $data))
  179. return app('json')->success(100001);
  180. else
  181. return app('json')->fail(100007);
  182. }
  183. /**
  184. * 删除指定资源
  185. *
  186. * @param int $id
  187. * @return \think\Response
  188. */
  189. public function delete($id)
  190. {
  191. if (!$id) {
  192. return app('json')->fail(100100);
  193. }
  194. if (!$this->services->delete((int)$id)) {
  195. return app('json')->fail(100008);
  196. } else {
  197. return app('json')->success(100002);
  198. }
  199. }
  200. /**
  201. * 显示和隐藏
  202. * @param $id
  203. * @return mixed
  204. */
  205. public function show($id)
  206. {
  207. if (!$id) {
  208. return app('json')->fail(100100);
  209. }
  210. [$show] = $this->request->postMore([['is_show', 0]], true);
  211. if ($this->services->update($id, ['is_show' => $show])) {
  212. return app('json')->success(100001);
  213. } else {
  214. return app('json')->fail(100007);
  215. }
  216. }
  217. /**
  218. * 获取菜单数据
  219. * @return mixed
  220. * @throws \think\db\exception\DataNotFoundException
  221. * @throws \think\db\exception\DbException
  222. * @throws \think\db\exception\ModelNotFoundException
  223. */
  224. public function menus()
  225. {
  226. [$menus, $unique] = $this->services->getMenusList($this->adminInfo['roles'], (int)$this->adminInfo['level']);
  227. return app('json')->success(['menus' => $menus, 'unique' => $unique]);
  228. }
  229. /**
  230. * 获取接口列表
  231. * @return array
  232. */
  233. public function ruleList()
  234. {
  235. //获取所有的路由
  236. $ruleList = app()->make(SystemRouteServices::class)->selectList(['app_name' => 'adminapi'], 'name,path,method,type,id')->toArray();
  237. $menuApiList = $this->services->getColumn(['auth_type' => 2, 'is_del' => 0], "concat(`api_url`,'_',lower(`methods`)) as rule");
  238. if ($menuApiList) $menuApiList = array_column($menuApiList, 'rule');
  239. $list = [];
  240. foreach ($ruleList as $item) {
  241. if (!in_array($item['path'] . '_' . strtolower($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. }