SystemMenus.php 7.8 KB

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