better-staridc-MNBT
1<?php
2/**
3 * 管理员端 - 官网留言管理(列表分页 + 标记已读/删除)
4 */
5if (!defined('IN_CRONLITE')) {
6 exit;
7}
8
9// —— 处理 POST(成功后 redirect 到带提示参数的地址,避免刷新重复提交)——
10if ($_SERVER['REQUEST_METHOD'] === 'POST') {
11 $act = $_POST['act'] ?? '';
12 $id = (int)($_POST['id'] ?? 0);
13
14 if ($act === 'set_read') {
15 $read = (int)($_POST['read'] ?? 0) === 1;
16 $ok = $id > 0 && site_message_set_read($id, $read);
17 header('Location: ' . site_admin_url('site_messages', ($ok ? 'saved=1&msg=' . urlencode($read ? '已标记为已读' : '已标记为未读') : 'msg=' . urlencode('操作失败'))));
18 exit;
19 } elseif ($act === 'delete') {
20 $ok = $id > 0 && site_message_delete($id);
21 header('Location: ' . site_admin_url('site_messages', ($ok ? 'saved=1&msg=' . urlencode('留言已删除') : 'msg=' . urlencode('删除失败'))));
22 exit;
23 }
24}
25
26// —— GET 参数处理(redirect 后的提示)——
27$saved = isset($_GET['saved']);
28$msg = $_GET['msg'] ?? ($msg ?? '');
29$msg_type = $saved ? 'success' : ($msg_type ?? 'danger');
30if ($saved && $msg === '') {
31 $msg = '保存成功';
32}
33
34$per = 15;
35$page = max(1, (int)($_GET['page'] ?? 1));
36$pageData = site_message_list($page, $per);
37$messages = $pageData['list'];
38$total = $pageData['total'];
39$pages = (int)ceil($total / $per);
40global $DB;
41$unread = (int)($DB->get_row_prepare("SELECT COUNT(*) AS c FROM MN_plugin_site_message WHERE is_read=0")['c'] ?? 0);
42
43$title = $title ?? '留言管理';
44mnbt_admin_include('head');
45?>
46<div class="container-fluid p-t-15">
47 <div class="card">
48 <div class="card-header">
49 <h4 style="display:inline-block">留言管理</h4>
50 <span class="float-right"><span class="badge badge-warning">未读 <?= (int)$unread ?> 条</span></span>
51 </div>
52 <div class="card-body">
53 <?php if (!empty($msg)): ?>
54 <div class="alert alert-<?= htmlspecialchars($msg_type ?? 'danger', ENT_QUOTES) ?>"><?= htmlspecialchars($msg) ?></div>
55 <?php endif; ?>
56
57 <?php if (empty($messages)): ?>
58 <p class="text-muted">还没有收到留言。</p>
59 <?php else: ?>
60 <div class="table-responsive">
61 <table class="table table-bordered table-hover">
62 <thead>
63 <tr>
64 <th style="width:60px">ID</th>
65 <th style="width:140px">姓名</th>
66 <th style="width:200px">联系方式</th>
67 <th>留言内容</th>
68 <th style="width:90px">状态</th>
69 <th style="width:140px">提交时间</th>
70 <th style="width:200px">操作</th>
71 </tr>
72 </thead>
73 <tbody>
74 <?php foreach ($messages as $m): ?>
75 <tr class="<?= (int)$m['is_read'] === 0 ? 'table-warning' : '' ?>">
76 <td><?= (int)$m['id'] ?></td>
77 <td><?= htmlspecialchars($m['name'], ENT_QUOTES) ?></td>
78 <td>
79 <?php if ($m['email'] !== ''): ?><i class="mdi mdi-email-outline"></i> <?= htmlspecialchars($m['email'], ENT_QUOTES) ?><br><?php endif; ?>
80 <?php if ($m['phone'] !== ''): ?><i class="mdi mdi-phone-outline"></i> <?= htmlspecialchars($m['phone'], ENT_QUOTES) ?><?php endif; ?>
81 </td>
82 <td><?= htmlspecialchars(mb_substr($m['message'], 0, 60), ENT_QUOTES) ?><?= mb_strlen($m['message']) > 60 ? '…' : '' ?></td>
83 <td>
84 <?php if ((int)$m['is_read'] === 0): ?>
85 <span class="badge badge-warning">未读</span>
86 <?php else: ?>
87 <span class="badge badge-success">已读</span>
88 <?php endif; ?>
89 </td>
90 <td><?= htmlspecialchars($m['created_at'], ENT_QUOTES) ?></td>
91 <td>
92 <button type="button" class="btn btn-sm btn-outline-primary" onclick="viewMessage(<?= (int)$m['id'] ?>)">查看</button>
93 <form method="post" style="display:inline-block">
94 <input type="hidden" name="act" value="set_read">
95 <input type="hidden" name="id" value="<?= (int)$m['id'] ?>">
96 <input type="hidden" name="read" value="<?= (int)$m['is_read'] === 0 ? 1 : 0 ?>">
97 <button type="submit" class="btn btn-sm btn-outline-secondary"><?= (int)$m['is_read'] === 0 ? '标为已读' : '标为未读' ?></button>
98 </form>
99 <form method="post" style="display:inline-block" onsubmit="var b=this.querySelector('button');b.disabled=true;b.textContent='删除中...';return confirm('确定删除此留言?')">
100 <input type="hidden" name="act" value="delete">
101 <input type="hidden" name="id" value="<?= (int)$m['id'] ?>">
102 <button type="submit" class="btn btn-sm btn-outline-danger">删除</button>
103 </form>
104 </td>
105 </tr>
106 <?php endforeach; ?>
107 </tbody>
108 </table>
109 </div>
110
111 <?php if ($pages > 1): ?>
112 <nav aria-label="分页">
113 <ul class="pagination pagination-sm justify-content-end" style="margin-bottom:0">
114 <?php for ($i = 1; $i <= $pages; $i++): ?>
115 <li class="page-item<?= $i === $page ? ' active' : '' ?>">
116 <a class="page-link" href="<?= htmlspecialchars(site_admin_url('site_messages', 'page=' . $i), ENT_QUOTES) ?>"><?= $i ?></a>
117 </li>
118 <?php endfor; ?>
119 </ul>
120 </nav>
121 <?php endif; ?>
122 <?php endif; ?>
123 </div>
124 </div>
125</div>
126
127<!-- 留言详情模态框 -->
128<div class="modal fade" id="viewModal" tabindex="-1" role="dialog" aria-hidden="true">
129 <div class="modal-dialog" role="document">
130 <div class="modal-content">
131 <div class="modal-header">
132 <h5 class="modal-title">留言详情</h5>
133 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
134 </div>
135 <div class="modal-body" id="viewBody"></div>
136 <div class="modal-footer">
137 <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
138 </div>
139 </div>
140 </div>
141</div>
142
143<script>
144var __siteMessages = <?= json_encode($messages, JSON_UNESCAPED_UNICODE) ?>;
145function esc(s) {
146 return String(s == null ? '' : s).replace(/[&<>"]/g, function (c) {
147 return { '&': '&', '<': '<', '>': '>', '"': '"' }[c];
148 });
149}
150function viewMessage(id) {
151 var m = __siteMessages.find(function (x) { return parseInt(x.id, 10) === parseInt(id, 10); });
152 if (!m) { return; }
153 var contact = [];
154 if (m.email) { contact.push('邮箱:' + esc(m.email)); }
155 if (m.phone) { contact.push('电话:' + esc(m.phone)); }
156 document.getElementById('viewBody').innerHTML =
157 '<p><strong>' + esc(m.name) + '</strong> <span class="text-muted">(' + esc(m.created_at) + ')</span></p>' +
158 (contact.length ? '<p class="text-muted">' + contact.join(' ') + '</p>' : '') +
159 '<hr><p style="white-space:pre-wrap;word-break:break-word;">' + esc(m.message) + '</p>';
160 $('#viewModal').modal('show');
161}
162</script>
163<?php mnbt_admin_include('foot'); ?>