Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 2x 1x 1x 2x 4x 1x 1x 1x 1x 1x 2x 4x 1x 1x 1x 1x 2x 2x | <script setup lang="ts">
import { computed, onMounted, ref, watch } from "vue";
import { RouterLink, useRoute, useRouter } from "vue-router";
import {
formatMoney,
parseSpecification,
type ProductDetail,
type ProductSku,
} from "@plain-journal/foundation";
import {
PjActionGroup,
PjButton,
PjPageContainer,
PjResponsiveImage,
PjStatusNotice,
PjSurface,
resolveCatalogImageDelivery,
} from "@plain-journal/ui";
import {
catalogApi,
CatalogAsyncState,
} from "../entities/catalog";
import { useBagStore } from "../entities/guest-bag";
import type { ReviewAccessContext } from "../entities/product-review";
import { useSessionStore } from "../features/customer-session";
import { ProductReviewsSection } from "../features/product-reviews";
const route = useRoute();
const router = useRouter();
const bag = useBagStore();
const session = useSessionStore();
const product = ref<ProductDetail | null>(null);
const selectedSkuId = ref<string | null>(null);
const quantity = ref(1);
const loading = ref(true);
const error = ref<string | null>(null);
const added = ref(false);
const reviewAccess = computed<ReviewAccessContext>(() => ({
authenticated: session.authenticated,
ownerId: session.profile?.id ?? null,
accessToken: session.accessToken,
}));
const activeSkus = computed(() => product.value?.skus.filter(
(sku) => sku.status === "ACTIVE",
) ?? []);
const selectedSku = computed<ProductSku | null>(() => (
activeSkus.value.find((sku) => sku.id === selectedSkuId.value)
?? activeSkus.value[0]
?? null
));
const selectedSpecs = computed(() => (
selectedSku.value ? parseSpecification(selectedSku.value.specJson) : []
));
const media = computed(() => product.value?.media.filter((item) => item.url) ?? []);
const imageIndex = computed(() => {
const raw = Number(route.query.image ?? 1);
return Number.isInteger(raw) && raw > 0 && raw <= media.value.length ? raw - 1 : 0;
});
const currentMedia = computed(() => media.value[imageIndex.value] ?? null);
async function load() {
const productId = String(route.params.productId ?? "");
loading.value = true;
error.value = null;
added.value = false;
try {
product.value = await catalogApi.getProduct(productId);
selectedSkuId.value = product.value.skus.find((sku) => sku.status === "ACTIVE")?.id ?? null;
document.title = `${product.value.title}|素简记`;
} catch (cause) {
product.value = null;
error.value = cause instanceof Error ? cause.message : "暂时无法读取商品详情。";
} finally {
loading.value = false;
}
}
function selectImage(index: number) {
router.replace({
query: {
...route.query,
image: String(index + 1),
},
});
}
function addToBag() {
if (!product.value || !selectedSku.value) {
return;
}
bag.addItem({
productId: product.value.id,
skuId: selectedSku.value.id,
productTitle: product.value.title,
skuName: selectedSku.value.name,
unitPrice: String(selectedSku.value.salePrice),
quantity: quantity.value,
coverUrl: currentMedia.value?.url ?? product.value.media.find((item) => item.url)?.url ?? null,
});
added.value = true;
}
onMounted(load);
watch(() => route.params.productId, load);
</script>
<template>
<PjPageContainer as="section" class="product-detail-page">
<CatalogAsyncState :loading="loading" :error="error" @retry="load">
<template v-if="product">
<nav class="content-path" aria-label="当前位置">
<RouterLink to="/">素简记</RouterLink>
<span aria-hidden="true">/</span>
<RouterLink
:to="{ name: 'products', query: { category: product.category.slug } }"
>
{{ product.category.name }}
</RouterLink>
<span aria-hidden="true">/</span>
<span>{{ product.title }}</span>
</nav>
<div class="product-detail">
<div class="product-media">
<PjSurface tone="media" padding="none" class="product-media__main">
<PjResponsiveImage
v-if="currentMedia?.url"
v-bind="resolveCatalogImageDelivery(currentMedia.url)"
:alt="`${product.title},图片 ${imageIndex + 1}`"
sizes="(max-width: 48rem) calc(100vw - 2rem), 55vw"
loading="eager"
decoding="async"
fetch-priority="high"
/>
<div v-else class="product-media__placeholder">
<span>{{ product.category.name }}</span>
<strong>{{ product.title }}</strong>
</div>
</PjSurface>
<div v-if="media.length > 1" class="product-media__list" aria-label="商品图片">
<button
v-for="(item, index) in media"
:key="item.id"
type="button"
:aria-current="index === imageIndex ? 'true' : undefined"
:aria-label="`查看第 ${index + 1} 张图片`"
@click="selectImage(index)"
>
<PjResponsiveImage
v-bind="resolveCatalogImageDelivery(item.url ?? '')"
alt=""
sizes="72px"
loading="lazy"
decoding="async"
/>
</button>
</div>
</div>
<div class="product-purchase">
<p class="product-context">
{{ product.category.name }} · {{ product.brand.name }}
</p>
<h1>{{ product.title }}</h1>
<p v-if="product.subtitle" class="product-lead">{{ product.subtitle }}</p>
<p class="product-price">
{{ formatMoney(selectedSku?.salePrice) }}
</p>
<fieldset class="sku-selector">
<legend>选择规格</legend>
<label
v-for="sku in product.skus"
:key="sku.id"
:class="{ 'is-unavailable': sku.status !== 'ACTIVE' }"
>
<input
v-model="selectedSkuId"
type="radio"
name="sku"
:value="sku.id"
:disabled="sku.status !== 'ACTIVE'"
/>
<span>
<strong>{{ sku.name }}</strong>
<small>{{ sku.status === "ACTIVE" ? formatMoney(sku.salePrice) : "暂时不可选" }}</small>
</span>
</label>
</fieldset>
<dl v-if="selectedSpecs.length" class="spec-list">
<div v-for="entry in selectedSpecs" :key="entry.label">
<dt>{{ entry.label }}</dt>
<dd>{{ entry.value }}</dd>
</div>
</dl>
<div class="purchase-action">
<p>
已选 {{ selectedSku?.name ?? "请先选择可用规格" }},{{ quantity }} 件
</p>
<PjActionGroup align="between">
<strong>{{ formatMoney(selectedSku?.salePrice) }}</strong>
<PjButton :disabled="!selectedSku" @click="addToBag">
加入购物袋
</PjButton>
</PjActionGroup>
<p class="purchase-note">加入购物袋不锁定库存,结算时重新校验。</p>
</div>
<PjStatusNotice v-if="added" tone="success" title="已放入购物袋">
<p>商品已保存在当前设备的购物袋中。</p>
<template #actions>
<RouterLink class="text-action" to="/bag">查看购物袋</RouterLink>
</template>
</PjStatusNotice>
</div>
</div>
<section class="product-information" aria-labelledby="product-information-title">
<div>
<p class="product-context">商品说明</p>
<h2 id="product-information-title">购买之前,先把事实看清。</h2>
</div>
<div class="product-description">
<p>{{ product.description || "当前商品尚未补充完整说明。" }}</p>
<dl>
<div>
<dt>当前版本</dt>
<dd>版本 {{ product.version }}</dd>
</div>
<div>
<dt>商品状态</dt>
<dd>{{ product.status === "ACTIVE" ? "正在销售" : product.status }}</dd>
</div>
<div>
<dt>价格说明</dt>
<dd>以所选 SKU 与结算时服务端价格为准。</dd>
</div>
</dl>
</div>
</section>
<ProductReviewsSection :access="reviewAccess" :product-id="product.id" />
</template>
</CatalogAsyncState>
</PjPageContainer>
</template>
<style scoped>
.product-detail-page {
padding-block: var(--pj-space-6) var(--pj-space-8);
}
.product-detail {
display: grid;
grid-template-columns: minmax(0, 1.08fr) minmax(22rem, 0.72fr);
gap: clamp(2rem, 6vw, 7rem);
align-items: start;
}
.product-media {
min-width: 0;
}
.product-media__main {
aspect-ratio: 4 / 5;
overflow: hidden;
}
.product-media__main img {
width: 100%;
height: 100%;
object-fit: cover;
}
.product-media__placeholder {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
background:
linear-gradient(
145deg,
var(--pj-surface-media-muted) 0%,
var(--pj-surface-media-highlight) 58%,
var(--pj-surface-media-shadow) 100%
);
color: var(--pj-text-secondary);
}
.product-media__placeholder strong {
margin-top: var(--pj-space-3);
color: var(--pj-text-primary);
font-size: var(--pj-font-size-lg);
}
.product-media__list {
display: flex;
gap: var(--pj-space-3);
margin-top: var(--pj-space-3);
}
.product-media__list button {
width: 4.5rem;
aspect-ratio: 1;
padding: 0;
border: 1px solid transparent;
background: var(--pj-surface-media);
cursor: pointer;
}
.product-media__list button[aria-current="true"] {
border-color: var(--pj-brand-primary-hover);
}
.product-media__list img {
width: 100%;
height: 100%;
object-fit: cover;
}
.product-purchase {
position: sticky;
top: var(--pj-space-5);
min-width: 0;
}
.product-context {
margin: 0 0 var(--pj-space-3);
color: var(--pj-text-secondary);
font-size: var(--pj-font-size-sm);
}
.product-purchase h1 {
margin: 0;
font-size: clamp(2.25rem, 4.4vw, 4.4rem);
font-weight: 520;
letter-spacing: -0.05em;
line-height: 1.02;
}
.product-lead {
max-width: 34rem;
margin-block: var(--pj-space-4);
color: var(--pj-text-secondary);
font-size: var(--pj-font-size-lg);
}
.product-price {
margin: var(--pj-space-6) 0 0;
font-size: clamp(1.5rem, 2.5vw, 2.25rem);
font-variant-numeric: tabular-nums;
}
.sku-selector {
margin: var(--pj-space-6) 0;
padding: 0;
border: 0;
}
.sku-selector legend {
margin-bottom: var(--pj-space-3);
font-weight: 650;
}
.sku-selector label {
display: block;
border-top: 1px solid var(--pj-border-subtle);
}
.sku-selector label:last-child {
border-bottom: 1px solid var(--pj-border-subtle);
}
.sku-selector label > span {
display: flex;
justify-content: space-between;
gap: var(--pj-space-4);
padding: var(--pj-space-4) 0;
cursor: pointer;
}
.sku-selector input {
position: absolute;
opacity: 0;
}
.sku-selector input:checked + span {
color: var(--pj-brand-primary-hover);
box-shadow: inset 0 -2px var(--pj-brand-primary);
}
.sku-selector input:focus-visible + span {
outline: 0.15rem solid var(--pj-focus-ring);
outline-offset: 0.15rem;
}
.sku-selector small {
color: var(--pj-text-secondary);
}
.sku-selector .is-unavailable {
color: var(--pj-text-secondary);
text-decoration: line-through;
}
.spec-list,
.product-description dl {
margin: 0;
}
.spec-list > div,
.product-description dl > div {
display: grid;
grid-template-columns: 8rem minmax(0, 1fr);
gap: var(--pj-space-4);
padding-block: var(--pj-space-3);
border-bottom: 1px solid var(--pj-border-subtle);
}
.spec-list dt,
.product-description dt {
color: var(--pj-text-secondary);
}
.spec-list dd,
.product-description dd {
min-width: 0;
margin: 0;
overflow-wrap: anywhere;
}
.purchase-action {
display: grid;
gap: var(--pj-space-3);
margin-top: var(--pj-space-6);
padding-top: var(--pj-space-4);
border-top: 1px solid var(--pj-border-strong);
}
.purchase-action > p {
margin: 0;
}
.purchase-action > p:first-child,
.purchase-note {
color: var(--pj-text-secondary);
font-size: var(--pj-font-size-sm);
}
.purchase-action strong {
font-size: var(--pj-font-size-lg);
}
.product-purchase :deep(.pj-status-notice) {
margin-top: var(--pj-space-5);
}
.product-information {
display: grid;
grid-template-columns: minmax(16rem, 0.68fr) minmax(0, 1.32fr);
gap: clamp(2rem, 6vw, 7rem);
margin-top: var(--pj-space-8);
padding-block: var(--pj-space-8);
border-top: 1px solid var(--pj-border-subtle);
}
.product-information h2 {
max-width: 15ch;
margin: 0;
font-size: clamp(1.8rem, 3vw, 3rem);
font-weight: 520;
line-height: var(--pj-line-height-tight);
}
.product-description > p {
margin-top: 0;
white-space: pre-line;
}
@media (max-width: 64rem) {
.product-detail {
grid-template-columns: minmax(0, 1fr) minmax(20rem, 0.9fr);
gap: var(--pj-space-6);
}
}
@media (max-width: 48rem) {
.product-detail,
.product-information {
grid-template-columns: 1fr;
}
.product-purchase {
position: static;
}
.product-information {
gap: var(--pj-space-6);
}
}
@media (max-width: 32rem) {
.product-detail-page {
padding-top: var(--pj-space-5);
}
.product-purchase :deep(.pj-action-group) {
align-items: stretch;
}
.product-purchase :deep(.pj-button) {
width: 100%;
}
.spec-list > div,
.product-description dl > div {
grid-template-columns: 1fr;
gap: var(--pj-space-1);
}
}
</style>
|