chengkun
2025-09-04 0ea78440cb53b7ecf0ef715e3f51d88918c5821d
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\controller;
 
use think\facade\Cache;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use think\Exception;
 
use app\admin\util\MenuService;
 
class Menu extends Common {
    private $menuService;
    private $tableName = 'supplier_menu';
    private $viewPath = 'menu';
    
    
    public function __construct() {
        parent::__construct();
        $this->menuService = new MenuService();
    }
    
    /**
     * 菜单列表
     */
    public function index() {
        ////////
        if (!Request::isPost()) {
            $other['title'] = '供应商菜单';
            $guide['one']['text'] = '供应商管理';
            $guide['two']['text'] = '供应商列表';
            View::assign('guide', $guide);
            View::assign('other', $other);
            View::assign('viewPath', $this->viewPath);
            return View::fetch('index');
        } else {
            $order = 'order_id asc,id asc';
            $list = $this->menuService->getMenuList($this->tableName, $order);
            $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'));
        }
        try {
            $id = input('id');
            if (!is_numeric($id)) {
                throw new Exception('参数错误!', 400);
            }
            $showMenu = input('show_menu');
            $isUpdate = $this->menuService->upShowMenu($this->tableName, (int)$id, $showMenu);
            if (!$isUpdate) {
                throw new Exception('更新失败!', 400);
            }
            return $this->successResponse('更新成功');
        } catch (Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }
    /*
     * 获取菜单详情
     */
    public function getMenuDetail() {
        try {
            if (!Request::isPost()){
                throw new Exception(lang('request_method_incorrect'));
            }
            $id = input('id');
            if (!is_numeric($id)) {
                throw new Exception('参数错误!', 400);
            }
            $menuDetail = $this->menuService->getMenuDetail($this->tableName, (int)$id);
            if(!$menuDetail){
                throw new Exception('菜单不存在!', 400);
            }
            $result['info']=$menuDetail;
            return $this->successResponse($result);
        } catch (Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }
    
    /**
     * 保存菜单
     * @return array|int[]
     */
    public function saveMenu() {
        if (!Request::isPost()) {
            return $this->errorResponse(lang('request_method_incorrect'));
        }
        //启动事务
        Db::startTrans();
        try {
            $data = Request::post();
            $id = $data['id'];
            if (isset($data['langs']) && $data['langs']) {
                $data['langs'] = json_encode($data['langs'], JSON_UNESCAPED_UNICODE);
            }
            if (is_numeric($id)) {
                //更新数据
                $this->menuService->upMenu($this->tableName, (int)$id, $data);
                $res['message'] = '菜单编辑成功';
            } else {
                unset($data['id']);
                $this->menuService->addMenu($this->tableName, (int)$id, $data);
                $res['message'] = '菜单添加成功';
            }
            ///////执行成功,提交事务///////
            Db::commit();
            return $this->successResponse($res);
        } catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->errorResponse($e->getMessage());
        }
    }
}