SystemMenus.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. ];
  168. }
  169. $this->services->saveAll($data);
  170. return app('json')->success(100021);
  171. }
  172. /**
  173. * 获取一条菜单权限信息
  174. * @param int $id
  175. * @return \think\Response
  176. */
  177. public function read($id)
  178. {
  179. if (!$id) {
  180. return app('json')->fail(100026);
  181. }
  182. return app('json')->success($this->services->find((int)$id));
  183. }
  184. /**
  185. * 修改菜单权限表单获取
  186. * @param int $id
  187. * @return \think\Response
  188. */
  189. public function edit($id)
  190. {
  191. if (!$id) {
  192. return app('json')->fail(100100);
  193. }
  194. return app('json')->success($this->services->updateMenus((int)$id));
  195. }
  196. /**
  197. * 修改菜单
  198. * @param $id
  199. * @return mixed
  200. */
  201. public function update($id)
  202. {
  203. if (!$id || !($menu = $this->services->get($id)))
  204. return app('json')->fail(100026);
  205. $data = $this->request->postMore([
  206. 'menu_name',
  207. 'controller',
  208. ['module', 'admin'],
  209. 'action',
  210. 'params',
  211. ['icon', ''],
  212. ['menu_path', ''],
  213. ['api_url', ''],
  214. ['methods', ''],
  215. ['unique_auth', ''],
  216. ['path', []],
  217. ['sort', 0],
  218. ['pid', 0],
  219. ['is_header', 0],
  220. ['header', ''],
  221. ['auth_type', 0],
  222. ['access', 1],
  223. ['is_show', 0],
  224. ['is_show_path', 0],
  225. ]);
  226. if (!$data['menu_name'])
  227. return app('json')->fail(400198);
  228. $data['path'] = implode('/', $data['path']);
  229. if ($this->services->update($id, $data))
  230. return app('json')->success(100001);
  231. else
  232. return app('json')->fail(100007);
  233. }
  234. /**
  235. * 删除指定资源
  236. *
  237. * @param int $id
  238. * @return \think\Response
  239. */
  240. public function delete($id)
  241. {
  242. if (!$id) {
  243. return app('json')->fail(100100);
  244. }
  245. if (!$this->services->delete((int)$id)) {
  246. return app('json')->fail(100008);
  247. } else {
  248. return app('json')->success(100002);
  249. }
  250. }
  251. /**
  252. * 权限的开启和关闭,显示和隐藏
  253. * @param $id
  254. * @return mixed
  255. */
  256. public function show($id)
  257. {
  258. if (!$id) {
  259. return app('json')->fail(100100);
  260. }
  261. [$isShow, $isShowPath] = $this->request->postMore([['is_show', 0], ['is_show_path', 0]], true);
  262. if ($isShow == -1) {
  263. $res = $this->services->update($id, ['is_show_path' => $isShowPath]);
  264. } else {
  265. $res = $this->services->update($id, ['is_show' => $isShow, 'is_show_path' => $isShow]);
  266. }
  267. if ($res) {
  268. return app('json')->success(100001);
  269. } else {
  270. return app('json')->fail(100007);
  271. }
  272. }
  273. /**
  274. * 获取菜单数据
  275. * @return mixed
  276. * @throws \think\db\exception\DataNotFoundException
  277. * @throws \think\db\exception\DbException
  278. * @throws \think\db\exception\ModelNotFoundException
  279. */
  280. public function menus()
  281. {
  282. [$menus, $unique] = $this->services->getMenusList($this->adminInfo['roles'], (int)$this->adminInfo['level']);
  283. return app('json')->success(['menus' => $menus, 'unique' => $unique]);
  284. }
  285. /**
  286. * 获取路由分类
  287. * @param SystemRouteCateServices $service
  288. * @return \think\Response
  289. * @author 等风来
  290. * @email 136327134@qq.com
  291. * @date 2023/4/25
  292. */
  293. public function ruleCate(SystemRouteCateServices $service)
  294. {
  295. return app('json')->success($service->getAllList('adminapi'));
  296. }
  297. /**
  298. * 获取接口列表
  299. * @return array
  300. */
  301. public function ruleList(SystemRouteServices $services)
  302. {
  303. $cateId = request()->get('cate_id', 0);
  304. //获取所有的路由
  305. $ruleList = $services->selectList(['cate_id' => $cateId, 'app_name' => 'adminapi'])->toArray();
  306. return app('json')->success($ruleList);
  307. }
  308. }