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
13 if ($act === 'save') {
14 $features = [];
15 foreach (preg_split('/[\r\n]+/', (string)($_POST['features'] ?? '')) as $line) {
16 $line = trim($line);
17 if ($line !== '') {
18 $features[] = $line;
19 }
20 }
21 $data = [
22 'id' => (int)($_POST['id'] ?? 0),
23 'name' => $_POST['name'] ?? '',
24 'category' => $_POST['category'] ?? '',
25 'description' => $_POST['description'] ?? '',
26 'features' => $features,
27 'image' => $_POST['image'] ?? '',
28 'link' => $_POST['link'] ?? '',
29 'status' => $_POST['status'] ?? 'active',
30 'sort' => (int)($_POST['sort'] ?? 50),
31 ];
32 $r = site_product_save($data);
33 if ($r === true) {
34 header('Location: ' . site_admin_url('site_products', 'saved=1'));
35 exit;
36 }
37 $msg = $r;
38 $msg_type = 'danger';
39 $form = $data;
40 $show_modal = true;
41 } elseif ($act === 'delete') {
42 $id = (int)($_POST['id'] ?? 0);
43 $ok = $id > 0 && site_product_delete($id);
44 header('Location: ' . site_admin_url('site_products', ($ok ? 'saved=1&msg=' . urlencode('产品已删除') : 'msg=' . urlencode('删除失败'))));
45 exit;
46 }
47}
48
49// —— GET 参数处理(redirect 后的提示)——
50$saved = isset($_GET['saved']);
51$msg = $_GET['msg'] ?? ($msg ?? '');
52$msg_type = $saved ? 'success' : ($msg_type ?? 'danger');
53if ($saved && $msg === '') {
54 $msg = '保存成功';
55}
56
57$products = site_product_list();
58$categories = site_product_categories();
59$title = $title ?? '产品管理';
60mnbt_admin_include('head');
61?>
62<div class="container-fluid p-t-15">
63 <div class="card">
64 <div class="card-header">
65 <h4 style="display:inline-block">产品管理</h4>
66 <span class="float-right">
67 <button type="button" class="btn btn-primary btn-sm" onclick="openEdit(0)">
68 <i class="mdi mdi-plus"></i> 添加产品
69 </button>
70 </span>
71 </div>
72 <div class="card-body">
73 <?php if (!empty($msg)): ?>
74 <div class="alert alert-<?= htmlspecialchars($msg_type ?? 'danger', ENT_QUOTES) ?>"><?= htmlspecialchars($msg) ?></div>
75 <?php endif; ?>
76
77 <?php if (empty($products)): ?>
78 <p class="text-muted">还没有产品,点击右上角"添加产品"创建第一个产品。</p>
79 <?php else: ?>
80 <div class="table-responsive">
81 <table class="table table-bordered table-hover">
82 <thead>
83 <tr>
84 <th style="width:60px">ID</th>
85 <th>产品名称</th>
86 <th style="width:110px">分类</th>
87 <th>简介</th>
88 <th style="width:130px">跳转链接</th>
89 <th style="width:80px">状态</th>
90 <th style="width:170px">操作</th>
91 </tr>
92 </thead>
93 <tbody>
94 <?php foreach ($products as $p): ?>
95 <tr>
96 <td><?= (int)$p['id'] ?></td>
97 <td><?= htmlspecialchars($p['name'], ENT_QUOTES) ?></td>
98 <td><?= htmlspecialchars($categories[$p['category']] ?? $p['category'], ENT_QUOTES) ?></td>
99 <td><?= htmlspecialchars(mb_substr($p['description'], 0, 40), ENT_QUOTES) ?><?= mb_strlen($p['description']) > 40 ? '…' : '' ?></td>
100 <td>
101 <?php if (!empty($p['link'])): ?>
102 <a href="<?= htmlspecialchars($p['link'], ENT_QUOTES) ?>" target="_blank" rel="noopener" class="text-primary"><?= htmlspecialchars(mb_substr($p['link'], 0, 22), ENT_QUOTES) ?><?= mb_strlen($p['link']) > 22 ? '…' : '' ?></a>
103 <?php else: ?>
104 <span class="text-muted">—</span>
105 <?php endif; ?>
106 </td>
107 <td>
108 <?php if ($p['status'] === 'active'): ?>
109 <span class="badge badge-success">上架</span>
110 <?php else: ?>
111 <span class="badge badge-secondary">下架</span>
112 <?php endif; ?>
113 </td>
114 <td>
115 <button type="button" class="btn btn-sm btn-outline-primary" onclick="openEdit(<?= (int)$p['id'] ?>)">编辑</button>
116 <form method="post" style="display:inline-block" onsubmit="var b=this.querySelector('button');b.disabled=true;b.textContent='删除中...';return confirm('确定删除此产品?')">
117 <input type="hidden" name="act" value="delete">
118 <input type="hidden" name="id" value="<?= (int)$p['id'] ?>">
119 <button type="submit" class="btn btn-sm btn-outline-danger">删除</button>
120 </form>
121 </td>
122 </tr>
123 <?php endforeach; ?>
124 </tbody>
125 </table>
126 </div>
127 <?php endif; ?>
128 </div>
129 </div>
130</div>
131
132<!-- 添加/编辑模态框 -->
133<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-hidden="true">
134 <div class="modal-dialog modal-lg" role="document">
135 <div class="modal-content">
136 <form method="post">
137 <input type="hidden" name="act" value="save">
138 <input type="hidden" name="id" id="f-id" value="0">
139 <div class="modal-header">
140 <h5 class="modal-title" id="modalTitle">添加产品</h5>
141 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
142 </div>
143 <div class="modal-body">
144 <div class="form-group">
145 <label>产品名称</label>
146 <input type="text" class="form-control" name="name" id="f-name" required>
147 </div>
148 <div class="form-group">
149 <label>产品分类</label>
150 <select class="form-control" name="category" id="f-category">
151 <?php foreach ($categories as $k => $v): ?>
152 <option value="<?= htmlspecialchars($k, ENT_QUOTES) ?>"><?= htmlspecialchars($v) ?></option>
153 <?php endforeach; ?>
154 </select>
155 </div>
156 <div class="form-group">
157 <label>产品简介</label>
158 <textarea class="form-control" name="description" id="f-description" rows="3" required></textarea>
159 </div>
160 <div class="form-group">
161 <label>产品特性(每行一条)</label>
162 <textarea class="form-control" name="features" id="f-features" rows="4" placeholder="高性能架构设计,确保系统稳定运行 易于集成和部署,降低实施成本"></textarea>
163 </div>
164 <div class="form-group">
165 <label>展示图片(URL,可选)</label>
166 <input type="text" class="form-control" name="image" id="f-image" placeholder="https://example.com/image.png">
167 </div>
168 <div class="form-group">
169 <label>跳转链接(可选)</label>
170 <input type="text" class="form-control" name="link" id="f-link" placeholder="/shop 或 https://example.com/product">
171 <small class="form-text text-muted">配置后,产品中心卡片点击将直接跳转到该链接(站内路径或 http(s) 外链);留空则进入产品详情页。</small>
172 </div>
173 <div class="form-row">
174 <div class="col-6">
175 <div class="form-group">
176 <label>状态</label>
177 <select class="form-control" name="status" id="f-status">
178 <option value="active">上架</option>
179 <option value="inactive">下架</option>
180 </select>
181 </div>
182 </div>
183 <div class="col-6">
184 <div class="form-group">
185 <label>排序(小到大)</label>
186 <input type="number" class="form-control" name="sort" id="f-sort" value="50" min="0">
187 </div>
188 </div>
189 </div>
190 </div>
191 <div class="modal-footer">
192 <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
193 <button type="submit" class="btn btn-primary">保存</button>
194 </div>
195 </form>
196 </div>
197 </div>
198</div>
199
200<script>
201var __siteProducts = <?= json_encode($products, JSON_UNESCAPED_UNICODE) ?>;
202<?php if (!empty($show_modal)): ?>
203var __siteForm = <?= json_encode($form, JSON_UNESCAPED_UNICODE) ?>;
204<?php endif; ?>
205function openEdit(id) {
206 var el = function (k) { return document.getElementById('f-' + k); };
207 document.getElementById('modalTitle').textContent = id ? '编辑产品' : '添加产品';
208 el('id').value = id;
209 if (id > 0) {
210 var p = __siteProducts.find(function (x) { return parseInt(x.id, 10) === parseInt(id, 10); });
211 if (p) {
212 el('name').value = p.name || '';
213 el('category').value = p.category || 'hosting';
214 el('description').value = p.description || '';
215 el('features').value = (p.features_list || []).join('\n');
216 el('image').value = p.image || '';
217 el('link').value = p.link || '';
218 el('status').value = p.status || 'active';
219 el('sort').value = p.sort || 50;
220 }
221 } else {
222 el('name').value = '';
223 el('category').value = 'hosting';
224 el('description').value = '';
225 el('features').value = '';
226 el('image').value = '';
227 el('link').value = '';
228 el('status').value = 'active';
229 el('sort').value = 50;
230 }
231 <?php if (!empty($show_modal)): ?>
232 // 提交失败回显:用服务端返回的表单数据覆盖字段
233 if (typeof __siteForm !== 'undefined') {
234 el('name').value = __siteForm.name || '';
235 el('category').value = __siteForm.category || 'hosting';
236 el('description').value = __siteForm.description || '';
237 el('features').value = (__siteForm.features || []).join('\n');
238 el('image').value = __siteForm.image || '';
239 el('link').value = __siteForm.link || '';
240 el('status').value = __siteForm.status || 'active';
241 el('sort').value = __siteForm.sort || 50;
242 }
243 <?php endif; ?>
244 $('#editModal').modal('show');
245}
246<?php if (!empty($show_modal)): ?>
247 $(function () { openEdit(<?= (int)($form['id'] ?? 0) ?>); });
248<?php endif; ?>
249</script>
250<?php mnbt_admin_include('foot'); ?>