Files
JayuCloud/YumiNail-Frontend/src/views/ProductDetailView.vue
T

466 lines
23 KiB
Vue
Raw Normal View History

<template>
<div class="detail-page">
<header class="detail-header">
<a-button type="text" @click="router.back()"><LeftOutlined /></a-button>
<h1>{{ productTitle }}</h1>
<a-button type="text" @click="handleFavoriteClick">
<component :is="isFavorite ? StarFilled : StarOutlined" :class="{ active: isFavorite }" />
</a-button>
</header>
<main class="detail-content">
<a-result v-if="loadError" status="warning" title="暂时无法查看作品详情" :sub-title="loadError">
<template #extra>
<a-button type="primary" @click="router.back()">返回上一页</a-button>
</template>
</a-result>
<template v-else>
<section class="media-section">
<a-image
class="detail-image-preview"
:src="productImages[0] || DEFAULT_PRODUCT_IMAGE"
:alt="productTitle"
:preview="{ src: productImages[0] || DEFAULT_PRODUCT_IMAGE }"
/>
</section>
<section class="info-section">
<div class="title-row">
<div>
<h2>{{ productTitle }}</h2>
<p class="price">¥{{ Number(product.price || 0).toFixed(2) }}</p>
</div>
<a-tag color="blue">{{ product.approximateDuration || '以门店确认为准' }}</a-tag>
</div>
<div class="metrics">
<button type="button" @click="openShopDetail" :disabled="!shopInfo?.id">
<ShopOutlined />
<span>门店</span>
<strong>{{ shopInfo?.shopName || '暂未绑定门店' }}</strong>
</button>
<div>
<TeamOutlined />
<span>可约美甲师</span>
<strong>{{ artistCountText }}</strong>
</div>
<div>
<CalendarOutlined />
<span>可约时间</span>
<strong>{{ earliestBookableText }}</strong>
</div>
</div>
<div v-if="shopInfo" class="shop-panel" @click="openShopDetail">
<strong>{{ shopInfo.shopName }}</strong>
<span>{{ shopInfo.address || '门店地址待完善' }}</span>
<span>{{ shopInfo.businessHours || '营业时间待完善' }}</span>
</div>
<div class="actions-row">
<button type="button" :class="{ active: isLiked }" @click="handleLikeClick">
<component :is="isLiked ? HeartFilled : HeartOutlined" />
{{ product.likeCount || 0 }} 点赞
</button>
<button type="button" :class="{ active: isFavorite }" @click="handleFavoriteClick">
<component :is="isFavorite ? StarFilled : StarOutlined" />
{{ product.collectCount || 0 }} 收藏
</button>
<button type="button" @click="copyShareLink">分享链接</button>
</div>
<div class="tags">
<a-tag v-for="tag in product.tagInfos || []" :key="tag.id">{{ tag.tagName }}</a-tag>
<a-tag v-if="!product.tagInfos?.length">暂无标签</a-tag>
</div>
<div class="description">
<h3>款式说明</h3>
<p>{{ product.description || '店家暂未补充作品说明。' }}</p>
</div>
<div v-if="product.targetAudience || product.precautions" class="guidance-grid">
<section v-if="product.targetAudience" class="guidance-panel">
<h3>适合人群</h3>
<p>{{ product.targetAudience }}</p>
</section>
<section v-if="product.precautions" class="guidance-panel">
<h3>注意事项</h3>
<p>{{ product.precautions }}</p>
</section>
</div>
</section>
<section class="comment-section">
<div class="section-head">
<h3>作品评论</h3>
<span>{{ comments.length }} 条</span>
</div>
<div class="comment-editor">
<a-textarea v-model:value="commentText" :maxlength="300" :rows="3" :placeholder="replyTarget ? `回复 ${replyTarget.userNickname || '用户'}` : '说说你的看法'" />
<div class="comment-tools">
<a-button v-if="replyTarget" type="text" @click="replyTarget = null">取消回复</a-button>
<a-button type="primary" :loading="submittingComment" @click="submitComment">发布</a-button>
</div>
</div>
<a-empty v-if="!comments.length" description="还没有评论,来写第一条吧" />
<div v-for="comment in comments" :key="comment.id" class="comment-item">
<div class="comment-main">
<strong>{{ comment.userNickname || '用户' }}</strong>
<span>{{ formatTime(comment.createTime) }}</span>
</div>
<p>{{ comment.commentContent }}</p>
<button type="button" @click="replyTarget = comment">回复</button>
<div v-if="comment.replies?.length" class="reply-list">
<div v-for="reply in comment.replies" :key="reply.id" class="reply-item">
<strong>{{ reply.userNickname || '用户' }}</strong>
<span>{{ reply.commentContent }}</span>
</div>
</div>
</div>
</section>
</template>
</main>
<footer v-if="!loadError" class="detail-footer">
<a-button size="large" @click="handleAddToCart"><ShoppingCartOutlined />加入购物车</a-button>
<a-button type="primary" size="large" @click="handleBuyNow"><ThunderboltOutlined />立即预约</a-button>
</footer>
</div>
</template>
<script setup lang="ts">
/**
* ProductDetailView page script.
* Call guide: the template binds the reactive state below, and the methods are triggered by button clicks, lifecycle hooks, or router events.
*/
import { computed, inject, onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import {
CalendarOutlined,
HeartFilled,
HeartOutlined,
LeftOutlined,
ShoppingCartOutlined,
ShopOutlined,
StarFilled,
StarOutlined,
TeamOutlined,
ThunderboltOutlined
} from '@ant-design/icons-vue'
import { message } from 'ant-design-vue'
import { cartApi } from '@/api/cart'
2026-05-23 18:44:57 +08:00
/**
* Purpose: CommentVO supports ProductDetailView.vue by holding or executing the comment vo page or API role.
* Usage: import, call, read, or update this type from the owning page/module while preserving the surrounding loading and permission flow.
*/
import { commentApi, type CommentVO } from '@/api/comment'
import { fileApi } from '@/api/file'
2026-05-23 18:44:57 +08:00
/**
* Purpose: ProductVO supports ProductDetailView.vue by holding or executing the product vo page or API role.
* Usage: import, call, read, or update this type from the owning page/module while preserving the surrounding loading and permission flow.
*/
import { productApi, type ProductVO } from '@/api/product'
import { productLikeApi } from '@/api/productLike'
2026-05-23 18:44:57 +08:00
/**
* Purpose: ShopVO supports ProductDetailView.vue by holding or executing the shop vo page or API role.
* Usage: import, call, read, or update this type from the owning page/module while preserving the surrounding loading and permission flow.
*/
import { shopApi, type ShopVO } from '@/api/shop'
import { wishlistApi } from '@/api/wishlist'
import { DEFAULT_PRODUCT_IMAGE } from '@/constants'
2026-03-27 14:55:41 +08:00
import type { IdType } from '@/utils/id'
import { toOptionalIdString } from '@/utils/id'
import { runWithGlobalLoading } from '@/utils/action-loading'
2026-05-23 18:44:57 +08:00
/**
* Purpose: route supports ProductDetailView.vue by holding or executing the route page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const route = useRoute()
2026-05-23 18:44:57 +08:00
/**
* Purpose: router supports ProductDetailView.vue by holding or executing the router page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const router = useRouter()
2026-05-23 18:44:57 +08:00
/**
* Purpose: updateCartCount supports ProductDetailView.vue by holding or executing the update cart count page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
2026-02-28 17:09:45 +08:00
const updateCartCount = inject<() => Promise<void>>('updateCartCount', async () => {})
2026-05-23 18:44:57 +08:00
/**
* Purpose: product supports ProductDetailView.vue by holding or executing the product page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const product = ref<ProductVO>({} as ProductVO)
2026-05-23 18:44:57 +08:00
/**
* Purpose: shopInfo supports ProductDetailView.vue by holding or executing the shop info page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const shopInfo = ref<ShopVO | null>(null)
2026-05-23 18:44:57 +08:00
/**
* Purpose: productImages supports ProductDetailView.vue by holding or executing the product images page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
2026-02-26 11:16:24 +08:00
const productImages = ref<string[]>([])
2026-05-23 18:44:57 +08:00
/**
* Purpose: imageIdCache supports ProductDetailView.vue by holding or executing the image id cache page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const imageIdCache = ref<Record<string, string>>({})
2026-05-23 18:44:57 +08:00
/**
* Purpose: comments supports ProductDetailView.vue by holding or executing the comments page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const comments = ref<CommentVO[]>([])
/** commentText is a reactive state variable used by the template and event handlers. */
const commentText = ref('')
2026-05-23 18:44:57 +08:00
/**
* Purpose: replyTarget supports ProductDetailView.vue by holding or executing the reply target page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const replyTarget = ref<CommentVO | null>(null)
/** submittingComment is a reactive state variable used by the template and event handlers. */
const submittingComment = ref(false)
/** isFavorite is a reactive state variable used by the template and event handlers. */
const isFavorite = ref(false)
/** isLiked is a reactive state variable used by the template and event handlers. */
const isLiked = ref(false)
/** loadError is a reactive state variable used by the template and event handlers. */
const loadError = ref('')
/** isLoggedIn is a reactive state variable used by the template and event handlers. */
const isLoggedIn = computed(() => Boolean(localStorage.getItem('accessToken') || sessionStorage.getItem('accessToken')))
/** productTitle is a reactive state variable used by the template and event handlers. */
const productTitle = computed(() => product.value.title || product.value.name || '作品详情')
/** artistCount is a reactive state variable used by the template and event handlers. */
const artistCount = computed(() => {
if (typeof product.value.availableBookableArtistCount === 'number') return product.value.availableBookableArtistCount
return String(product.value.bookableArtists || '').split(',').map(id => id.trim()).filter(Boolean).length
})
/** artistCountText is a reactive state variable used by the template and event handlers. */
const artistCountText = computed(() => artistCount.value > 0 ? `${artistCount.value} 位` : (product.value.bookableArtistRestricted ? '暂无可约' : '门店安排'))
/** earliestBookableText is a reactive state variable used by the template and event handlers. */
const earliestBookableText = computed(() => shopInfo.value?.businessHours || '以门店安排为准')
2026-05-23 18:44:57 +08:00
/**
* Purpose: copyShareLink supports ProductDetailView.vue by holding or executing the copy share link page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const copyShareLink = async () => {
const url = `${window.location.origin}/product/${route.params.id}`
try {
await navigator.clipboard.writeText(url)
message.success('分享链接已复制,未登录用户也可以查看详情')
} catch {
message.warning(url)
}
}
/** requireLogin is a local helper used by this page script. */
const requireLogin = (actionText: string) => {
if (isLoggedIn.value) return true
message.info(`${actionText}前请先登录`)
router.push('/')
return false
2026-03-05 15:15:59 +08:00
}
/** loadProductImages is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
2026-02-26 11:16:24 +08:00
const loadProductImages = async () => {
const fileIds = product.value.imageFileIds?.split(',').map(id => id.trim()).filter(Boolean) || []
if (!fileIds.length) {
productImages.value = [DEFAULT_PRODUCT_IMAGE]
return
}
const urls: string[] = []
for (const id of fileIds) {
try {
if (!imageIdCache.value[id]) imageIdCache.value[id] = await fileApi.getSignedUrl(id) as unknown as string
if (imageIdCache.value[id]) urls.push(imageIdCache.value[id])
} catch {}
}
productImages.value = urls.length ? urls : [DEFAULT_PRODUCT_IMAGE]
}
/** loadShopInfo is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
2026-03-27 14:55:41 +08:00
const loadShopInfo = async (shopId?: IdType | null) => {
shopInfo.value = shopId ? await shopApi.getShopById(shopId).catch(() => null) : null
2026-02-26 11:16:24 +08:00
}
/** loadComments is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
const loadComments = async () => {
if (!product.value.id) return
comments.value = await commentApi.listByProduct(product.value.id).catch(() => [])
}
/** checkInteractionStatus is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
const checkInteractionStatus = async () => {
if (!isLoggedIn.value || !product.value.id) {
isFavorite.value = false
isLiked.value = false
return
}
const [favorite, liked] = await Promise.all([
wishlistApi.checkWishlist(product.value.id).catch(() => false),
productLikeApi.checkLikeStatus(product.value.id).catch(() => false)
])
isFavorite.value = Boolean(favorite)
isLiked.value = Boolean(liked)
}
/** fetchProductDetail is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
const fetchProductDetail = async () => {
loadError.value = ''
try {
2026-03-27 14:55:41 +08:00
const productId = toOptionalIdString(route.params.id)
if (!productId) throw new Error('作品不存在')
const response = await productApi.getProductById(productId)
if (!response?.id) throw new Error('作品不存在或已下架')
product.value = response
await Promise.all([loadProductImages(), loadShopInfo(response.shopId), checkInteractionStatus(), loadComments()])
} catch (error: any) {
loadError.value = error?.message || '网络异常,请稍后再试'
}
2026-02-26 11:16:24 +08:00
}
/** openShopDetail is a local helper used by this page script. */
const openShopDetail = () => {
if (shopInfo.value?.id) router.push(`/shop/${shopInfo.value.id}`)
}
/** handleFavoriteClick is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
const handleFavoriteClick = async () => {
if (!product.value.id || !requireLogin('收藏作品')) return
await runWithGlobalLoading(async () => {
if (isFavorite.value) {
await wishlistApi.removeFromWishlist(product.value.id)
product.value.collectCount = Math.max(0, Number(product.value.collectCount || 0) - 1)
message.success('已取消收藏')
} else {
await wishlistApi.addToWishlist(product.value.id)
product.value.collectCount = Number(product.value.collectCount || 0) + 1
message.success('已加入收藏')
}
isFavorite.value = !isFavorite.value
}).catch((error: any) => message.error(error?.message || '收藏操作失败'))
}
/** handleLikeClick is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
2026-03-05 15:15:59 +08:00
const handleLikeClick = async () => {
if (!product.value.id || !requireLogin('点赞作品')) return
await runWithGlobalLoading(async () => {
if (isLiked.value) {
await productLikeApi.unlikeProduct(product.value.id)
product.value.likeCount = Math.max(0, Number(product.value.likeCount || 0) - 1)
message.success('已取消点赞')
} else {
await productLikeApi.likeProduct(product.value.id)
product.value.likeCount = Number(product.value.likeCount || 0) + 1
message.success('已点赞')
}
2026-03-05 15:15:59 +08:00
isLiked.value = !isLiked.value
}).catch((error: any) => message.error(error?.message || '点赞操作失败'))
2026-03-05 15:15:59 +08:00
}
/** handleAddToCart is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
const handleAddToCart = async () => {
if (!product.value.id || !requireLogin('加入购物车')) return
await runWithGlobalLoading(() => cartApi.addToCart({ productId: product.value.id, quantity: 1 }))
await updateCartCount()
message.success('已加入购物车')
}
/** handleBuyNow is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
const handleBuyNow = async () => {
if (!product.value.id || !requireLogin('预约下单')) return
await runWithGlobalLoading(() => cartApi.addToCart({ productId: product.value.id, quantity: 1 }))
await updateCartCount()
message.success('已加入确认订单')
router.push({ path: '/cart', query: { mode: 'buyNow', productId: String(product.value.id) } })
}
/** submitComment is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
const submitComment = async () => {
if (!product.value.id || !requireLogin('发表评论')) return
const content = commentText.value.trim()
if (!content) return message.warning('请输入评论内容')
submittingComment.value = true
try {
await commentApi.create({
productId: product.value.id,
shopId: product.value.shopId,
parentCommentId: replyTarget.value?.id,
commentContent: content
})
commentText.value = ''
replyTarget.value = null
await loadComments()
message.success('评论已发布')
} catch (error: any) {
message.error(error?.message || '评论发布失败')
} finally {
submittingComment.value = false
}
}
2026-02-24 09:59:22 +08:00
/** formatTime is a local helper used by this page script. */
const formatTime = (value?: string) => value ? value.replace('T', ' ').slice(0, 16) : ''
2026-02-24 09:59:22 +08:00
onMounted(() => {
void fetchProductDetail()
2026-02-24 09:59:22 +08:00
})
</script>
<style scoped>
.detail-page { min-height: 100vh; background: linear-gradient(rgba(255, 255, 255, 0.68), rgba(255, 255, 255, 0.92)), var(--theme-background-image), var(--theme-page-bg); background-size: cover, cover, auto; background-position: center top; }
.detail-header { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; background: rgba(255,255,255,.96); border-bottom: 1px solid var(--theme-border); backdrop-filter: blur(10px); }
.detail-header h1 { flex: 1; margin: 0; font-size: 17px; text-align: center; color: var(--text-primary); }
.detail-header .active { color: var(--theme-accent); }
.detail-content { max-width: 1040px; margin: 0 auto; padding: 18px 16px 118px; }
.media-section { overflow: hidden; border-radius: 8px; background: var(--theme-card-bg); }
.media-section :deep(.ant-image),
.media-section :deep(.ant-image-img),
.media-section img { display: block; width: 100%; aspect-ratio: 16 / 10; object-fit: cover; cursor: zoom-in; }
.info-section, .comment-section { margin-top: 14px; padding: 22px; border-radius: 8px; background: var(--theme-card-bg); border: 1px solid var(--theme-border); }
.title-row { display: flex; justify-content: space-between; gap: 16px; }
.title-row h2 { margin: 0 0 8px; font-size: 24px; color: var(--text-primary); }
.price { margin: 0; font-size: 26px; font-weight: 700; color: var(--theme-accent); }
.metrics { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-top: 18px; }
.metrics > div:nth-of-type(2) { display: none; }
.metrics > div, .metrics > button { display: grid; gap: 4px; padding: 14px; border: 1px solid var(--theme-border); border-radius: 8px; background: var(--theme-primary-soft); text-align: left; }
.metrics svg { color: var(--theme-primary); }
.metrics span { font-size: 12px; color: var(--text-secondary); }
.metrics strong { color: var(--text-primary); }
.shop-panel { display: none; }
.shop-panel strong { color: var(--text-primary); }
.actions-row { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 18px; }
.actions-row button { display: inline-flex; align-items: center; gap: 8px; padding: 9px 14px; border: 1px solid var(--theme-border); border-radius: 999px; background: var(--theme-card-bg); color: var(--text-secondary); }
.actions-row button.active { color: var(--theme-accent); border-color: var(--theme-border); background: var(--theme-primary-soft); }
.tags { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 18px; }
.description { margin-top: 22px; }
.description h3, .guidance-panel h3, .section-head h3 { margin: 0; font-size: 18px; color: var(--text-primary); }
.description p, .guidance-panel p { margin: 10px 0 0; color: var(--text-secondary); line-height: 1.8; white-space: pre-wrap; }
.guidance-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-top: 18px; }
.guidance-panel { padding: 14px; border-radius: 8px; border: 1px solid var(--theme-border); background: var(--theme-primary-soft); }
.section-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.section-head span { color: var(--text-secondary); }
.comment-editor { padding: 14px; border: 1px solid var(--theme-border); border-radius: 8px; background: var(--theme-primary-soft); }
.comment-tools { display: flex; justify-content: flex-end; gap: 8px; margin-top: 10px; }
.comment-item { padding: 16px 0; border-bottom: 1px solid var(--theme-border); }
.comment-item:last-child { border-bottom: 0; }
.comment-main { display: flex; justify-content: space-between; gap: 12px; color: var(--text-secondary); }
.comment-main strong { color: var(--text-primary); }
.comment-item p { margin: 8px 0; color: var(--text-primary); line-height: 1.7; }
.comment-item button { padding: 0; border: 0; background: transparent; color: var(--theme-primary); cursor: pointer; }
.reply-list { margin-top: 10px; padding: 10px 12px; border-radius: 8px; background: var(--theme-primary-soft); }
.reply-item { display: flex; gap: 8px; padding: 6px 0; color: var(--text-secondary); }
.reply-item strong { color: var(--text-primary); }
.detail-footer { position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; display: flex; gap: 12px; padding: 14px 16px calc(14px + env(safe-area-inset-bottom)); background: rgba(255,255,255,.96); border-top: 1px solid var(--theme-border); backdrop-filter: blur(10px); }
.detail-footer :deep(.ant-btn) { flex: 1; height: 46px; border-radius: 999px; font-weight: 700; }
@media (max-width: 720px) { .metrics, .guidance-grid { grid-template-columns: 1fr; } .title-row { flex-direction: column; } }
2026-03-05 15:15:59 +08:00
</style>