view.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. <template>
  2. <view class="product-view-page client-theme client-subpage">
  3. <view v-if="loading" class="loading">
  4. <ClientSkeleton variant="detail" :rows="2" />
  5. </view>
  6. <view v-else-if="!product" class="empty">商品不存在</view>
  7. <view v-else class="product-view-inner">
  8. <view class="client-subpage-hero product-view-hero">
  9. <text class="client-subpage-hero__title">商品详情</text>
  10. <text class="client-subpage-hero__sub">{{ product.name }}</text>
  11. </view>
  12. <view class="detail-fixed-top">
  13. <swiper
  14. v-if="bannerList.length > 0"
  15. class="banner-swiper"
  16. :indicator-dots="bannerList.length > 1"
  17. :autoplay="true"
  18. :interval="3000"
  19. :circular="bannerList.length > 1"
  20. >
  21. <swiper-item v-for="(banner, index) in bannerList" :key="index">
  22. <ClientImageLoader
  23. v-if="banner.mediaType === 'image'"
  24. :src="banner.fileUrl"
  25. mode="aspectFill"
  26. class="banner-image"
  27. />
  28. <video v-else :src="banner.fileUrl" controls class="banner-video" />
  29. </swiper-item>
  30. </swiper>
  31. <view v-else class="banner-empty">
  32. <text class="banner-empty-text">暂无轮播图</text>
  33. </view>
  34. <view class="product-info">
  35. <view class="price-row">
  36. <text class="price">¥{{ formatPrice(product.price) }}</text>
  37. <text class="unit">/{{ formatMeasureText(product.measureValue, product.unitName) }}</text>
  38. </view>
  39. <text class="name">{{ product.name }}</text>
  40. <text class="spec">{{ formatMeasureText(product.measureValue, product.unitName) }}</text>
  41. </view>
  42. </view>
  43. <scroll-view scroll-y class="detail-scroll-area" :show-scrollbar="true">
  44. <view class="detail-scroll-inner">
  45. <view class="detail-section" v-if="detailContent">
  46. <view class="section-title">商品详情</view>
  47. <view class="rich-content">
  48. <rich-text :nodes="detailContent"></rich-text>
  49. </view>
  50. </view>
  51. <view class="params-section" v-if="parameterValues.length > 0">
  52. <view class="section-title">商品参数</view>
  53. <view class="params-list">
  54. <view v-for="param in parameterValues" :key="param.parameterId" class="param-item">
  55. <text class="param-name">{{ param.parameterName }}</text>
  56. <text class="param-value">{{ param.paramValue }}</text>
  57. </view>
  58. </view>
  59. </view>
  60. <view class="detail-section" v-if="detailMediaList.length > 0">
  61. <view class="section-title">详情图片/视频</view>
  62. <view class="detail-media-list">
  63. <view v-for="(m, idx) in detailMediaList" :key="m.id || `dm-${idx}`" class="detail-media-item">
  64. <ClientImageLoader
  65. v-if="m.contentType === 'image'"
  66. :src="m.fileUrl"
  67. mode="widthFix"
  68. class="detail-media-img"
  69. />
  70. <video v-else :src="m.fileUrl" :poster="ossImg(m.thumbnailUrl || '')" controls class="detail-media-video" />
  71. <text v-if="m.description" class="detail-media-desc">{{ m.description }}</text>
  72. </view>
  73. </view>
  74. </view>
  75. </view>
  76. </scroll-view>
  77. </view>
  78. <view v-if="product" class="bottom-bar">
  79. <view class="action-left" @click="goToCart">
  80. <text class="cart-icon">🛒</text>
  81. <view v-if="cartCount > 0" class="badge">{{ cartCount > 99 ? '99+' : cartCount }}</view>
  82. </view>
  83. <view class="action-btns">
  84. <view class="add-cart-btn" @click="openQuantityPopup('cart')">加入购物车</view>
  85. <view class="buy-btn" @click="openQuantityPopup('buy')">立即购买</view>
  86. </view>
  87. </view>
  88. <ClientTabBar :active="tabBarActive" />
  89. <van-popup :show="showQuantityPopup" position="bottom" round @update:show="showQuantityPopup = $event">
  90. <view class="quantity-popup">
  91. <view class="product-row">
  92. <ClientImageLoader :src="product?.imageUrl || IMG_DEFAULT_PRODUCT" class="popup-image" mode="aspectFill" />
  93. <view class="popup-info">
  94. <text class="popup-name">{{ product?.name || '-' }}</text>
  95. <text class="popup-price">¥{{ formatPrice(product?.price) }}</text>
  96. </view>
  97. </view>
  98. <view class="quantity-row">
  99. <text class="quantity-label">购买数量</text>
  100. <view class="quantity-control">
  101. <view class="qty-btn" @click="decreaseQty">-</view>
  102. <input v-model="quantity" type="number" class="qty-input" />
  103. <view class="qty-btn" @click="increaseQty">+</view>
  104. </view>
  105. </view>
  106. <view class="confirm-btn" @click="confirmAction">{{ actionType === 'cart' ? '加入购物车' : '立即购买' }}</view>
  107. </view>
  108. </van-popup>
  109. </view>
  110. </template>
  111. <script setup>
  112. import { ref } from 'vue'
  113. import { onLoad } from '@dcloudio/uni-app'
  114. import { useUserStore } from '@/stores/user'
  115. import { request, parseResponseJson } from '@/utils/request'
  116. import { useCartStore } from '@/stores/cart'
  117. import { formatYuan } from '@/utils/format'
  118. import ClientTabBar from '@/components/client/client-tab-bar.vue'
  119. import ClientSkeleton from '@/components/client/client-skeleton.vue'
  120. import ClientImageLoader from '@/components/client/client-image-loader.vue'
  121. import { IMG_DEFAULT_PRODUCT } from '@/utils/staticAsset'
  122. import { withOssVersion } from '@/utils/ossCache'
  123. const userStore = useUserStore()
  124. const cartStore = useCartStore()
  125. const loading = ref(false)
  126. const product = ref(null)
  127. const bannerList = ref([])
  128. const parameterValues = ref([])
  129. const detailContent = ref('')
  130. const detailMediaList = ref([])
  131. const showQuantityPopup = ref(false)
  132. const quantity = ref(1)
  133. const actionType = ref('cart')
  134. const cartCount = ref(0)
  135. const tabBarActive = ref('home')
  136. const formatPrice = (fen) => formatYuan(fen ?? 0)
  137. const ossImg = withOssVersion
  138. function formatMeasureText(measureValue, unitName) {
  139. const unitText = (unitName || '').trim()
  140. if (measureValue == null || measureValue === '') {
  141. return unitText ? `- ${unitText}` : '-'
  142. }
  143. const num = Number(measureValue)
  144. if (!Number.isNaN(num)) {
  145. const measureText = String(num).replace(/\.?0+$/, '')
  146. return unitText ? `${measureText} ${unitText}` : measureText
  147. }
  148. const measureText = String(measureValue).trim()
  149. return unitText ? `${measureText} ${unitText}` : measureText || '-'
  150. }
  151. async function loadProductDetail(productId) {
  152. loading.value = true
  153. try {
  154. const res = await request({ url: `/mini/client/products/${productId}`, method: 'GET' })
  155. const body = parseResponseJson(res)
  156. if (body.code !== 200 || !body.data) {
  157. uni.showToast({ title: body.message || '加载失败', icon: 'none' })
  158. product.value = null
  159. return
  160. }
  161. product.value = body.data.product || body.data || null
  162. bannerList.value = Array.isArray(body.data.banners) ? body.data.banners : []
  163. parameterValues.value = Array.isArray(body.data.parameterValues) ? body.data.parameterValues : []
  164. detailContent.value = product.value?.detailContent || ''
  165. detailMediaList.value = Array.isArray(body.data.detailMedia)
  166. ? body.data.detailMedia.map((item) => ({
  167. id: item.id,
  168. contentType: item.contentType || item.content_type || 'image',
  169. fileUrl: item.fileUrl || item.file_url || '',
  170. thumbnailUrl: item.thumbnailUrl || item.thumbnail_url,
  171. description: item.description,
  172. sortOrder: item.sortOrder ?? item.sort_order ?? 0
  173. }))
  174. : []
  175. } finally {
  176. loading.value = false
  177. }
  178. }
  179. function goToCart() {
  180. uni.switchTab({ url: '/pages/client/cart/cart' })
  181. }
  182. function openQuantityPopup(type) {
  183. actionType.value = type
  184. quantity.value = 1
  185. showQuantityPopup.value = true
  186. }
  187. function decreaseQty() {
  188. const q = Number(quantity.value) || 1
  189. if (q > 1) quantity.value = q - 1
  190. }
  191. function increaseQty() {
  192. const q = Number(quantity.value) || 1
  193. quantity.value = q + 1
  194. }
  195. async function confirmAction() {
  196. const q = Math.max(1, Number(quantity.value) || 1)
  197. quantity.value = q
  198. if (!product.value?.id) return
  199. if (actionType.value === 'cart') {
  200. await cartStore.addToCart(product.value, q)
  201. cartCount.value = cartStore.totalCount
  202. uni.showToast({ title: '已加入购物车', icon: 'success' })
  203. showQuantityPopup.value = false
  204. return
  205. }
  206. showQuantityPopup.value = false
  207. uni.navigateTo({ url: `/pages/client/order/create/create?productId=${product.value.id}&quantity=${q}` })
  208. }
  209. onLoad((options) => {
  210. userStore.checkLoginStatus()
  211. if (!userStore.isLoggedIn) return uni.reLaunch({ url: '/pages/login/login' })
  212. if (!userStore.isClient) {
  213. uni.showToast({ title: '请使用客户账号登录', icon: 'none' })
  214. return setTimeout(() => userStore.navigateToHome(), 1200)
  215. }
  216. const productId = options?.productId || ''
  217. if (!productId) {
  218. uni.showToast({ title: '缺少商品ID', icon: 'none' })
  219. return
  220. }
  221. const ft = options?.fromTab
  222. if (ft === 'home' || ft === 'order' || ft === 'cart' || ft === 'profile') {
  223. tabBarActive.value = ft
  224. }
  225. cartCount.value = cartStore.totalCount
  226. void loadProductDetail(productId)
  227. })
  228. </script>
  229. <style lang="scss" scoped>
  230. .product-view-page {
  231. height: 100vh;
  232. box-sizing: border-box;
  233. display: flex;
  234. flex-direction: column;
  235. background: var(--c-bg);
  236. padding-bottom: calc(220rpx + env(safe-area-inset-bottom, 0px));
  237. overflow: hidden;
  238. }
  239. .product-view-inner {
  240. flex: 1;
  241. display: flex;
  242. flex-direction: column;
  243. min-height: 0;
  244. }
  245. .detail-fixed-top {
  246. flex-shrink: 0;
  247. }
  248. .detail-scroll-area {
  249. flex: 1;
  250. min-height: 0;
  251. height: 0;
  252. background: var(--c-bg);
  253. }
  254. .detail-scroll-inner {
  255. padding-bottom: 24rpx;
  256. }
  257. .loading {
  258. flex: 1;
  259. min-height: 0;
  260. padding: 0;
  261. }
  262. .empty {
  263. padding: 40rpx;
  264. text-align: center;
  265. color: var(--c-muted);
  266. font-size: 26rpx;
  267. }
  268. .banner-swiper {
  269. width: 100%;
  270. aspect-ratio: 4 / 3;
  271. height: auto;
  272. background: #fff;
  273. .banner-image, .banner-video {
  274. width: 100%;
  275. height: 100%;
  276. }
  277. }
  278. .banner-empty {
  279. width: 100%;
  280. aspect-ratio: 4 / 3;
  281. height: auto;
  282. background: #f8fcfa;
  283. display: flex;
  284. align-items: center;
  285. justify-content: center;
  286. .banner-empty-text {
  287. font-size: 28rpx;
  288. color: #9ba8a2;
  289. }
  290. }
  291. .product-info {
  292. margin-top: 12rpx;
  293. background: var(--c-card);
  294. padding: 20rpx;
  295. border: 1rpx solid var(--c-border);
  296. border-radius: var(--c-radius-card);
  297. margin-left: 32rpx;
  298. margin-right: 32rpx;
  299. .price-row {
  300. display: flex;
  301. align-items: baseline;
  302. gap: 8rpx;
  303. margin-bottom: 12rpx;
  304. }
  305. .price {
  306. font-size: 44rpx;
  307. color: var(--c-danger);
  308. font-weight: 700;
  309. }
  310. .unit {
  311. font-size: 26rpx;
  312. color: var(--c-muted);
  313. }
  314. .name {
  315. display: block;
  316. font-size: 32rpx;
  317. color: var(--c-text);
  318. font-weight: 600;
  319. }
  320. .spec {
  321. display: block;
  322. margin-top: 10rpx;
  323. font-size: 26rpx;
  324. color: var(--c-muted);
  325. }
  326. }
  327. .section-title {
  328. padding: 18rpx 24rpx;
  329. font-size: 28rpx;
  330. color: var(--c-text);
  331. font-weight: 700;
  332. background: transparent;
  333. }
  334. .detail-section {
  335. margin-top: 12rpx;
  336. background: var(--c-card);
  337. border: 1rpx solid var(--c-border);
  338. border-radius: var(--c-radius-card);
  339. overflow: hidden;
  340. margin-left: 32rpx;
  341. margin-right: 32rpx;
  342. .rich-content {
  343. padding: 0 24rpx 20rpx;
  344. font-size: 28rpx;
  345. line-height: 1.6;
  346. color: #33423c;
  347. }
  348. .detail-media-list {
  349. padding-bottom: 8rpx;
  350. }
  351. .detail-media-item {
  352. border-top: 1rpx solid rgba(220, 235, 228, 0.65);
  353. }
  354. .detail-media-img {
  355. width: 100%;
  356. display: block;
  357. }
  358. .detail-media-video {
  359. width: 100%;
  360. height: 420rpx;
  361. display: block;
  362. background: #000;
  363. }
  364. .detail-media-desc {
  365. display: block;
  366. padding: 16rpx 24rpx 24rpx;
  367. font-size: 24rpx;
  368. color: var(--c-muted);
  369. line-height: 1.5;
  370. }
  371. }
  372. .params-section {
  373. margin-top: 12rpx;
  374. background: var(--c-card);
  375. border: 1rpx solid var(--c-border);
  376. border-radius: var(--c-radius-card);
  377. overflow: hidden;
  378. margin-left: 32rpx;
  379. margin-right: 32rpx;
  380. .params-list {
  381. padding: 0 24rpx 20rpx;
  382. }
  383. .param-item {
  384. display: flex;
  385. padding: 10rpx 0;
  386. border-bottom: 1rpx solid rgba(220, 235, 228, 0.65);
  387. &:last-child {
  388. border-bottom: none;
  389. }
  390. }
  391. .param-name {
  392. width: 180rpx;
  393. font-size: 26rpx;
  394. color: var(--c-muted);
  395. }
  396. .param-value {
  397. flex: 1;
  398. font-size: 26rpx;
  399. color: var(--c-text);
  400. }
  401. }
  402. .bottom-bar {
  403. position: fixed;
  404. left: 0;
  405. right: 0;
  406. bottom: calc(100rpx + env(safe-area-inset-bottom, 0px));
  407. z-index: 520;
  408. background: transparent;
  409. border-top: none;
  410. padding: 0 32rpx 14rpx;
  411. display: flex;
  412. align-items: center;
  413. gap: 16rpx;
  414. box-sizing: border-box;
  415. }
  416. .bottom-bar::before {
  417. content: '';
  418. position: absolute;
  419. left: 32rpx;
  420. right: 32rpx;
  421. bottom: 14rpx;
  422. height: 116rpx;
  423. border-radius: 36rpx;
  424. background: linear-gradient(145deg, #ffffff, #f5fbf8);
  425. border: 1rpx solid #d9ebe3;
  426. box-shadow: var(--c-shadow-soft);
  427. }
  428. .action-left {
  429. position: relative;
  430. width: 92rpx;
  431. height: 92rpx;
  432. border-radius: 46rpx;
  433. background: #ffffff;
  434. display: flex;
  435. align-items: center;
  436. justify-content: center;
  437. flex-shrink: 0;
  438. margin-left: 10rpx;
  439. .cart-icon {
  440. font-size: 44rpx;
  441. }
  442. .badge {
  443. position: absolute;
  444. top: -6rpx;
  445. right: -6rpx;
  446. min-width: 32rpx;
  447. height: 32rpx;
  448. padding: 0 6rpx;
  449. background: #ee0a24;
  450. color: #fff;
  451. font-size: 20rpx;
  452. border-radius: 16rpx;
  453. display: flex;
  454. align-items: center;
  455. justify-content: center;
  456. }
  457. }
  458. .action-btns {
  459. position: relative;
  460. flex: 1;
  461. display: flex;
  462. height: 80rpx;
  463. border-radius: 40rpx;
  464. overflow: hidden;
  465. margin-right: 10rpx;
  466. }
  467. .add-cart-btn, .buy-btn {
  468. flex: 1;
  469. display: flex;
  470. align-items: center;
  471. justify-content: center;
  472. font-size: 28rpx;
  473. }
  474. .add-cart-btn {
  475. background: #fff7e6;
  476. color: #ff976a;
  477. }
  478. .buy-btn {
  479. background: var(--c-brand-green);
  480. color: #fff;
  481. }
  482. .quantity-popup {
  483. padding: 30rpx;
  484. .product-row {
  485. display: flex;
  486. margin-bottom: 30rpx;
  487. .popup-image {
  488. width: 160rpx;
  489. height: 160rpx;
  490. border-radius: 12rpx;
  491. background: #f5f7fa;
  492. }
  493. .popup-info {
  494. flex: 1;
  495. margin-left: 20rpx;
  496. display: flex;
  497. flex-direction: column;
  498. justify-content: center;
  499. .popup-name {
  500. font-size: 28rpx;
  501. color: #303133;
  502. margin-bottom: 12rpx;
  503. }
  504. .popup-price {
  505. font-size: 36rpx;
  506. color: var(--c-danger);
  507. font-weight: 700;
  508. }
  509. }
  510. }
  511. .quantity-row {
  512. display: flex;
  513. justify-content: space-between;
  514. align-items: center;
  515. padding: 20rpx 0;
  516. border-top: 1rpx solid #ebeef5;
  517. .quantity-label {
  518. font-size: 28rpx;
  519. color: #303133;
  520. }
  521. .quantity-control {
  522. display: flex;
  523. align-items: center;
  524. .qty-btn {
  525. width: 60rpx;
  526. height: 60rpx;
  527. background: #f5f7fa;
  528. border-radius: 8rpx;
  529. display: flex;
  530. align-items: center;
  531. justify-content: center;
  532. font-size: 32rpx;
  533. color: #606266;
  534. }
  535. .qty-input {
  536. width: 90rpx;
  537. height: 60rpx;
  538. text-align: center;
  539. font-size: 28rpx;
  540. background: #f5f7fa;
  541. margin: 0 12rpx;
  542. border-radius: 8rpx;
  543. }
  544. }
  545. }
  546. .confirm-btn {
  547. height: 88rpx;
  548. line-height: 88rpx;
  549. background: var(--c-brand-green);
  550. color: #fff;
  551. font-size: 32rpx;
  552. text-align: center;
  553. border-radius: 44rpx;
  554. margin-top: 30rpx;
  555. }
  556. }
  557. </style>
  558. <style lang="scss">
  559. /* 与首页一致:禁止页面级滚动,仅中间 scroll-view 滚动 */
  560. page {
  561. height: 100%;
  562. overflow: hidden;
  563. background-color: #f5f8f7;
  564. }
  565. </style>