SystemMenus.php 8.1 KB

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