SystemMenus.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\setting
  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. ['keyword', ''],
  41. ['pid', $pid]
  42. ], $this->request);
  43. $this->assign(MenusModel::getAdminPage($params));
  44. $this->assign(compact('params'));
  45. return $this->fetch();
  46. }
  47. /**
  48. * 显示创建资源表单页.
  49. *
  50. * @return \think\Response
  51. */
  52. public function create($cid = 0)
  53. {
  54. $form = Form::create(Url::build('save'), [
  55. Form::input('menu_name', '按钮名称')->required('按钮名称必填'),
  56. Form::select('pid', '父级id', $cid)->setOptions(function () {
  57. $list = (Util::sortListTier(MenusModel::all()->toArray(), '顶级', 'pid', 'menu_name'));
  58. $menus = [['value' => 0, 'label' => '顶级按钮']];
  59. foreach ($list as $menu) {
  60. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['menu_name']];
  61. }
  62. return $menus;
  63. })->filterable(1),
  64. Form::select('module', '模块名')->options([['label' => '总后台', 'value' => 'admin']]),
  65. Form::input('controller', '控制器名'),
  66. Form::input('action', '方法名'),
  67. Form::input('params', '参数')->placeholder('举例:a/123/b/234'),
  68. Form::frameInputOne('icon', '图标', Url::build('admin/widget.widgets/icon', array('fodder' => 'icon')))->icon('ionic'),
  69. Form::number('sort', '排序', 0)->min(0),
  70. Form::radio('is_show', '是否菜单', 1)->options([['value' => 0, 'label' => '隐藏'], ['value' => 1, 'label' => '显示(菜单只显示三级)']]),
  71. ]);
  72. $form->setMethod('post')->setTitle('添加权限')->setSuccessScript('parent.$(".J_iframe:visible")[0].contentWindow.location.reload(); setTimeout(function(){parent.layer.close(parent.layer.getFrameIndex(window.name));},2000);');
  73. $this->assign(compact('form'));
  74. return $this->fetch('public/form-builder');
  75. }
  76. /**
  77. * 保存新建的资源
  78. *
  79. * @param \think\Request $request
  80. * @return \think\Response
  81. */
  82. public function save(Request $request)
  83. {
  84. $data = parent::postMore([
  85. 'menu_name',
  86. 'controller',
  87. ['module', 'admin'],
  88. 'action',
  89. 'icon',
  90. 'params',
  91. ['pid', 0],
  92. ['sort', 0],
  93. ['is_show', 0],
  94. ['access', 1]], $request);
  95. if (!$data['menu_name']) return Json::fail('请输入按钮名称');
  96. MenusModel::set($data);
  97. return Json::successful('添加菜单成功!');
  98. }
  99. /**
  100. * 显示编辑资源表单页.
  101. *
  102. * @param int $id
  103. * @return \think\Response
  104. */
  105. public function edit($id)
  106. {
  107. $menu = MenusModel::get($id);
  108. if (!$menu) return Json::fail('数据不存在!');
  109. $form = Form::create(Url::build('update', array('id' => $id)), [
  110. Form::input('menu_name', '按钮名称', $menu['menu_name']),
  111. Form::select('pid', '父级id', (string)$menu->getData('pid'))->setOptions(function () use ($id) {
  112. $list = (Util::sortListTier(MenusModel::where('id', '<>', $id)->select()->toArray(), '顶级', 'pid', 'menu_name'));
  113. $menus = [['value' => 0, 'label' => '顶级按钮']];
  114. foreach ($list as $menu) {
  115. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['menu_name']];
  116. }
  117. return $menus;
  118. })->filterable(1),
  119. Form::select('module', '模块名', $menu['module'])->options([['label' => '总后台', 'value' => 'admin']]),
  120. Form::input('controller', '控制器名', $menu['controller']),
  121. Form::input('action', '方法名', $menu['action']),
  122. Form::input('params', '参数', MenusModel::paramStr($menu['params']))->placeholder('举例:a/123/b/234'),
  123. Form::frameInputOne('icon', '图标', Url::build('admin/widget.widgets/icon', array('fodder' => 'icon')), $menu['icon'])->icon('ionic'),
  124. Form::number('sort', '排序', $menu['sort'])->min(0),
  125. Form::radio('is_show', '是否菜单', $menu['is_show'])->options([['value' => 0, 'label' => '隐藏'], ['value' => 1, 'label' => '显示(菜单只显示三级)']])
  126. ]);
  127. $form->setMethod('post')->setTitle('编辑权限')->setSuccessScript('parent.$(".J_iframe:visible")[0].contentWindow.location.reload(); setTimeout(function(){parent.layer.close(parent.layer.getFrameIndex(window.name));},2000);');
  128. $this->assign(compact('form'));
  129. return $this->fetch('public/form-builder');
  130. }
  131. /**
  132. * 保存更新的资源
  133. *
  134. * @param \think\Request $request
  135. * @param int $id
  136. * @return \think\Response
  137. */
  138. public function update(Request $request, $id)
  139. {
  140. $data = parent::postMore([
  141. 'menu_name',
  142. 'controller',
  143. ['module', 'admin'],
  144. 'action',
  145. 'params',
  146. 'icon',
  147. ['sort', 0],
  148. ['pid', 0],
  149. ['is_show', 0],
  150. ['access', 1]], $request);
  151. if (!$data['menu_name']) return Json::fail('请输入按钮名称');
  152. if (!MenusModel::get($id)) return Json::fail('编辑的记录不存在!');
  153. MenusModel::edit($data, $id);
  154. return Json::successful('修改成功!');
  155. }
  156. /**
  157. * 删除指定资源
  158. *
  159. * @param int $id
  160. * @return \think\Response
  161. */
  162. public function delete($id)
  163. {
  164. if (!$id) return $this->failed('参数错误,请重新打开');
  165. $res = MenusModel::delMenu($id);
  166. if (!$res)
  167. return Json::fail(MenusModel::getErrorInfo('删除失败,请稍候再试!'));
  168. else
  169. return Json::successful('删除成功!');
  170. }
  171. }