SystemMenus.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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\admin\controller\setting;
  12. use service\FormBuilder as Form;
  13. use traits\CurdControllerTrait;
  14. use service\UtilService as Util;
  15. use service\JsonService as Json;
  16. use service\UploadService as Upload;
  17. use think\Request;
  18. use think\Url;
  19. use app\admin\model\system\SystemMenus as MenusModel;
  20. use app\admin\controller\AuthController;
  21. /**
  22. * 菜单管理控制器
  23. * Class SystemMenus
  24. * @package app\admin\controller\system
  25. */
  26. class SystemMenus extends AuthController
  27. {
  28. use CurdControllerTrait;
  29. public $bindModel = MenusModel::class;
  30. /**
  31. * 显示资源列表
  32. *
  33. * @return \think\Response
  34. */
  35. public function index()
  36. {
  37. $pid = $this->request->param('pid') ? $this->request->param('pid') : 0;
  38. $params = parent::getMore([
  39. ['is_show', ''],
  40. // ['access',''],
  41. ['keyword', ''],
  42. ['pid', $pid]
  43. ], $this->request);
  44. $this->assign(MenusModel::getAdminPage($params));
  45. $this->assign(compact('params'));
  46. return $this->fetch();
  47. }
  48. /**
  49. * 显示创建资源表单页.
  50. *
  51. * @return \think\Response
  52. */
  53. public function create($cid = 0)
  54. {
  55. $form = Form::create(Url::build('save'), [
  56. Form::input('menu_name', '按钮名称')->required('按钮名称必填'),
  57. Form::select('pid', '父级id', $cid)->setOptions(function () {
  58. $list = (Util::sortListTier(MenusModel::all()->toArray(), '顶级', 'pid', 'menu_name'));
  59. $menus = [['value' => 0, 'label' => '顶级按钮']];
  60. foreach ($list as $menu) {
  61. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['menu_name']];
  62. }
  63. return $menus;
  64. })->filterable(1),
  65. Form::select('module', '模块名')->options([['label' => '总后台', 'value' => 'admin']]),
  66. Form::input('controller', '控制器名'),
  67. Form::input('action', '方法名'),
  68. Form::input('params', '参数')->placeholder('举例:a/123/b/234'),
  69. Form::frameInputOne('icon', '图标', Url::build('admin/widget.widgets/icon', array('fodder' => 'icon')))->icon('ionic'),
  70. Form::number('sort', '排序', 0),
  71. Form::radio('is_show', '是否菜单', 1)->options([['value' => 0, 'label' => '隐藏'], ['value' => 1, 'label' => '显示(菜单只显示三级)']]),
  72. ]);
  73. $form->setMethod('post')->setTitle('添加权限');
  74. $this->assign(compact('form'));
  75. return $this->fetch('public/form-builder');
  76. }
  77. /**
  78. * 保存新建的资源
  79. *
  80. * @param \think\Request $request
  81. * @return \think\Response
  82. */
  83. public function save(Request $request)
  84. {
  85. $data = parent::postMore([
  86. 'menu_name',
  87. 'controller',
  88. ['module', 'admin'],
  89. 'action',
  90. 'icon',
  91. 'params',
  92. ['pid', 0],
  93. ['sort', 0],
  94. ['is_show', 0],
  95. ['access', 1]], $request);
  96. if (!$data['menu_name']) return Json::fail('请输入按钮名称');
  97. MenusModel::set($data);
  98. return Json::successful('添加菜单成功!');
  99. }
  100. /**
  101. * 显示编辑资源表单页.
  102. *
  103. * @param int $id
  104. * @return \think\Response
  105. */
  106. public function edit($id)
  107. {
  108. $menu = MenusModel::get($id);
  109. if (!$menu) return Json::fail('数据不存在!');
  110. $form = Form::create(Url::build('update', array('id' => $id)), [
  111. Form::input('menu_name', '按钮名称', $menu['menu_name']),
  112. Form::select('pid', '父级id', (string)$menu->getData('pid'))->setOptions(function () use ($id) {
  113. $list = (Util::sortListTier(MenusModel::where('id', '<>', $id)->select()->toArray(), '顶级', 'pid', 'menu_name'));
  114. $menus = [['value' => 0, 'label' => '顶级按钮']];
  115. foreach ($list as $menu) {
  116. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['menu_name']];
  117. }
  118. return $menus;
  119. })->filterable(1),
  120. Form::select('module', '模块名', $menu['module'])->options([['label' => '总后台', 'value' => 'admin']]),
  121. Form::input('controller', '控制器名', $menu['controller']),
  122. Form::input('action', '方法名', $menu['action']),
  123. Form::input('params', '参数', MenusModel::paramStr($menu['params']))->placeholder('举例:a/123/b/234'),
  124. Form::frameInputOne('icon', '图标', Url::build('admin/widget.widgets/icon', array('fodder' => 'icon')), $menu['icon'])->icon('ionic'),
  125. Form::number('sort', '排序', $menu['sort']),
  126. Form::radio('is_show', '是否菜单', $menu['is_show'])->options([['value' => 0, 'label' => '隐藏'], ['value' => 1, 'label' => '显示(菜单只显示三级)']])
  127. ]);
  128. $form->setMethod('post')->setTitle('编辑权限');
  129. $this->assign(compact('form'));
  130. return $this->fetch('public/form-builder');
  131. }
  132. /**
  133. * 保存更新的资源
  134. *
  135. * @param \think\Request $request
  136. * @param int $id
  137. * @return \think\Response
  138. */
  139. public function update(Request $request, $id)
  140. {
  141. $data = parent::postMore([
  142. 'menu_name',
  143. 'controller',
  144. ['module', 'admin'],
  145. 'action',
  146. 'params',
  147. 'icon',
  148. ['sort', 0],
  149. ['pid', 0],
  150. ['is_show', 0],
  151. ['access', 1]], $request);
  152. if (!$data['menu_name']) return Json::fail('请输入按钮名称');
  153. if (!MenusModel::get($id)) return Json::fail('编辑的记录不存在!');
  154. MenusModel::edit($data, $id);
  155. return Json::successful('修改成功!');
  156. }
  157. /**
  158. * 删除指定资源
  159. *
  160. * @param int $id
  161. * @return \think\Response
  162. */
  163. public function delete($id)
  164. {
  165. if (!$id) return $this->failed('参数错误,请重新打开');
  166. $res = MenusModel::delMenu($id);
  167. if (!$res)
  168. return Json::fail(MenusModel::getErrorInfo('删除失败,请稍候再试!'));
  169. else
  170. return Json::successful('删除成功!');
  171. }
  172. }