chengkun
2025-08-06 b54e02d98e42ae73071e3c01e59f12671d13d06a
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
 
namespace app\admin\controller;
 
use think\facade\Cache;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use think\Exception;
 
class Category extends Common {
    private $viewPath = 'category';
    
    /**
     * 菜单列表
     */
    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');
        }
    }
    
    public function getCategoryList() {
        if (!Request::isPost()) {
            return $this->errorResponse(lang('request_method_incorrect'));
        }
        try {
            $father_code = input('father_code');
            $where[] = ['father_code', '=', $father_code];
            $order = 'order_id asc,id asc';
            $list = Db::name('category')->field('*')->where($where)->order($order)->select()->toArray();
            if (!$list) {
                return $this->errorResponse('暂无菜单');
            }
            $reuslt['list'] = $list;
            return $this->successResponse($reuslt);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }
    
    ///获取平台列表
    public function getPlatformList() {
        try {
            if (!Request::isPost()) {
                throw new Exception('请求方式错误');
            }
            $order = 'order_id asc,docking_platform_id asc';
            $list = Db::name('docking_platform')->field('*')->order($order)->select()->toArray();
            if (!$list) {
                throw new Exception('暂无平台信息');
            }
            $reuslt['list'] = $list;
            return $this->successResponse($reuslt);
        } catch (Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }
    
    /**
     * 保存分类 信息
     * @return array|int[]
     */
    public function saveCategory() {
        Db::startTrans();
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $data = Request::post();
            $id = $data['id'];
            if (is_numeric($id)) {
                //更新数据
                $validate = new \app\admin\validate\Category();
                $result = $validate->check($data);
                if (!$result) {
                    throw new Exception($validate->getError());
                }
                //////保存////////////
                Db::name('category')->where('id', $id)->withoutField('id')->save($data);
                ////////////////////////
                ///////执行成功,提交事务///////
                Db::commit();
                $this->delCache();
                $res['message'] = '编辑成功';
                return $this->successResponse($res);
            } else {
                unset($data['id']);
                $data['status'] = 1;
                $validate = new \app\admin\validate\Category();
                $result = $validate->check($data);
                if (!$result) {
                    throw new Exception($validate->getError());
                }
                //////////////////////
                $new_id = Db::name('category')->insertGetId($data);
                if (!$new_id) {
                    throw new Exception('添加失败');
                }
                Db::name('category')->where('id', $new_id)->save(['order_id' => $new_id]);
                ///////执行成功,提交事务///////
                Db::commit();
                $this->delCache();
                $res['message'] = '添加成功';
                return $this->successResponse($res);
            }
        } catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->errorResponse($e->getMessage());
        }
        
    }
    
    //保存对接平台信息
    public function savePlatform() {
        Db::startTrans();
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $data = Request::post();
            $docking_platform_id = $data['docking_platform_id'];
            if (is_numeric($docking_platform_id)) {
                //更新数据
                $validate = new \app\admin\validate\DockingPlatform();
                $result = $validate->check($data);
                if (!$result) {
                    return $this->errorResponse($validate->getError());
                }
                //////保存////////////
                Db::name('docking_platform')->where('docking_platform_id', $docking_platform_id)->withoutField('docking_platform_id')->save($data);
                ////////////////////////
                Db::commit();
                $this->delDPCache();
                $res['message'] = '编辑成功';
                return $this->successResponse($res);
            } else {
                unset($data['docking_platform_id']);
                $validate = new \app\admin\validate\DockingPlatform();
                $result = $validate->check($data);
                if (!$result) {
                    return $this->errorResponse($validate->getError());
                }
                //////////////////////
                $new_id = Db::name('docking_platform')->insertGetId($data);
                if (!$new_id) {
                    throw new Exception('添加失败');
                }
                Db::name('docking_platform')->where('docking_platform_id', $new_id)->save(['order_id' => $new_id]);
                ///////执行成功,提交事务///////
                $this->delDPCache();
                Db::commit();
                $res['message'] = '添加成功';
                return $this->successResponse($res);
            }
        } catch (Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->errorResponse($e->getMessage());
        }
        
    }
    
    /**
     * 清除缓存
     * @return void
     */
    private function delCache() {
        Cache::tag('category')->clear();
    }
    
    private function delDPCache() {
        Cache::tag('docking_platform')->clear();
    }
    
    
}