123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- namespace app\admin\controller;
- use think\admin\Controller;
- use think\admin\extend\DataExtend;
- use think\admin\service\AdminService;
- use think\admin\service\MenuService;
- use think\admin\service\NodeService;
- class Menu extends Controller
- {
-
- private $table = 'SystemMenu';
-
- public function index()
- {
- $this->title = '系统菜单管理';
- $this->_page($this->table, false);
- }
-
- protected function _index_page_filter(array &$data)
- {
- foreach ($data as &$vo) {
- if ($vo['url'] !== '#' && !preg_match('#^https?://#', $vo['url'])) {
- $vo['url'] = trim(url($vo['url']) . ($vo['params'] ? "?{$vo['params']}" : ''), '\\/');
- }
- $vo['ids'] = join(',', DataExtend::getArrSubIds($data, $vo['id']));
- }
- $data = DataExtend::arr2table($data);
- }
-
- public function add()
- {
- $this->_applyFormToken();
- $this->_form($this->table, 'form');
- }
-
- public function edit()
- {
- $this->_applyFormToken();
- $this->_form($this->table, 'form');
- }
-
- protected function _form_filter(array &$vo)
- {
- if ($this->request->isGet()) {
-
- if ($this->app->isDebug()) {
- AdminService::instance()->clearCache();
- }
-
- $vo['pid'] = $vo['pid'] ?? input('pid', '0');
-
- $this->auths = [];
- $this->nodes = MenuService::instance()->getList();
- foreach (NodeService::instance()->getMethods() as $node => $item) {
- if ($item['isauth'] && substr_count($node, '/') >= 2) {
- $this->auths[] = ['node' => $node, 'title' => $item['title']];
- }
- }
-
- $menus = $this->app->db->name($this->table)->order('sort desc,id asc')->column('id,pid,icon,url,node,title,params', 'id');
- $this->menus = DataExtend::arr2table(array_merge($menus, [['id' => '0', 'pid' => '-1', 'url' => '#', 'title' => '顶部菜单']]));
- if (isset($vo['id'])) foreach ($this->menus as $menu) if ($menu['id'] === $vo['id']) $vo = $menu;
- foreach ($this->menus as $key => $menu) if ($menu['spt'] >= 3 || $menu['url'] !== '#') unset($this->menus[$key]);
- if (isset($vo['spt']) && isset($vo['spc']) && in_array($vo['spt'], [1, 2]) && $vo['spc'] > 0) {
- foreach ($this->menus as $key => $menu) if ($vo['spt'] <= $menu['spt']) unset($this->menus[$key]);
- }
- }
- }
-
- protected function _form_result(bool $state)
- {
- if ($state) {
- $this->success('系统菜单修改成功!', 'javascript:location.reload()');
- }
- }
-
- public function state()
- {
- $this->_applyFormToken();
- $this->_save($this->table, $this->_vali([
- 'status.in:0,1' => '状态值范围异常!',
- 'status.require' => '状态值不能为空!',
- ]));
- }
-
- public function remove()
- {
- $this->_applyFormToken();
- $this->_delete($this->table);
- }
-
- protected function _add_form_result(bool $result)
- {
- if ($result) {
- $id = $this->app->db->name($this->table)->getLastInsID();
- sysoplog('系统菜单管理', "添加系统菜单[{$id}]成功");
- }
- }
-
- protected function _edit_form_result(bool $result)
- {
- if ($result) {
- $id = input('id') ?: 0;
- sysoplog('系统菜单管理', "修改系统菜单[{$id}]成功");
- }
- }
-
- protected function _state_save_result(bool $result)
- {
- if ($result) {
- [$id, $state] = [input('id'), input('status')];
- sysoplog('系统菜单管理', ($state ? '激活' : '禁用') . "系统菜单[{$id}]成功");
- }
- }
-
- protected function _remove_delete_result(bool $result)
- {
- if ($result) {
- $id = input('id') ?: 0;
- sysoplog('系统菜单管理', "删除系统菜单[{$id}]成功");
- }
- }
- }
|