123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- <?php
- namespace app\adminapi\controller\v1\setting;
- use app\adminapi\controller\AuthController;
- use crmeb\services\FormBuilder as Form;
- use crmeb\services\UtilService;
- use app\models\system\SystemMenus as SystemMenusAdmin;
- use crmeb\services\UtilService as Util;
- use think\facade\Route as Url;
- use think\Request;
- class SystemMenus extends AuthController
- {
-
- public function index()
- {
- $where = UtilService::getMore([
- ['is_show', ''],
- ['pid', ''],
- ['keyword', ''],
- ]);
- return $this->success(SystemMenusAdmin::getAuthList($where));
- }
-
- public function create()
- {
- $field[] = Form::input('menu_name', '按钮名称')->required('按钮名称必填');
- $field[] = Form::select('pid', '父级id')->setOptions(function () {
- $list = sort_list_tier(SystemMenusAdmin::getMenusArray(SystemMenusAdmin::all(function ($m) {
- $m->order('sort DESC,id ASC')->where('is_del', 0);
- })), '0', 'pid', 'id');
- $menus = [['value' => 0, 'label' => '顶级按钮']];
- foreach ($list as $menu) {
- $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['menu_name']];
- }
- return $menus;
- })->filterable(1);
- $field[] = Form::select('module', '模块名')->options([['label' => '总后台', 'value' => 'admin']]);
- $field[] = Form::input('controller', '控制器名');
- $field[] = Form::input('action', '方法名');
- $field[] = Form::input('menu_path', '路由名称')->placeholder('请输入前台跳转路由地址')->required('请填写前台路由地址');
- $field[] = Form::input('unique_auth', '权限标识')->placeholder('不填写则后台自动生成');
- $field[] = Form::input('params', '参数')->placeholder('举例:a/123/b/234');
- $field[] = Form::frameInputOne('icon', '图标', Url::buildUrl('admin/widget.widgets/icon', array('fodder' => 'icon')))->icon('md-add')->height('500px');
- $field[] = Form::number('sort', '排序', 0);
- $field[] = Form::radio('auth_type', '类型', 1)->options([['value' => 2, 'label' => '接口'], ['value' => 1, 'label' => '菜单(菜单只显示三级)']]);
- $field[] = Form::radio('is_header', '是否顶部菜单', 0)->options([['value' => 0, 'label' => '否'], ['value' => 1, 'label' => '是']]);
- $field[] = Form::radio('is_show', '状态', 0)->options([['value' => 0, 'label' => '关闭'], ['value' => 1, 'label' => '开启']]);
- $field[] = Form::radio('is_show_path', '是否为前端隐藏菜单', 0)->options([['value' => 1, 'label' => '是'], ['value' => 0, 'label' => '否']]);
- $field[] = Form::select('header', '顶部菜单')->setOptions(function () {
- $menulist = SystemMenusAdmin::all(function ($m) {
- $m->where(['is_header' => 1])->order('sort DESC,id ASC');
- })->toArray();
- $list = sort_list_tier($menulist, '顶级', 'pid', 'id');
- $menus = [['value' => '', 'label' => '请选择']];
- foreach ($list as $menu) {
- $menus[] = ['value' => $menu['header'], 'label' => $menu['menu_name']];
- }
- return $menus;
- })->filterable(1);
- return $this->makePostForm('添加权限', $field, Url::buildUrl('/setting/save')->suffix(false));
- }
-
- public function save(Request $request)
- {
- $data = UtilService::getMore([
- ['menu_name', ''],
- ['controller', ''],
- ['module', 'admin'],
- ['action', ''],
- ['icon', ''],
- ['params', ''],
- ['menu_path', ''],
- ['api_url', ''],
- ['methods', ''],
- ['unique_auth', ''],
- ['header', ''],
- ['is_header', 0],
- ['pid', 0],
- ['sort', 0],
- ['auth_type', 0],
- ['access', 1],
- ['is_show', 0],
- ['is_show_path', 0],
- ], $request);
- if (!$data['menu_name'])
- return $this->fail('请填写按钮名称');
- if (SystemMenusAdmin::create($data))
- return $this->success('添加成功');
- else
- return $this->fail('添加失败');
- }
-
- public function read($id)
- {
- if (!$id || !($menu = SystemMenusAdmin::get($id)))
- return $this->fail('数据不存在');
- $menu = $menu->getData();
- $menu['pid'] = (string)$menu['pid'];
- $menu['auth_type'] = (string)$menu['auth_type'];
- $menu['is_header'] = (string)$menu['is_header'];
- $menu['is_show'] = (string)$menu['is_show'];
- $menu['is_show_path'] = (string)$menu['is_show_path'];
- return $this->success($menu);
- }
-
- public function edit($id)
- {
- if (!$id || !($menu = SystemMenusAdmin::get($id)))
- return $this->fail('数据不存在');
- $field[] = Form::input('menu_name', '按钮名称', $menu['menu_name']);
- $field[] = Form::select('pid', '父级id', (string)$menu->getData('pid'))->setOptions(function () use ($id) {
- $list = (Util::sortListTier(SystemMenusAdmin::all(function ($m) {
- $m->order('sort DESC,id ASC')->where('is_del', 0);
- })->toArray(), '顶级', 'pid', 'menu_name'));
- $menus = [['value' => 0, 'label' => '顶级按钮']];
- foreach ($list as $menu) {
- $menus[] = ['value' => (int)$menu['id'], 'label' => $menu['html'] . $menu['menu_name']];
- }
- return $menus;
- })->filterable(1);
- $field[] = Form::select('module', '模块名', $menu['module'])->options([['label' => '总后台', 'value' => 'admin']]);
- $field[] = Form::input('controller', '控制器名', $menu['controller']);
- if (!empty($menu['controller'])) {
- $controller = preg_replace_callback('/([.]+([a-z]{1}))/i', function ($matches) {
- return '\\' . strtoupper($matches[2]);
- }, $menu['controller']);
- if (class_exists('\app\adminapi\v1\controller\\' . $controller)) {
- $list = get_this_class_methods('\app\adminapi\v1\controller\\' . $controller);
- $field[] = Form::select('action', '方法名', (string)$menu->getData('action'))->setOptions(function () use ($list) {
- $menus = [['value' => 0, 'label' => '默认函数']];
- foreach ($list as $menu) {
- $menus[] = ['value' => $menu, 'label' => $menu];
- }
- return $menus;
- })->filterable(1);
- } else {
- $field[] = Form::input('action', '方法名', $menu['action']);
- }
- } else {
- $field[] = Form::input('action', '方法名');
- }
- $field[] = Form::input('params', '参数', SystemMenusAdmin::paramStr($menu['params']))->placeholder('举例:a/123/b/234');
- $field[] = Form::input('menu_path', '路由名称', $menu['menu_path'])->placeholder('请输入前台跳转路由地址')->required('请填写前台路由地址');
- $field[] = Form::input('unique_auth', '权限标识', $menu['unique_auth'])->placeholder('不填写则后台自动生成');
- $field[] = Form::frameInputOne('icon', '图标', Url::buildUrl('admin/widget.widgets/icon', array('fodder' => 'icon')), $menu['icon'])->icon('ionic')->height('500px');
- $field[] = Form::number('sort', '排序', $menu['sort']);
- $field[] = Form::radio('auth_type', '是否菜单', $menu['is_show'])->options([['value' => 0, 'label' => '隐藏'], ['value' => 1, 'label' => '显示(菜单只显示三级)']]);
- $field[] = Form::radio('is_header', '是否顶部菜单', $menu['is_header'])->options([['value' => 0, 'label' => '否'], ['value' => 1, 'label' => '是']]);
- $field[] = Form::radio('is_show_path', '是否为前端隐藏菜单', $menu['is_show_path'])->options([['value' => 0, 'label' => '否'], ['value' => 1, 'label' => '是']]);
- $field[] = Form::select('header', '顶部菜单', (string)$menu->getData('header'))->setOptions(function () use ($id) {
- $list = (Util::sortListTier(SystemMenusAdmin::all(function ($m) {
- $m->where(['is_header' => 1]);
- })->toArray(), '顶级', 'header', 'menu_name'));
- $list = SystemMenusAdmin::where(['is_header' => 1])->select()->toArray();
- dd($list);
- $menus = [['value' => 0, 'label' => '请选择']];
- foreach ($list as $menu) {
- $menus[] = ['value' => (int)$menu['header'], 'label' => $menu['html'] . $menu['menu_name']];
- }
- return $menus;
- })->filterable(1);
- return $this->makePostForm('修改权限', $field, Url::buildUrl('/setting/update/' . $id)->suffix(false));
- }
-
- public function update(Request $request, $id)
- {
- if (!$id || !($menu = SystemMenusAdmin::get($id)))
- return $this->fail('数据不存在');
- $data = UtilService::postMore([
- 'menu_name',
- 'controller',
- ['module', 'admin'],
- 'action',
- 'params',
- ['icon', ''],
- ['menu_path', ''],
- ['api_url', ''],
- ['methods', ''],
- ['unique_auth', ''],
- ['sort', 0],
- ['pid', 0],
- ['is_header', 0],
- ['header', ''],
- ['auth_type', 0],
- ['access', 1],
- ['is_show', 0],
- ['is_show_path', 0],
- ], $request);
- if (!$data['menu_name'])
- return $this->fail('请输入按钮名称');
- if (SystemMenusAdmin::edit($data, $id))
- return $this->success('修改成功');
- else
- return $this->fail('修改失败');
- }
-
- public function delete($id)
- {
- if (!$id)
- return $this->fail('参数错误,请重新打开');
- $res = SystemMenusAdmin::delMenu($id);
- if (!$res)
- return $this->fail(SystemMenusAdmin::getErrorInfo('删除失败,请稍候再试!'));
- else
- return $this->success('删除成功!');
- }
-
- public function show($id)
- {
- if (!$id)
- return $this->fail('参数错误,请重新打开');
- [$show] = UtilService::postMore([['is_show', 0]], $this->request, true);
- if (SystemMenusAdmin::edit(['is_show' => $show], $id))
- return $this->success('修改成功');
- else
- return $this->fail('修改失败');
- }
- }
|