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
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
<?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 Notice extends Common {
    
    /**
     * 公告管理
     * @return string
     */
    public function index() {
        ////////
        return View::fetch();
    }
    /*
     * 获取公告列表
     */
    public function getNoticeList() {
        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 = 'id asc';
            $count = Db::name('notice')->where($condition)->count();
            $list = Db::name('notice')
                ->field("*")->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', 'notice-index');
        $id = input('id');
        $other['id'] = $id;
        View::assign('other', $other);
        return View::fetch();
    }
    
    public function getNoticeDetail() {
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $id = input('notice_id');
            if (!$id) {
                throw new Exception(lang('param_error'));
            }
            $info = \app\seller\model\Notice::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('notice')->update($data);
            $result['message'] = '操作成功';
            return $this->successResponse($result);
        }
        catch (\Exception $exc) {
            return $this->errorResponse($exc->getMessage());
        }
    }
    
    /**
     * 保存公告信息
     */
    public function saveNotice() {
        // 开始数据库事务
        Db::startTrans();
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            
            $data = Request::post();
            $validate = new \app\admin\validate\Notice();
            $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('notice')->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('notice')->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() {
        $notice_id = input('notice_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['notice_id'] = $notice_id;
                    $filed['title'] = $info['name'];
                    $filed['filename'] = $photo;
                    $filed['filesize'] = getfileSize('.' . $photo);
                    $filed['ext'] = strtoupper(getext($photo));
                    $filed['add_time'] = time();
                    Db::name('notice_attachment')->insertGetId($filed);
                }
            }
            return $this->successResponse(lang('successfully_uploaded'));
            ///////////////
        }
    }
    
    /*
     * 获取附件列表
     */
    public function getAttachmentList() {
        try {
            if (!Request::isPost()) {
                throw new Exception(lang('request_method_incorrect'));
            }
            $notice_id = input('notice_id');
            $where['notice_id'] = $notice_id;
            $list = Db::name('notice_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 deleteNotice() {
        // 启动事务
        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('notice')->field('id')->where($condition)->find();
            if (!$info) {
                throw new Exception(lang('delete_failed'));
            }
            $where['notice_id'] = $id;
            $attachmentList = Db::name('notice_attachment')->field('id,filename')->where($where)->select()->toArray();
            if ($attachmentList) {
                foreach ($attachmentList as $k => $v) {
                    delpic($v['filename']);
                }
                Db::name('notice_attachment')->where($where)->delete();//删除公告附件
            }
            Db::name('notice')->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('notice_attachment')->field('id,filename')->where($condition)->find();
            if (!$info) {
                throw new Exception(lang('delete_failed'));
            }
            Db::name('notice_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());
        }
        
    }
    
    
    
}