better-staridc-MNBT
1<?php
2/**
3 * official_site 插件 - 核心函数库
4 *
5 * 提供:产品/新闻/留言的 CRUD 与前台数据查询,供 bootstrap.php 与 admin 视图使用。
6 */
7
8if (!defined('IN_CRONLITE')) {
9 exit;
10}
11
12/* ============================================================
13 * 辅助函数
14 * ============================================================ */
15
16/** 管理员端插件页面 URL。 */
17function site_admin_url($page, $extra = '')
18{
19 $base = 'plugin.php?p=official_site&page=' . rawurlencode($page);
20 if ($extra !== '') {
21 $base .= '&' . ltrim($extra, '&');
22 }
23 return $base;
24}
25
26/** 输出 JSON 并退出。 */
27function site_json($code, $extra = [])
28{
29 @header('Content-Type: application/json; charset=UTF-8');
30 $payload = ['code' => $code];
31 if (is_array($extra)) {
32 $payload = array_merge($payload, $extra);
33 }
34 echo json_encode($payload, JSON_UNESCAPED_UNICODE);
35 exit;
36}
37
38/** 产品分类定义(MNBT 业务语境)。 */
39function site_product_categories()
40{
41 return [
42 'ai' => 'AI 智能',
43 'cloud' => '云服务器',
44 'hosting' => '虚拟主机',
45 'domain' => '域名服务',
46 'security' => '安全防护',
47 'service' => '增值服务',
48 ];
49}
50
51/* ============================================================
52 * 产品
53 * ============================================================ */
54
55/** 解析产品特性(JSON 字符串 ↔ 数组)。 */
56function site_product_features_parse($row)
57{
58 $features = [];
59 $raw = trim((string)($row['features'] ?? ''));
60 if ($raw !== '') {
61 $decoded = json_decode($raw, true);
62 if (is_array($decoded)) {
63 $features = array_values(array_filter(array_map('trim', $decoded), function ($v) {
64 return $v !== '';
65 }));
66 }
67 }
68 return $features;
69}
70
71/** 产品表结构迁移:确保 link 列存在(老版本升级,幂等)。 */
72function site_product_ensure_schema()
73{
74 static $done = false;
75 if ($done) {
76 return;
77 }
78 $done = true;
79 global $DB;
80 $cols = $DB->get_all_prepare("SHOW COLUMNS FROM `MN_plugin_site_product` LIKE 'link'") ?: [];
81 if (empty($cols)) {
82 @$DB->query("ALTER TABLE `MN_plugin_site_product` ADD COLUMN `link` varchar(500) NOT NULL DEFAULT '' COMMENT '跳转链接' AFTER `image`");
83 }
84}
85
86/**
87 * 产品列表。
88 * @param string $status ''=全部,'active'/'inactive'
89 * @param string $category ''=全部,或具体分类
90 */
91function site_product_list($status = '', $category = '')
92{
93 global $DB;
94 site_product_ensure_schema();
95 $sql = "SELECT * FROM MN_plugin_site_product";
96 $where = [];
97 $args = [];
98 if ($status !== '') {
99 $where[] = "status = ?";
100 $args[] = $status;
101 }
102 if ($category !== '' && $category !== 'all') {
103 $where[] = "category = ?";
104 $args[] = $category;
105 }
106 if (!empty($where)) {
107 $sql .= " WHERE " . implode(' AND ', $where);
108 }
109 $sql .= " ORDER BY sort ASC, id ASC";
110 $rows = $DB->get_all_prepare($sql, $args) ?: [];
111 $list = [];
112 foreach ($rows as $r) {
113 $r['features_list'] = site_product_features_parse($r);
114 $list[] = $r;
115 }
116 return $list;
117}
118
119/** 产品详情(含特性数组)。 */
120function site_product_get($id)
121{
122 global $DB;
123 site_product_ensure_schema();
124 $row = $DB->get_row_prepare("SELECT * FROM MN_plugin_site_product WHERE id=? LIMIT 1", [(int)$id]) ?: null;
125 if ($row) {
126 $row['features_list'] = site_product_features_parse($row);
127 }
128 return $row;
129}
130
131/** 保存产品(新增/更新),返回 true 或错误消息。 */
132function site_product_save($data)
133{
134 global $DB;
135 $now = date('Y-m-d H:i:s');
136 $features = isset($data['features']) && is_array($data['features']) ? $data['features'] : [];
137 $features = array_values(array_filter(array_map('trim', $features), function ($v) {
138 return $v !== '';
139 }));
140 $fields = [
141 'name' => trim((string)($data['name'] ?? '')),
142 'category' => trim((string)($data['category'] ?? '')),
143 'description' => trim((string)($data['description'] ?? '')),
144 'features' => json_encode($features, JSON_UNESCAPED_UNICODE),
145 'image' => trim((string)($data['image'] ?? '')),
146 'link' => trim((string)($data['link'] ?? '')),
147 'status' => ($data['status'] ?? 'active') === 'inactive' ? 'inactive' : 'active',
148 'sort' => max(0, (int)($data['sort'] ?? 50)),
149 ];
150 if ($fields['name'] === '') {
151 return '产品名称不能为空';
152 }
153 if ($fields['category'] === '') {
154 return '请选择产品分类';
155 }
156
157 $id = (int)($data['id'] ?? 0);
158 if ($id > 0) {
159 $ok = $DB->query_prepare(
160 "UPDATE MN_plugin_site_product SET name=?, category=?, description=?, features=?, image=?, link=?, status=?, sort=? WHERE id=?",
161 [$fields['name'], $fields['category'], $fields['description'], $fields['features'], $fields['image'], $fields['link'], $fields['status'], $fields['sort'], $id]
162 );
163 return $ok ? true : '更新失败';
164 }
165 $ok = $DB->query_prepare(
166 "INSERT INTO MN_plugin_site_product (name, category, description, features, image, link, status, sort, created_at) VALUES (?,?,?,?,?,?,?,?,?)",
167 [$fields['name'], $fields['category'], $fields['description'], $fields['features'], $fields['image'], $fields['link'], $fields['status'], $fields['sort'], $now]
168 );
169 return $ok ? true : '新增失败';
170}
171
172/** 删除产品。 */
173function site_product_delete($id)
174{
175 global $DB;
176 return (bool)$DB->query_prepare("DELETE FROM MN_plugin_site_product WHERE id=? LIMIT 1", [(int)$id]);
177}
178
179/* ============================================================
180 * 新闻
181 * ============================================================ */
182
183/** 新闻分类统计(前台侧边栏)。 */
184function site_news_categories()
185{
186 global $DB;
187 return $DB->get_all_prepare(
188 "SELECT category, COUNT(*) AS cnt FROM MN_plugin_site_news WHERE status='active' AND category != '' GROUP BY category ORDER BY cnt DESC, category ASC"
189 ) ?: [];
190}
191
192/**
193 * 新闻分页列表。
194 * @param string $status 'active'=仅上架(默认,前台);''=全部(管理端)
195 */
196function site_news_list($page = 1, $per = 6, $category = '', $status = 'active')
197{
198 global $DB;
199 $page = max(1, (int)$page);
200 $per = max(1, min(100, (int)$per));
201 $where = [];
202 $args = [];
203 if ($status !== '') {
204 $where[] = "status = ?";
205 $args[] = $status;
206 }
207 if ($category !== '' && $category !== 'all') {
208 $where[] = "category = ?";
209 $args[] = $category;
210 }
211 $whereSql = $where === [] ? '' : (' WHERE ' . implode(' AND ', $where));
212 $total = (int)($DB->get_row_prepare("SELECT COUNT(*) AS c FROM MN_plugin_site_news" . $whereSql, $args)['c'] ?? 0);
213 $offset = ($page - 1) * $per;
214 $rows = $DB->get_all_prepare(
215 "SELECT * FROM MN_plugin_site_news" . $whereSql . " ORDER BY sort ASC, id DESC LIMIT {$offset},{$per}",
216 $args
217 ) ?: [];
218 return ['total' => $total, 'list' => $rows];
219}
220
221/** 新闻详情($incViews 为 true 时累计浏览量)。 */
222function site_news_get($id, $incViews = false)
223{
224 global $DB;
225 $id = (int)$id;
226 $row = $DB->get_row_prepare("SELECT * FROM MN_plugin_site_news WHERE id=? LIMIT 1", [$id]) ?: null;
227 if ($row && $incViews) {
228 $DB->query_prepare("UPDATE MN_plugin_site_news SET views = views + 1 WHERE id=?", [$id]);
229 $row['views'] = (int)$row['views'] + 1;
230 }
231 return $row;
232}
233
234/** 热门新闻(按浏览量)。 */
235function site_news_popular($limit = 4)
236{
237 global $DB;
238 $limit = max(1, min(20, (int)$limit));
239 return $DB->get_all_prepare(
240 "SELECT id, title, views, created_at FROM MN_plugin_site_news WHERE status='active' ORDER BY views DESC, id DESC LIMIT " . $limit
241 ) ?: [];
242}
243
244/** 保存新闻(新增/更新),返回 true 或错误消息。 */
245function site_news_save($data)
246{
247 global $DB;
248 $now = date('Y-m-d H:i:s');
249 $fields = [
250 'title' => trim((string)($data['title'] ?? '')),
251 'category' => trim((string)($data['category'] ?? '')),
252 'content' => (string)($data['content'] ?? ''),
253 'status' => ($data['status'] ?? 'active') === 'inactive' ? 'inactive' : 'active',
254 'sort' => max(0, (int)($data['sort'] ?? 50)),
255 ];
256 if ($fields['title'] === '') {
257 return '新闻标题不能为空';
258 }
259 if ($fields['content'] === '') {
260 return '新闻内容不能为空';
261 }
262
263 $id = (int)($data['id'] ?? 0);
264 if ($id > 0) {
265 $ok = $DB->query_prepare(
266 "UPDATE MN_plugin_site_news SET title=?, category=?, content=?, status=?, sort=? WHERE id=?",
267 [$fields['title'], $fields['category'], $fields['content'], $fields['status'], $fields['sort'], $id]
268 );
269 return $ok ? true : '更新失败';
270 }
271 $ok = $DB->query_prepare(
272 "INSERT INTO MN_plugin_site_news (title, category, content, views, status, sort, created_at) VALUES (?,?,?,0,?,?,?)",
273 [$fields['title'], $fields['category'], $fields['content'], $fields['status'], $fields['sort'], $now]
274 );
275 return $ok ? true : '新增失败';
276}
277
278/** 删除新闻。 */
279function site_news_delete($id)
280{
281 global $DB;
282 return (bool)$DB->query_prepare("DELETE FROM MN_plugin_site_news WHERE id=? LIMIT 1", [(int)$id]);
283}
284
285/* ============================================================
286 * 留言
287 * ============================================================ */
288
289/** 新增留言(前台提交),返回 true 或错误消息。 */
290function site_message_add($data)
291{
292 global $DB;
293 $now = date('Y-m-d H:i:s');
294 $fields = [
295 'name' => trim((string)($data['name'] ?? '')),
296 'email' => trim((string)($data['email'] ?? '')),
297 'phone' => trim((string)($data['phone'] ?? '')),
298 'message' => trim((string)($data['message'] ?? '')),
299 ];
300 if ($fields['name'] === '') {
301 return '请输入姓名';
302 }
303 if ($fields['email'] === '' || !filter_var($fields['email'], FILTER_VALIDATE_EMAIL)) {
304 return '请输入有效的邮箱地址';
305 }
306 if ($fields['message'] === '') {
307 return '请输入留言内容';
308 }
309 $ok = $DB->query_prepare(
310 "INSERT INTO MN_plugin_site_message (name, email, phone, message, is_read, created_at) VALUES (?,?,?,?,0,?)",
311 [$fields['name'], $fields['email'], $fields['phone'], $fields['message'], $now]
312 );
313 return $ok ? true : '留言提交失败,请稍后重试';
314}
315
316/** 留言分页列表(管理端)。 */
317function site_message_list($page = 1, $per = 15)
318{
319 global $DB;
320 $page = max(1, (int)$page);
321 $per = max(1, min(100, (int)$per));
322 $total = (int)($DB->get_row_prepare("SELECT COUNT(*) AS c FROM MN_plugin_site_message")['c'] ?? 0);
323 $offset = ($page - 1) * $per;
324 $rows = $DB->get_all_prepare(
325 "SELECT * FROM MN_plugin_site_message ORDER BY id DESC LIMIT {$offset},{$per}"
326 ) ?: [];
327 return ['total' => $total, 'list' => $rows];
328}
329
330/** 删除留言。 */
331function site_message_delete($id)
332{
333 global $DB;
334 return (bool)$DB->query_prepare("DELETE FROM MN_plugin_site_message WHERE id=? LIMIT 1", [(int)$id]);
335}
336
337/** 标记留言已读/未读。 */
338function site_message_set_read($id, $read)
339{
340 global $DB;
341 return (bool)$DB->query_prepare("UPDATE MN_plugin_site_message SET is_read=? WHERE id=?", [$read ? 1 : 0, (int)$id]);
342}