评价本身也能填「观后感受」

- lib/db.php:reviews 表新增 feelings 列。建表语句里带上,同时补一条
  ALTER TABLE 迁移 —— 上一版已经上线过没有该列的 reviews 表,老库要能自动补列。
- lib/reviews.php:saveReview() 接收感受并落库;getMovieReviews()/getMyReview()
  把 feelings 解析成 key 列表(旧记录为 NULL → 空数组,不报错)。
- detail.php:评价表单加了「我看完的感受(可多选)」,与讨论用的是同一套感受;
  修改评价时原勾选自动回填,取消勾选即清空;已保存的感受在评价正文下方以只读标签展示。

感受仍然只存 key(happy/sad/…),emoji 与文案统一在 feelingCatalog() 里维护。
This commit is contained in:
2026-09-22 13:03:04 +08:00
parent a2c26ad002
commit dc5e788f27
4 changed files with 71 additions and 21 deletions
+41 -8
View File
@@ -35,14 +35,16 @@ if (isset($_SESSION['rv_flash'])) {
$rvError = ''; // 评价表单的错误
$rvFormRating = 0;
$rvFormContent = '';
$rvFormFeelings = []; // 评价表单里已勾选的观后感受
$rvCommentErrId = 0; // 哪一条评价的讨论表单报错
$rvCommentErr = '';
// 当前账号已有的评价(有则表单变成「修改」)
$myReview = getMyReview($id, $adminId);
if ($myReview) {
$rvFormRating = (int)$myReview['rating'];
$rvFormContent = $myReview['content'];
$rvFormRating = (int)$myReview['rating'];
$rvFormContent = $myReview['content'];
$rvFormFeelings = $myReview['feelings'];
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $adminId > 0) {
@@ -52,10 +54,13 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && $adminId > 0) {
// movie_id 必须对得上:表单里的 id 是客户端可控的,防止写到别的电影上
if ($postedMovie === $id) {
if ($rvAction === 'save_review') {
$rating = (int)($_POST['rating'] ?? 0);
$content = trim((string)($_POST['content'] ?? ''));
$rvFormRating = $rating;
$rvFormContent = $content;
$rating = (int)($_POST['rating'] ?? 0);
$content = trim((string)($_POST['content'] ?? ''));
$feelings = normalizeFeelings($_POST['feelings'] ?? []);
$rvFormRating = $rating;
$rvFormContent = $content;
// 出错重渲染时把用户勾过的感受带回去,不用重新勾一遍
$rvFormFeelings = $feelings;
if ($rating < 1 || $rating > 10) {
$rvError = '请先选一个 1-10 的评分';
@@ -63,7 +68,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && $adminId > 0) {
$rvError = '评价内容不能为空';
} elseif (reviewTextLength($content) > REVIEW_CONTENT_MAX) {
$rvError = '评价内容太长了(最多 ' . REVIEW_CONTENT_MAX . ' 字)';
} elseif (saveReview($id, $adminId, $rating, $content)) {
} elseif (saveReview($id, $adminId, $rating, $content, $feelings)) {
$_SESSION['rv_flash'] = ['type' => 'success', 'msg' => $myReview ? '评价已更新' : '评价已发布'];
header('Location: detail.php?id=' . $id . '#reviews');
exit;
@@ -685,6 +690,10 @@ $hasGallery = !empty($gallery);
gap: 6px;
margin: 4px 0 2px;
}
/* 评价正文下面展示的感受,跟正文一样缩进到头像右侧 */
.rv-item > .rv-feelings {
margin-left: 44px;
}
.rv-comment-form {
margin-left: 44px;
}
@@ -759,7 +768,8 @@ $hasGallery = !empty($gallery);
.rv-content,
.rv-updated,
.rv-comments,
.rv-comment-form {
.rv-comment-form,
.rv-item > .rv-feelings {
margin-left: 0;
}
}
@@ -1074,6 +1084,19 @@ $hasGallery = !empty($gallery);
<?php endif; ?>
<textarea name="content" class="rv-textarea" maxlength="<?= REVIEW_CONTENT_MAX ?>"
placeholder="这部电影怎么样?剧情、表演、画面、音效……随便聊聊"><?= h($rvFormContent) ?></textarea>
<div class="rv-feeling-pick">
<span class="rv-pick-label">我看完的感受(可多选)</span>
<div class="feel-chips">
<?php foreach ($feelings as $fkey => $f): ?>
<label class="feel-chip feel-pick" title="<?= h($f['label']) ?>">
<input type="checkbox" name="feelings[]" value="<?= h($fkey) ?>"<?= in_array($fkey, $rvFormFeelings, true) ? ' checked' : '' ?>>
<span class="feel-inner">
<span class="feel-emoji"><?= $f['emoji'] ?></span><span class="feel-label"><?= h($f['label']) ?></span>
</span>
</label>
<?php endforeach; ?>
</div>
</div>
<div class="rv-form-foot">
<span class="rv-hint">每个账号对每部电影只有一条评价,再次提交即为修改。</span>
<button type="submit" class="btn btn-primary btn-sm"><?= $myReview ? '💾 保存修改' : '📝 发表评价' ?></button>
@@ -1110,6 +1133,16 @@ $hasGallery = !empty($gallery);
<?php endif; ?>
</div>
<div class="rv-content"><?= h($rv['content']) ?></div>
<?php if (!empty($rv['feelings'])): ?>
<div class="rv-feelings">
<?php foreach ($rv['feelings'] as $fkey): ?>
<?php $f = feelingInfo($fkey); if (!$f) continue; ?>
<span class="feel-chip is-static" title="观后感受:<?= h($f['label']) ?>">
<span class="feel-emoji"><?= $f['emoji'] ?></span><span class="feel-label"><?= h($f['label']) ?></span>
</span>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if (!empty($rv['updated_at']) && $rv['updated_at'] !== $rv['created_at']): ?>
<div class="rv-updated">已编辑 <?= h(substr((string)$rv['updated_at'], 0, 16)) ?></div>
<?php endif; ?>