SystemMenus.php 9.6 KB

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