chengkun
2025-09-04 2e12809f4d16aa00239b5e2c6a13a9a51842d134
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
<?php
 
namespace app\admin\util;
 
use think\Exception;
use think\facade\Config;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Session;
use think\facade\Cookie;
 
 
class MenuService {
    /**
     * 获取菜单列表
     * @param string $tableName
     * @param string $order
     * @return array
     */
    public function getMenuList(String $tableName, String $order){
        $menulist = Db::name($tableName)->field("*")->withAttr('langs', function ($langs) {
            if (empty($langs)) {
                return [];
            }
            return json_decode($langs, true);
        })->order($order)->select()->toArray();
        $list = _generateListTree($menulist, 0, ['id', 'father_id']);
        return $list;
    }
 
    /**
     * 更新菜单显示状态
     * @param string $tableName
     * @param int $id
     * @param string $showMenu
     * @return bool
     */
    public function upShowMenu(String $tableName, int $id, String $showMenu):bool {
        $data = [];
        $info = Db::name($tableName)->field('id')->where('id', $id)->find();
        if (empty($info)) {
            throw new Exception('信息不存在!', 400);
        }
        if ($showMenu == '1') {
            $data['show_menu'] = 1;
        } else {
            $data['show_menu'] = 0;
        }
        $backval = Db::name($tableName)->where('id', $id)->save($data);
        if ($backval === FALSE) {
            throw new Exception('更新失败!', 400);
        } else {
            $this->delCache($tableName);
            return true;
        }
        return false;
    }
    
    public function getMenuDetail(String $tableName, int $id){
        return Db::name($tableName)->field('*')->withAttr('langs', function ($langs) {
            if (empty($langs)) {
                return [];
            }
            return json_decode($langs, true);
        })->where('id', $id)->find();
    }
 
    /**
     * 检查菜单数据
     * @param array $data
     */
    public function validateData(array $data){
        $validate = new \app\admin\validate\SupplierMenu();
        $result = $validate->check($data);
        if (!$result) {
            throw new Exception($validate->getError(), 400);
        }
    }
 
    /**
     * 更新菜单
     * @param string $tableName
     * @param int $id
     * @param array $data
     * @return bool
     */
    public function upMenu(String $tableName, int $id, array $data) {
        $this->validateData($data);
        //更新数据
        //////保存////////////
        Db::name($tableName)->where('id', $id)->withoutField('id')->save($data);
        ////////////////////////
        $this->delCache($tableName);
    }
 
    /**
     * 添加菜单
     * @param string $tableName
     * @param int $id
     * @param array $data
     * @return bool
     */
    public function addMenu(String $tableName, int $id, array $data):bool {
        $this->validateData($data);
        $data['add_time'] = time();
        $new_id = Db::name($tableName)->insertGetId($data);
        if ($new_id) {
            Db::name($tableName)->where('id', $new_id)->save(['order_id' => $new_id]);
            ///////执行成功,提交事务///////
            $this->delCache($tableName);
            return true;
        } else {
            throw new Exception('菜单添加失败', 400);
        }
    }
 
    /**
     * 清除缓存
     * @return void
     */
    private function delCache($tableName) {
        Cache::tag($tableName)->clear();
    }
 
}