SystemMenus.php 8.2 KB

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