SystemMenus.php 8.0 KB

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