chengkun
2025-09-11 364a083e94138f7ed2d8114bf6dbdfda4eaf2683
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
 
namespace app\admin\controller;
 
use think\facade\Cache;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
 
class Adminmenu extends Common {
    
    /**
     * 菜单列表
     */
    public function index() {
        ////////
        if (!Request::isPost()) {
            $other['title'] = '总共后台菜单';
            $guide['one']['text'] = '后台菜单管理';
            $guide['two']['text'] = '总后台菜单列表';
            View::assign('guide', $guide);
            View::assign('other', $other);
            return View::fetch('index');
        } else {
            $order = 'order_id asc,id asc';
            $menulist = Db::name('admin_menu')->field("id,title,menu_index,menu_icon,menu_url,show_menu,father_id,order_id")->order($order)->select()->toArray();
            $list = _generateListTree($menulist, 0, ['id', 'father_id']);;
            $result['list'] = $list;
            return $this->successResponse($result);
        }
    }
    
    /**
     * 更新菜单显示状态
     */
    public function updateShowMenu() {
        header('Content-Type:text/html;charset=utf-8');
        if (!Request::isPost()) {
            return $this->errorResponse(lang('request_method_incorrect'));
        }
        $id = input('id');
        if (!is_numeric($id)) {
            return $this->errorResponse('参数错误');
        }
        $show_menu = input('show_menu');
        $info = Db::name('admin_menu')->field('id')->where('id', $id)->find();
        if (empty($info)) {
            return $this->errorResponse('信息不存在');
        }
        if ($show_menu == '1') {
            $data['show_menu'] = 1;
        } else {
            $data['show_menu'] = 0;
        }
        $backval = Db::name('admin_menu')->where('id', $id)->save($data);
        if ($backval === FALSE) {
            return $this->errorResponse('更新失败');
        } else {
            $this->delCache();
            return $this->successResponse('更新成功');
        }
    }
    
    /**
     * 保存菜单
     * @return array|int[]
     */
    public function saveMenu() {
        if (!Request::isPost()) {
            return $this->errorResponse(lang('request_method_incorrect'));
        }
        $id = input('id');
        $data = Request::post();
        $id = $data['id'];
        if (is_numeric($id)) {
            //更新数据
            $validate = new \app\admin\validate\AdminMenu();
            $result = $validate->check($data);
            if (!$result) {
                return $this->errorResponse($validate->getError());
            }
            //////保存////////////
            Db::name('admin_menu')->where('id', $id)->withoutField('id')->save($data);
            ////////////////////////
            $this->delCache();
            $res['message'] = '菜单编辑成功';
            return $this->successResponse($res);
        } else {
            unset($data['id']);
            $validate = new \app\admin\validate\AdminMenu();
            $result = $validate->check($data);
            if (!$result) {
                return $this->errorResponse($validate->getError());
            }
            //////////////////////
            Db::startTrans();
            try {
                $data['add_time'] = time();
                $new_id = Db::name('admin_menu')->insertGetId($data);
                if ($new_id) {
                    Db::name('admin_menu')->where('id', $new_id)->save(['order_id' => $new_id]);
                    ///////执行成功,提交事务///////
                    Db::commit();
                    $this->delCache();
                    $res['message'] = '菜单添加成功';
                    return $this->successResponse($res);
                } else {
                    //任一执行失败,执行回滚操作
                    Db::rollback();
                    return $this->errorResponse('菜单添加失败');
                }
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return $this->errorResponse('菜单添加失败');
            }
        }
    }
    
    /**
     * 清除缓存
     * @return void
     */
    private function delCache() {
        Cache::tag('admin_menu')->clear();
    }
    
}