138 lines
6.2 KiB
PHP
138 lines
6.2 KiB
PHP
<?php
|
||
// 后台商品分组管理:列表 + 新增 / 删除(删除前将相关商品归为「其他」避免孤儿)
|
||
require __DIR__ . '/../includes/init.php';
|
||
requireStaff();
|
||
require '_common.php';
|
||
$isAdmin = isAdminStaff();
|
||
|
||
$db = db();
|
||
$msg = '';
|
||
$err = '';
|
||
// 客服账号仅可查看,禁止写操作
|
||
if (!$isAdmin && $_SERVER['REQUEST_METHOD'] === 'POST') {
|
||
$err = '客服账号仅可查看,如需操作请联系管理员。';
|
||
}
|
||
// ---------- 新增 / 编辑保存 ----------
|
||
if (isset($_POST['save'])) {
|
||
verifyCsrf();
|
||
$id = (int) ($_POST['id'] ?? 0);
|
||
$name = trim($_POST['name'] ?? '');
|
||
$icon = trim($_POST['icon'] ?? 'fa-tags');
|
||
$sort = max(0, (int) ($_POST['sort_order'] ?? 0));
|
||
|
||
if ($name === '') {
|
||
$err = '请填写分组名称';
|
||
} elseif (mb_strlen($name) > 40) {
|
||
$err = '分组名称过长(最多 40 字)';
|
||
} else {
|
||
// 重名检查(排除自身)
|
||
$dup = $db->prepare('SELECT COUNT(*) FROM ' . tn('product_groups') . ' WHERE name = ? AND id <> ?');
|
||
$dup->execute([$name, $id]);
|
||
if ((int) $dup->fetchColumn() > 0) {
|
||
$err = '已存在同名分组,请换一个名称';
|
||
}
|
||
}
|
||
|
||
if ($err === '') {
|
||
if ($id > 0) {
|
||
$db->prepare('UPDATE ' . tn('product_groups') . ' SET name=?, icon=?, sort_order=? WHERE id=?')
|
||
->execute([$name, $icon, $sort, $id]);
|
||
$msg = '分组已更新';
|
||
} else {
|
||
$db->prepare('INSERT INTO ' . tn('product_groups') . ' (name, icon, sort_order, created_at) VALUES (?,?,?,NOW())')
|
||
->execute([$name, $icon, $sort]);
|
||
$msg = '分组已添加';
|
||
}
|
||
}
|
||
}
|
||
|
||
// ---------- 删除 ----------
|
||
if (isset($_POST['delete'])) {
|
||
verifyCsrf();
|
||
$id = (int) $_POST['delete'];
|
||
$row = $db->prepare('SELECT name FROM ' . tn('product_groups') . ' WHERE id = ?');
|
||
$row->execute([$id]);
|
||
$g = $row->fetch();
|
||
if ($g) {
|
||
// 将该分组下的商品归为「其他」,避免孤儿数据
|
||
$db->prepare('UPDATE ' . tn('products') . ' SET category = ? WHERE category = ?')
|
||
->execute(['其他', $g['name']]);
|
||
$db->prepare('DELETE FROM ' . tn('product_groups') . ' WHERE id = ?')->execute([$id]);
|
||
$msg = '分组「' . $g['name'] . '」已删除,相关商品已归为「其他」';
|
||
}
|
||
}
|
||
|
||
// ---------- 编辑回填 ----------
|
||
$edit = null;
|
||
if (isset($_GET['edit'])) {
|
||
$stmt = $db->prepare('SELECT * FROM ' . tn('product_groups') . ' WHERE id = ?');
|
||
$stmt->execute([(int) $_GET['edit']]);
|
||
$edit = $stmt->fetch();
|
||
}
|
||
$showForm = ($edit !== null) || (isset($_GET['act']) && $_GET['act'] === 'add');
|
||
|
||
// ---------- 列表(含商品计数) ----------
|
||
$list = $db->query(
|
||
'SELECT g.*, (SELECT COUNT(*) FROM ' . tn('products') . ' p WHERE p.category = g.name) AS cnt
|
||
FROM ' . tn('product_groups') . ' g ORDER BY g.sort_order ASC, g.id ASC'
|
||
)->fetchAll();
|
||
|
||
adminHeader('商品分组', 'groups');
|
||
?>
|
||
<h1 class="page-title"><i class="fas fa-tags"></i> 商品分组</h1>
|
||
<p class="muted" style="margin-bottom:16px;">分组用于首页分类筛选与商品归类。删除分组会将其下商品自动归为「其他」,不会丢失商品数据。</p>
|
||
|
||
<?php if ($msg): ?><p class="banner-ok"><i class="fas fa-check-circle"></i> <?= h($msg) ?></p><?php endif; ?>
|
||
<?php if ($err): ?><p class="form-err"><?= h($err) ?></p><?php endif; ?>
|
||
|
||
<?php if ($showForm): ?>
|
||
<div class="panel">
|
||
<h3><?= $edit ? '编辑分组 #' . (int) $edit['id'] : '新增分组' ?></h3>
|
||
<form method="post" action="" class="grid-form">
|
||
<?= csrfField() ?>
|
||
<input type="hidden" name="id" value="<?= $edit ? (int) $edit['id'] : 0 ?>">
|
||
<label>分组名称 *<input type="text" name="name" value="<?= $edit ? h($edit['name']) : '' ?>" required maxlength="40" placeholder="如 周边 / 数码"></label>
|
||
<label>排序(数字小靠前)<input type="number" name="sort_order" value="<?= $edit ? (int) $edit['sort_order'] : 0 ?>"></label>
|
||
<label class="span2">图标类名<input type="text" name="icon" value="<?= $edit ? h($edit['icon']) : 'fa-tags' ?>" placeholder="如 fa-tags(FontAwesome 类名)"></label>
|
||
<div class="form-actions span2">
|
||
<button type="submit" name="save" class="btn btn-primary"><i class="fas fa-save"></i> 保存</button>
|
||
<a href="groups.php" class="btn btn-ghost">取消</a>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
<?php endif; ?>
|
||
|
||
<div class="panel">
|
||
<div class="panel-head">
|
||
<h3>分组列表(共 <?= count($list) ?> 个)</h3>
|
||
<?php if (!$showForm): ?><a href="groups.php?act=add" class="btn btn-primary"><i class="fas fa-plus"></i> 新增分组</a><?php endif; ?>
|
||
</div>
|
||
<?php if (empty($list)): ?>
|
||
<p class="empty">还没有分组,点上方「新增分组」添加吧。</p>
|
||
<?php else: ?>
|
||
<table class="data-table">
|
||
<thead><tr><th>ID</th><th>图标</th><th>名称</th><th>排序</th><th>商品数</th><th>操作</th></tr></thead>
|
||
<tbody>
|
||
<?php foreach ($list as $g): ?>
|
||
<tr>
|
||
<td><?= (int) $g['id'] ?></td>
|
||
<td><i class="fas <?= h($g['icon']) ?>"></i></td>
|
||
<td><?= h($g['name']) ?></td>
|
||
<td><?= (int) $g['sort_order'] ?></td>
|
||
<td><?= (int) $g['cnt'] ?></td>
|
||
<td class="row-actions">
|
||
<a href="groups.php?edit=<?= (int) $g['id'] ?>" class="mini-btn"><i class="fas fa-edit"></i></a>
|
||
<form method="post" action="" style="display:inline" onsubmit="return confirm('确定删除该分组?其下商品将归为「其他」。');">
|
||
<?= csrfField() ?>
|
||
<button type="submit" name="delete" value="<?= (int) $g['id'] ?>" class="mini-btn danger"><i class="fas fa-trash"></i></button>
|
||
</form>
|
||
</td>
|
||
</tr>
|
||
<?php endforeach; ?>
|
||
</tbody>
|
||
</table>
|
||
<?php endif; ?>
|
||
</div>
|
||
|
||
<?php adminFooter(); ?>
|