chengkun
2025-09-11 364a083e94138f7ed2d8114bf6dbdfda4eaf2683
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
<?php
 
namespace app\admin\controller;
 
use think\Exception;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;
use think\response\Json;
 
class OnlineMessage extends Common {
    public function index() {
        View::assign('menuitem', strtolower('onlineMessage-index'));
        return View::fetch();
    }
    
    /**
     * 获取在线留言列表
     * @return Json
     */
    public function get_online_message_list(): Json {
        try {
            if (!Request::isPost()) {
                throw new Exception('请求方式错误');
            }
            $page   = input('page', 1);
            $limit  = input('limit', 10);
            $kw     = input('kw', '');
            $status = input('status', '');
            $where  = [];
            if ($kw) {
                $where[] = ['name|phone|email|subject|message', 'like', "%{$kw}%"];
            }
            
            if (in_array($status, [0, 1])) {
                $where[] = ['status', '=', $status];
            }
            $total  = Db::name('online_message')->where($where)->count();
            $list   = Db::name('online_message')->where($where)->order('id desc')->page($page, $limit)->select()->toArray();
            $result = [
                'code'    => 200,
                'message' => '获取成功',
                'data'    => [
                    'list'  => $list,
                    'total' => $total,
                ],
            ];
        } catch (Exception $exc) {
            $result = [
                'code'    => $exc->getCode(),
                'message' => $exc->getMessage(),
            ];
        }
        return json($result);
    }
    
    /**
     * 修改留言状态
     * @return Json
     */
    public function change_message_status(): Json {
        try {
            if (!Request::isPost()) {
                throw new Exception('请求方式错误');
            }
            $id = input('id', 0);
            if (!$id || !is_numeric($id)) {
                throw new Exception('参数错误');
            }
            $info = Db::name('online_message')->where('id', $id)->find();
            if (!$info) {
                throw new Exception('留言不存在');
            }
            $status        = $info['status'] ? 0 : 1;
            $update_result = Db::name('online_message')->where('id', $id)->save(['status' => $status]);
            if (!$update_result) {
                throw new Exception('操作失败');
            }
            $result = [
                'code'    => 200,
                'message' => '操作成功',
            ];
        } catch (Exception $exc) {
            $result = [
                'code'    => $exc->getCode(),
                'message' => $exc->getMessage(),
            ];
        }
        return json($result);
    }
}