feat:實作評價系統 — 匿名評價、有幫助投票、手動完成預約

後端:
- 新增 reviews / review_edits / review_votes migration(含索引)
- Review / ReviewEdit / ReviewVote Model
- ReviewController:評價 CRUD、資格驗證(completed booking)、rating 即時重算
- toggleHelpful:Member 限定、GREATEST 原子防負、DB transaction 同步
- AdminReviewController:全量列表、刪除(含重算)
- AdminBookingController:全量列表、手動標記 completed
- ProviderBookingController 新增 complete 方法(教練手動完成預約)
- DevelopmentSeeder:快速重建測試資料(admin/coach/member + offers + bookings)
- EnsureAdmin middleware 正式納入 bootstrap/app.php
- Nginx server_name 加入 cfdive.local

前端:
- 課程詳情頁加入評價區塊(星等分布、排序切換、撰寫/修改/刪除、有幫助 Toggle)
- Coach Portal 新增「課程評價」頁(只讀,依課程分組)
- Coach 預約管理加入「完成」按鈕
- Admin 新增「預約管理」頁(標記完成)、「評價管理」頁(刪除)
- Admin / Coach Navbar 新增對應連結

OpenSpec:
- review-system change 歸檔至 archive/2026-05-12-review-system
- 新增 specs/review-lifecycle 與 specs/review-voting 主規格
- review-voting spec 補充 Member 限定與 GREATEST 原子更新說明

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-12 02:46:54 +08:00
parent 88c0c09ccb
commit 8ef293332a
35 changed files with 1781 additions and 8 deletions
+161 -1
View File
@@ -4,6 +4,7 @@ import { useRoute, useRouter } from 'vue-router'
import api from '../api/axios'
import { getSchedulesByOffer } from '../api/scheduleApi'
import { createBooking } from '../api/bookingApi'
import { getReviews, createReview, updateReview, deleteReview, toggleHelpful } from '../api/reviewApi'
import { useAuthStore } from '../stores/auth'
const route = useRoute()
@@ -18,12 +19,24 @@ const selected = ref(null)
const participants = ref(1)
const booking = ref({ loading: false, success: false, error: '' })
// 評價相關
const reviewSort = ref('helpful')
const reviewSummary = ref(null)
const reviews = ref([])
const myReview = ref(null)
const reviewForm = ref({ show: false, rating: 5, comment: '', saving: false, error: '' })
const editTarget = ref(null)
onMounted(async () => {
try {
const res = await api.get(`/diving-offers/${route.params.id}`)
offer.value = res.data.data
const sRes = await getSchedulesByOffer(route.params.id)
const [sRes, rRes] = await Promise.all([
getSchedulesByOffer(route.params.id),
getReviews(route.params.id, reviewSort.value),
])
schedules.value = sRes.data.data
applyReviewData(rRes.data.data)
} catch (e) {
notFound.value = true
} finally {
@@ -31,6 +44,57 @@ onMounted(async () => {
}
})
function applyReviewData(data) {
reviewSummary.value = data.summary
reviews.value = data.reviews
myReview.value = data.reviews.find(r => r.is_mine) || null
}
async function switchSort(sort) {
reviewSort.value = sort
const res = await getReviews(route.params.id, sort)
applyReviewData(res.data.data)
}
async function submitReview() {
reviewForm.value.saving = true
reviewForm.value.error = ''
try {
if (editTarget.value) {
await updateReview(editTarget.value.id, { rating: reviewForm.value.rating, comment: reviewForm.value.comment })
} else {
await createReview({ diving_offer_id: offer.value.id, rating: reviewForm.value.rating, comment: reviewForm.value.comment })
}
reviewForm.value.show = false
editTarget.value = null
const res = await getReviews(route.params.id, reviewSort.value)
applyReviewData(res.data.data)
} catch (e) {
reviewForm.value.error = e.response?.data?.message || '送出失敗'
} finally {
reviewForm.value.saving = false
}
}
function openEdit(review) {
editTarget.value = review
reviewForm.value = { show: true, rating: review.rating, comment: review.comment, saving: false, error: '' }
}
async function doDeleteReview(review) {
if (!confirm('確定要刪除此評價?')) return
await deleteReview(review.id)
const res = await getReviews(route.params.id, reviewSort.value)
applyReviewData(res.data.data)
}
async function doToggleHelpful(review) {
if (!auth.isLoggedIn) return
const res = await toggleHelpful(review.id)
review.helpful_count = res.data.data.helpful_count
review.has_voted = res.data.data.has_voted
}
async function submitBooking() {
if (!selected.value) return
booking.value = { loading: true, success: false, error: '' }
@@ -154,6 +218,102 @@ async function submitBooking() {
</button>
</div>
</div>
<!-- 評價區塊 -->
<div class="bg-white rounded-2xl shadow p-6 mb-6">
<!-- 標題 + 排序 -->
<div class="flex items-center justify-between mb-5 flex-wrap gap-3">
<div>
<h2 class="text-lg font-semibold text-gray-800">課程評價</h2>
<p v-if="reviewSummary" class="text-sm text-gray-500 mt-0.5">
{{ reviewSummary.average }} · {{ reviewSummary.total }} 則評價
</p>
</div>
<div class="flex gap-2 text-sm">
<button v-for="s in [['helpful','最多幫助'],['rating','最高分'],['newest','最新']]" :key="s[0]"
@click="switchSort(s[0])"
:class="reviewSort === s[0]
? 'bg-ocean-700 text-white px-3 py-1 rounded-full'
: 'bg-gray-100 text-gray-600 hover:bg-gray-200 px-3 py-1 rounded-full transition'">
{{ s[1] }}
</button>
</div>
</div>
<!-- 星等分布條 -->
<div v-if="reviewSummary?.total > 0" class="space-y-1 mb-6">
<div v-for="star in [5,4,3,2,1]" :key="star" class="flex items-center gap-2 text-sm">
<span class="w-8 text-right text-gray-500">{{ star }}</span>
<div class="flex-1 bg-gray-100 rounded-full h-2">
<div class="bg-yellow-400 h-2 rounded-full transition-all"
:style="`width:${reviewSummary.total > 0 ? (reviewSummary.distribution[star] / reviewSummary.total * 100) : 0}%`">
</div>
</div>
<span class="w-6 text-gray-400 text-xs">{{ reviewSummary.distribution[star] }}</span>
</div>
</div>
<!-- 我的評價 / 新增表單 -->
<div v-if="auth.isLoggedIn" class="mb-5">
<div v-if="!myReview && !reviewForm.show">
<button @click="reviewForm = { show: true, rating: 5, comment: '', saving: false, error: '' }; editTarget = null"
class="text-sm text-ocean-600 hover:underline">
+ 撰寫評價
</button>
</div>
<div v-if="reviewForm.show" class="border border-ocean-200 rounded-xl p-4 bg-ocean-50">
<p class="text-sm font-medium text-gray-700 mb-3">{{ editTarget ? '修改評價' : '撰寫評價' }}</p>
<!-- 星等選擇 -->
<div class="flex gap-1 mb-3">
<button v-for="n in [1,2,3,4,5]" :key="n" @click="reviewForm.rating = n"
:class="n <= reviewForm.rating ? 'text-yellow-400' : 'text-gray-300'"
class="text-2xl transition"></button>
</div>
<textarea v-model="reviewForm.comment" rows="3" placeholder="分享你的課程體驗..."
class="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm resize-none focus:outline-none focus:ring-2 focus:ring-ocean-400" />
<p v-if="reviewForm.error" class="text-red-500 text-xs mt-1">{{ reviewForm.error }}</p>
<div class="flex gap-2 mt-3">
<button @click="submitReview" :disabled="reviewForm.saving"
class="bg-ocean-700 text-white text-sm px-4 py-1.5 rounded-full hover:bg-ocean-600 transition disabled:opacity-60">
{{ reviewForm.saving ? '送出中...' : '送出' }}
</button>
<button @click="reviewForm.show = false; editTarget = null"
class="text-sm text-gray-500 hover:text-gray-700 px-4 py-1.5">取消</button>
</div>
</div>
</div>
<!-- 評價列表 -->
<div v-if="reviews.length === 0" class="text-gray-400 text-sm py-4 text-center">尚無評價</div>
<div v-else class="space-y-4">
<div v-for="r in reviews" :key="r.id"
class="pb-4 border-b border-gray-100 last:border-0">
<div class="flex items-start justify-between">
<div>
<div class="flex items-center gap-2">
<span class="text-yellow-400 text-sm">{{ '★'.repeat(r.rating) }}{{ '☆'.repeat(5 - r.rating) }}</span>
<span class="text-xs text-gray-400">{{ r.reviewer_name }}</span>
<span v-if="r.is_edited" class="text-xs text-gray-400">已修改</span>
</div>
<p class="text-sm text-gray-700 mt-1 leading-relaxed">{{ r.comment }}</p>
<p class="text-xs text-gray-400 mt-1">{{ new Date(r.created_at).toLocaleDateString('zh-TW') }}</p>
</div>
<!-- 本人操作 -->
<div v-if="r.is_mine" class="flex gap-2 text-xs ml-3 shrink-0">
<button @click="openEdit(r)" class="text-ocean-600 hover:underline">修改</button>
<button @click="doDeleteReview(r)" class="text-red-400 hover:underline">刪除</button>
</div>
</div>
<!-- 有幫助 -->
<button @click="doToggleHelpful(r)"
:class="r.has_voted ? 'text-ocean-600' : 'text-gray-400 hover:text-gray-600'"
class="mt-2 text-xs flex items-center gap-1 transition"
:disabled="!auth.isLoggedIn">
👍 有幫助 {{ r.helpful_count > 0 ? `(${r.helpful_count})` : '' }}
</button>
</div>
</div>
</div>
</template>
</main>