SystemMenus.php 9.1 KB

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