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