chengkun
2025-09-13 36fcee83fd60816d65f7c06840f5b8f92c01484a
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
 
namespace app\admin\controller;
 
 
use think\Exception;
use think\facade\Config;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
 
//商品零售价限制
 
class Message extends Common {
    
    private $viewPath = 'Message';
    /**
     * 公告管理
     * @return string
     */
    public function index() {
        View::assign('viewPath', $this->viewPath);
        return View::fetch();
    }
    /*
     * 获取消息列表
     */
    public function getMessageList() {
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $condition = [];
            $kw = input('kw');
            if ($kw) {
                $condition[] = ['title', 'like', '%' . $kw . '%'];
            }
            $p = input('p', 1);
            $other['page_size'] = input('page_size', 20);
            $order = 'M.id asc';
            $count = Db::name('message')
                ->alias('M')
                ->join('msg_category MC','M.msg_category_id=MC.id','left')
                ->where($condition)->count();
            $list = Db::name('message')
                ->alias('M')
                ->join('msg_category MC','M.msg_category_id=MC.id','left')
                ->field("M.*,MC.title as catetitle")->where($condition)->order($order)->withAttr('publish_time', function ($value) {
                    return date('Y-m-d H:i:s', $value);
                })->page($p, $other['page_size'])->select()->toArray();
            $other['count'] = $count;
            if ($list) {
                $result['list'] = $list;
                $result['other'] = $other;
                return $this->successResponse($result);
            } else {
                throw new Exception(lang('no_data_found'));
            }
        } catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /**
     * 创建消息
     */
    public function create() {
        ////////
        View::assign('menuitem', 'message-index');
        $id = input('id');
        $other['id'] = $id;
        View::assign('other', $other);
        View::assign('viewPath', $this->viewPath);
        return View::fetch();
    }
    
    public function getMessageDetail() {
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $id = input('message_id');
            if (!$id) {
                throw new Exception(lang('param_error'));
            }
            $info = \app\admin\model\Message::field("*")->where('id', $id)->withAttr('publish_time', function ($value) {return date('Y-m-d H:i:s', $value);})->find();
            if ($info) {
                $result['info']=$info;
                return $this->successResponse($result);
            } else {
                throw new Exception(lang('no_data_found'));
            }
        } catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    /*
     * 更新消息状态
 */
    function updateExt() {
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $postField = 'id,status';
            $data = Request::only(explode(',', $postField), 'post');
            if (!$data['id']) {
                throw new Exception(lang('parameter_error'));
            }
            Db::name('message')->update($data);
            $result['message'] = '操作成功';
            return $this->successResponse($result);
        }
        catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /**
     * 保存消息信息
     */
    public function saveMessage() {
        // 开始数据库事务
        Db::startTrans();
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            
            $data = Request::post();
            $validate = new \app\admin\validate\Message();
            $check_result = $validate->check($data);
            if (!$check_result) {
                throw new Exception($validate->getError());
            }
            $id = input('post.id');
            if (is_numeric($id)) {
                $data['publish_time'] = strtotime($data['publish_time']);
                $data['update_time'] = time();
                Db::name('message')->where('id', $id)->update($data);//更新数据
                // 提交事务
                Db::commit();
                $result['id'] = $id;
                $result['message'] = lang('successfully_saved');
                return $this->successResponse($result);
            } else {
                unset($data['id']);
                $data['publish_time'] = strtotime($data['publish_time']);
                $data['add_time'] = time();
                $data['update_time'] = time();
                $newid = Db::name('message')->insertGetId($data);
                Db::commit();
                $result['id'] = $newid;
                $result['message'] = lang('successfully_saved');
                return $this->successResponse($result);
            }
        } catch (\Exception $exc) {
            // 回滚事务
            Db::rollback();
            return $this->errorResponse($exc->getMessage());
        }
    }
    /*
     * 上传附件
     */
    public function uploadAttachment() {
        $message_id = input('message_id');
        $ext_arr = array(
            'image' => array('gif', 'jpg', 'jpeg', 'bmp', 'png'),
            'files' => array('doc', 'docx', 'xls', 'xlsx', 'pptx', 'ppt', 'pdf'),
        );
        $maxSizeArr = array(
            'image' => 1024 * 1024 * 10,
            'files' => 1024 * 1024 * 250,
        );
        
        $upload = new \common\UploadFile(); // 实例化上传类
        $upload->uploadReplace = TRUE; //同名文件自动覆盖
        $upload->autoSub = TRUE; // 启用子目录保存文件
        $upload->subType = 'date';  // 子目录创建方式 可以使用hash date custom
        $upload->dateFormat = 'Y/m/d'; //定义 subType=date 后,才需要定义dateFormat
        $upload->maxSize = 1024 * 1024 * 0; // 设置附件上传大小 10M
        $upload->maxSizeArr = $maxSizeArr; // 设置附件上传大小 10M
        $upload->allowExts = $ext_arr; // 设置附件上传类型
        $upload->savePath = './static/upload/'; // 设置附件上传目录
        $backinfo = $upload->uploadmore();
        if (!$backinfo) {// 上传错误提示错误信息
            return array('code' => 0, 'message' => $upload->getErrorMsg());
        } else {
            // 上传成功 获取上传文件信息
            foreach ($backinfo as $info) {
                $photo = $info['savename'];
                if (!empty($photo)) {
                    $filed['message_id'] = $message_id;
                    $filed['title'] = $info['name'];
                    $filed['filename'] = $photo;
                    $filed['filesize'] = getfileSize('.' . $photo);
                    $filed['ext'] = strtoupper(getext($photo));
                    $filed['add_time'] = time();
                    Db::name('message_attachment')->insertGetId($filed);
                }
            }
            return $this->successResponse(lang('successfully_uploaded'));
            ///////////////
        }
    }
    
    /*
     * 获取附件列表
     */
    public function getAttachmentList() {
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $message_id = input('message_id');
            $where['message_id'] = $message_id;
            $list = Db::name('message_attachment')->field('id,title,filename,filesize,ext')->where($where)->withAttr('filesize', function ($value) {
                return getsizebytype($value);
            })->order('id asc')->select()->toArray();///
            if ($list) {
                $result['list'] = $list;
                return $this->successResponse($result);
            } else {
                throw new Exception(lang('no_data_found'));
            }
        } catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    /*
     * 删除消息
     */
    public function deleteMessage() {
        // 启动事务
        Db::startTrans();
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $id = input('id');
            if (!$id || !is_numeric($id)) {
                throw new Exception(lang('parameter_error'));
            }
            $condition['id'] = $id;
            $info = Db::name('message')->field('id')->where($condition)->find();
            if (!$info) {
                throw new Exception(lang('delete_failed'));
            }
            $where['message_id'] = $id;
            $attachmentList = Db::name('message_attachment')->field('id,filename')->where($where)->select()->toArray();
            if ($attachmentList) {
                foreach ($attachmentList as $k => $v) {
                    delpic($v['filename']);
                }
                Db::name('message_attachment')->where($where)->delete();//删除消息附件
            }
            Db::name('message')->where('id', $id)->delete();//删除消息
            ////执行成功,提交事务
            Db::commit();
            return $this->successResponse(lang('delete_successful'));
        } catch (\Exception $exc) {
            //// 回滚事务
            Db::rollback();
            return $this->errorResponse($exc->getMessage());
        }
        
    }
    /*
     * 删除消息附件
     */
    public function deleteAttachment() {
        // 启动事务
        Db::startTrans();
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
 
            $id = input('id');
            if (!$id || !is_numeric($id)) {
                throw new Exception(lang('parameter_error'));
            }
            $condition['id'] = $id;
            $info = Db::name('message_attachment')->field('id,filename')->where($condition)->find();
            if (!$info) {
                throw new Exception(lang('delete_failed'));
            }
            Db::name('message_attachment')->where('id', $id)->delete();//删除消息附件
            delpic($info['filename']);
            ////执行成功,提交事务
            Db::commit();
            return $this->successResponse(lang('delete_successful'));
        } catch (\Exception $exc) {
            //// 回滚事务
            Db::rollback();
            return $this->errorResponse($exc->getMessage());
        }
        
    }
    /*
     * 获取消息类别列表
     */
    public function getMsgCategoryList() {
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $message_id = input('message_id');
            $where['ifshow'] = 1;
            $list = Db::name('msg_category')->field('id,title,father_id')->where($where)->order('order_id asc,id asc')->select()->toArray();///
            if ($list) {
                
                $result['list'] = datalist($list, 'father_id','',true);
                return $this->successResponse($result);
            } else {
                throw new Exception(lang('no_data_found'));
            }
        } catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    
}