SystemMenus.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\v1\system;
  12. use app\controller\admin\AuthController;
  13. use app\services\system\SystemMenusServices;
  14. use crmeb\services\CacheService;
  15. use think\facade\App;
  16. use think\helper\Str;
  17. /**
  18. * 菜单权限
  19. * Class SystemMenus
  20. * @package app\controller\admin\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. ['type', 1],
  43. ['is_show', ''],
  44. ['keyword', ''],
  45. ]);
  46. return $this->success($this->services->getList($where));
  47. }
  48. /**
  49. * 显示创建资源表单页.
  50. *
  51. * @return \think\Response
  52. */
  53. public function create()
  54. {
  55. [$type] = $this->request->getMore([
  56. ['type', 1]
  57. ], true);
  58. return $this->success($this->services->createMenus($type));
  59. }
  60. /**
  61. * 保存菜单权限
  62. * @return mixed
  63. */
  64. public function save()
  65. {
  66. $data = $this->request->getMore([
  67. ['menu_name', ''],
  68. ['controller', ''],
  69. ['module', 'admin'],
  70. ['action', ''],
  71. ['icon', ''],
  72. ['params', ''],
  73. ['path', []],
  74. ['menu_path', ''],
  75. ['api_url', ''],
  76. ['methods', ''],
  77. ['unique_auth', ''],
  78. ['header', ''],
  79. ['is_header', 0],
  80. ['pid', 0],
  81. ['type', 1],
  82. ['sort', 0],
  83. ['auth_type', 0],
  84. ['access', 1],
  85. ['is_show', 1],
  86. ['is_show_path', 0],
  87. ]);
  88. if (!$data['menu_name'])
  89. return $this->fail('请填写按钮名称');
  90. $data['path'] = implode('/', $data['path']);
  91. $data['is_show'] = $data['auth_type'] == 2 ? 1 : $data['is_show'];
  92. if ($this->services->save($data)) {
  93. CacheService::redisHandler('system_menus')->clear();
  94. return $this->success('添加成功');
  95. } else {
  96. return $this->fail('添加失败');
  97. }
  98. }
  99. /**
  100. * 获取一条菜单权限信息
  101. * @param int $id
  102. * @return \think\Response
  103. */
  104. public function read($id)
  105. {
  106. if (!$id) {
  107. return $this->fail('数据不存在');
  108. }
  109. return $this->success($this->services->find((int)$id));
  110. }
  111. /**
  112. * 修改菜单权限表单获取
  113. * @param int $id
  114. * @return \think\Response
  115. */
  116. public function edit($id)
  117. {
  118. if (!$id) {
  119. return $this->fail('缺少修改参数');
  120. }
  121. return $this->success($this->services->updateMenus((int)$id));
  122. }
  123. /**
  124. * 修改菜单
  125. * @param $id
  126. * @return mixed
  127. */
  128. public function update($id)
  129. {
  130. if (!$id || !($menu = $this->services->get($id)))
  131. return $this->fail('数据不存在');
  132. $data = $this->request->postMore([
  133. ['type', 1],
  134. 'menu_name',
  135. 'controller',
  136. ['module', 'admin'],
  137. 'action',
  138. 'params',
  139. ['icon', ''],
  140. ['menu_path', ''],
  141. ['api_url', ''],
  142. ['methods', ''],
  143. ['unique_auth', ''],
  144. ['path', []],
  145. ['sort', 0],
  146. ['pid', 0],
  147. ['is_header', 0],
  148. ['header', ''],
  149. ['auth_type', 0],
  150. ['access', 1],
  151. ['is_show', 1],
  152. ['is_show_path', 0],
  153. ]);
  154. if (!$data['menu_name'])
  155. return $this->fail('请输入按钮名称');
  156. $data['path'] = implode('/', $data['path']);
  157. $data['is_show'] = $data['auth_type'] == 2 ? 1 : $data['is_show'];
  158. if ($this->services->update($id, $data)) {
  159. CacheService::redisHandler('system_menus')->clear();
  160. return $this->success('修改成功');
  161. } else
  162. return $this->fail('修改失败');
  163. }
  164. /**
  165. * 删除指定资源
  166. *
  167. * @param int $id
  168. * @return \think\Response
  169. */
  170. public function delete($id)
  171. {
  172. if (!$id) {
  173. return $this->fail('参数错误,请重新打开');
  174. }
  175. if (!$this->services->delete((int)$id)) {
  176. CacheService::redisHandler('system_menus')->clear();
  177. return $this->fail('删除失败,请稍候再试!');
  178. } else {
  179. return $this->success('删除成功!');
  180. }
  181. }
  182. /**
  183. * 显示和隐藏
  184. * @param $id
  185. * @return mixed
  186. */
  187. public function show($id)
  188. {
  189. if (!$id) {
  190. return $this->fail('参数错误,请重新打开');
  191. }
  192. [$show] = $this->request->postMore([['is_show', 0]], true);
  193. if ($this->services->update($id, ['is_show' => $show])) {
  194. CacheService::redisHandler('system_menus')->clear();
  195. return $this->success('修改成功');
  196. } else {
  197. return $this->fail('修改失败');
  198. }
  199. }
  200. /**
  201. * 获取菜单数据
  202. * @return mixed
  203. * @throws \think\db\exception\DataNotFoundException
  204. * @throws \think\db\exception\DbException
  205. * @throws \think\db\exception\ModelNotFoundException
  206. */
  207. public function menus()
  208. {
  209. [$type] = $this->request->getMore([
  210. ['type', 1]
  211. ], true);
  212. [$menus, $unique] = $this->services->getMenusList($this->adminInfo['roles'], (int)$this->adminInfo['level']);
  213. return app('json')->success(['menus' => $menus, 'unique' => $unique]);
  214. }
  215. /**
  216. * 获取接口列表
  217. * @return array
  218. */
  219. public function ruleList($type = 1)
  220. {
  221. $this->app = app();
  222. $rule = $type == 1 ? 'adminapi/' : 'storeapi/';
  223. $this->app->route->setTestMode(true);
  224. $this->app->route->clear();
  225. $path = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR;
  226. $files = is_dir($path) ? scandir($path) : [];
  227. foreach ($files as $file) {
  228. if (strpos($file, '.php')) {
  229. include $path . $file;
  230. }
  231. }
  232. $ruleList = $this->app->route->getRuleList();
  233. $ruleNewList = [];
  234. foreach ($ruleList as $item) {
  235. if (Str::contains($item['rule'], $rule)) {
  236. $ruleNewList[] = $item;
  237. }
  238. }
  239. foreach ($ruleNewList as $key => &$value) {
  240. $only = $value['option']['only'] ?? [];
  241. $route = is_string($value['route']) ? explode('/', $value['route']) : [];
  242. $value['route'] = is_string($value['route']) ? $value['route'] : '';
  243. $action = $route[count($route) - 1] ?? null;
  244. if ($only && $action && !in_array($action, $only)) {
  245. unset($ruleNewList[$key]);
  246. }
  247. $except = $value['option']['except'] ?? [];
  248. if ($except && $action && in_array($action, $except)) {
  249. unset($ruleNewList[$key]);
  250. }
  251. }
  252. $ruleList = $ruleNewList;
  253. //获取所有的路由
  254. // $ruleList = Route::getRuleList();
  255. $menuApiList = $this->services->getColumn(['auth_type' => 2, 'is_del' => 0, 'type' => $type], "concat(`api_url`,'_',lower(`methods`)) as rule");
  256. if ($menuApiList) $menuApiList = array_column($menuApiList, 'rule');
  257. $list = [];
  258. $action_arr = ['index', 'read', 'create', 'save', 'edit', 'update', 'delete'];
  259. $middleware = $type == 1 ? 'app\http\middleware\admin\AdminCkeckRoleMiddleware' : 'app\http\middleware\store\StoreCkeckRoleMiddleware';
  260. foreach ($ruleList as $item) {
  261. $item['rule'] = str_replace($type == 1 ? 'adminapi/' : 'storeapi/', '', $item['rule']);
  262. if (!in_array($item['rule'] . '_' . $item['method'], $menuApiList) && isset($item['option']['middleware']) && in_array($middleware, $item['option']['middleware'])) {
  263. $real_name = $item['option']['real_name'] ?? '';
  264. if (is_array($real_name)) {
  265. foreach ($action_arr as $action) {
  266. if (Str::contains($item['route'], $action)) {
  267. $real_name = $real_name[$action] ?? '';
  268. }
  269. }
  270. }
  271. $item['real_name'] = $real_name;
  272. unset($item['option']);
  273. $item['method'] = strtoupper($item['method']);
  274. $list[] = $item;
  275. }
  276. }
  277. return app('json')->success($list);
  278. }
  279. }