SystemMenus.php 5.8 KB

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