SystemMenus.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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\adminapi\controller\v1\setting;
  12. use app\adminapi\controller\AuthController;
  13. use app\services\system\SystemMenusServices;
  14. use think\facade\App;
  15. use think\facade\Route;
  16. /**
  17. * 菜单权限
  18. * Class SystemMenus
  19. * @package app\adminapi\controller\v1\setting
  20. */
  21. class SystemMenus extends AuthController
  22. {
  23. /**
  24. * SystemMenus constructor.
  25. * @param App $app
  26. * @param SystemMenusServices $services
  27. */
  28. public function __construct(App $app, SystemMenusServices $services)
  29. {
  30. parent::__construct($app);
  31. $this->services = $services;
  32. $this->request->filter(['addslashes', 'trim']);
  33. }
  34. /**
  35. * 菜单展示列表
  36. * @return \think\Response
  37. */
  38. public function index()
  39. {
  40. $where = $this->request->getMore([
  41. ['is_show', ''],
  42. ['keyword', ''],
  43. ]);
  44. return app('json')->success($this->services->getList($where));
  45. }
  46. /**
  47. * 显示创建资源表单页.
  48. *
  49. * @return \think\Response
  50. */
  51. public function create()
  52. {
  53. return app('json')->success($this->services->createMenus());
  54. }
  55. /**
  56. * 保存菜单权限
  57. * @return mixed
  58. */
  59. public function save()
  60. {
  61. $data = $this->request->getMore([
  62. ['menu_name', ''],
  63. ['controller', ''],
  64. ['module', 'admin'],
  65. ['action', ''],
  66. ['icon', ''],
  67. ['params', ''],
  68. ['path', []],
  69. ['menu_path', ''],
  70. ['api_url', ''],
  71. ['methods', ''],
  72. ['unique_auth', ''],
  73. ['header', ''],
  74. ['is_header', 0],
  75. ['pid', 0],
  76. ['sort', 0],
  77. ['auth_type', 0],
  78. ['access', 1],
  79. ['is_show', 0],
  80. ['is_show_path', 0],
  81. ]);
  82. if (!$data['menu_name'])
  83. return app('json')->fail('请填写按钮名称');
  84. $data['path'] = implode('/', $data['path']);
  85. if ($this->services->save($data)) {
  86. return app('json')->success('添加成功');
  87. } else {
  88. return app('json')->fail('添加失败');
  89. }
  90. }
  91. /**
  92. * 获取一条菜单权限信息
  93. * @param int $id
  94. * @return \think\Response
  95. */
  96. public function read($id)
  97. {
  98. if (!$id) {
  99. return app('json')->fail('数据不存在');
  100. }
  101. return app('json')->success($this->services->find((int)$id));
  102. }
  103. /**
  104. * 修改菜单权限表单获取
  105. * @param int $id
  106. * @return \think\Response
  107. */
  108. public function edit($id)
  109. {
  110. if (!$id) {
  111. return app('json')->fail('缺少修改参数');
  112. }
  113. return app('json')->success($this->services->updateMenus((int)$id));
  114. }
  115. /**
  116. * 修改菜单
  117. * @param $id
  118. * @return mixed
  119. */
  120. public function update($id)
  121. {
  122. if (!$id || !($menu = $this->services->get($id)))
  123. return app('json')->fail('数据不存在');
  124. $data = $this->request->postMore([
  125. 'menu_name',
  126. 'controller',
  127. ['module', 'admin'],
  128. 'action',
  129. 'params',
  130. ['icon', ''],
  131. ['menu_path', ''],
  132. ['api_url', ''],
  133. ['methods', ''],
  134. ['unique_auth', ''],
  135. ['path', []],
  136. ['sort', 0],
  137. ['pid', 0],
  138. ['is_header', 0],
  139. ['header', ''],
  140. ['auth_type', 0],
  141. ['access', 1],
  142. ['is_show', 0],
  143. ['is_show_path', 0],
  144. ]);
  145. if (!$data['menu_name'])
  146. return app('json')->fail('请输入按钮名称');
  147. $data['path'] = implode('/', $data['path']);
  148. if ($this->services->update($id, $data))
  149. return app('json')->success('修改成功');
  150. else
  151. return app('json')->fail('修改失败');
  152. }
  153. /**
  154. * 删除指定资源
  155. *
  156. * @param int $id
  157. * @return \think\Response
  158. */
  159. public function delete($id)
  160. {
  161. if (!$id) {
  162. return app('json')->fail('参数错误,请重新打开');
  163. }
  164. if (!$this->services->delete((int)$id)) {
  165. return app('json')->fail('删除失败,请稍候再试!');
  166. } else {
  167. return app('json')->success('删除成功!');
  168. }
  169. }
  170. /**
  171. * 显示和隐藏
  172. * @param $id
  173. * @return mixed
  174. */
  175. public function show($id)
  176. {
  177. if (!$id) {
  178. return app('json')->fail('参数错误,请重新打开');
  179. }
  180. [$show] = $this->request->postMore([['is_show', 0]], true);
  181. if ($this->services->update($id, ['is_show' => $show])) {
  182. return app('json')->success('修改成功');
  183. } else {
  184. return app('json')->fail('修改失败');
  185. }
  186. }
  187. /**
  188. * 获取菜单数据
  189. * @return mixed
  190. * @throws \think\db\exception\DataNotFoundException
  191. * @throws \think\db\exception\DbException
  192. * @throws \think\db\exception\ModelNotFoundException
  193. */
  194. public function menus()
  195. {
  196. [$menus, $unique] = $this->services->getMenusList($this->adminInfo['roles'], (int)$this->adminInfo['level']);
  197. return app('json')->success(['menus' => $menus, 'unique' => $unique]);
  198. }
  199. /**
  200. * 获取接口列表
  201. * @return array
  202. */
  203. public function ruleList()
  204. {
  205. //获取所有的路由
  206. $ruleList = Route::getRuleList();
  207. $menuApiList = $this->services->getColumn(['auth_type' => 2, 'is_del' => 0], "concat(`api_url`,'_',lower(`methods`)) as rule");
  208. if ($menuApiList) $menuApiList = array_column($menuApiList, 'rule');
  209. $list = [];
  210. foreach ($ruleList as $item) {
  211. $item['rule'] = str_replace('adminapi/', '', $item['rule']);
  212. if (!in_array($item['rule'] . '_' . $item['method'], $menuApiList)) {
  213. $item['real_name'] = $item['option']['real_name'] ?? '';
  214. unset($item['option']);
  215. $item['method'] = strtoupper($item['method']);
  216. $list[] = $item;
  217. }
  218. }
  219. return app('json')->success($list);
  220. }
  221. }