details.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <!-- 商品详情 -->
  2. <template>
  3. <Navbar class="mb-3" />
  4. <div class="default-width pb-5" v-if="state.goodsInfo && Object.keys(state.goodsInfo).length">
  5. <v-card class="carousel border-radius-8 white-bgc pa-5" style="width: 100%;">
  6. <div class=" d-flex">
  7. <!-- 图片展示-轮播 -->
  8. <div style="width: 500px; height: 500px;">
  9. <div v-if="selectedSkuPicUrl" class="selectedSkuImgBox" @mouseover="showSelectedSkuImg = true" @mouseleave="showSelectedSkuImg = false">
  10. <v-img :src="selectedSkuPicUrl" :aspect-ratio="1" style="border-radius: 8px;"></v-img>
  11. <v-btn
  12. v-show="showSelectedSkuImg"
  13. size="x-small"
  14. class="close px-3"
  15. @click="selectedSkuPicUrl = ''"
  16. >关闭</v-btn>
  17. </div>
  18. <v-carousel v-else show-arrows="hover" cycle :model-value="0" :hide-delimiters="!carouselHover" @mouseover="carouselHover = true" @mouseleave="carouselHover = false">
  19. <v-carousel-item v-for="(imgUrl, i) in state.goodsInfo.sliderPicUrls" :key="'wareImg'+i" @click="null">
  20. <v-img :src="imgUrl" :aspect-ratio="1" style="border-radius: 8px;"></v-img>
  21. </v-carousel-item>
  22. </v-carousel>
  23. </div>
  24. <!-- s-select-sku 商品属性选择 -->
  25. <div style="flex: 1;" class="pl-10">
  26. <!-- 大标题 -->
  27. <div class="title-name">{{ state.goodsInfo?.name || '--' }}</div>
  28. <!-- 小标题 -->
  29. <div class="title-introduction">{{ state.goodsInfo?.introduction || '--' }}</div>
  30. <!-- 价格 -->
  31. <div class="prices my-5">
  32. <div class="price mr-5"><span>¥</span>{{ selectedSkuPrice}}</div>
  33. <div class="marketPrice" v-if="selectedSkuMarketPrice && selectedSkuMarketPrice !== selectedSkuPrice">优惠前¥{{ selectedSkuMarketPrice}}</div>
  34. </div>
  35. <!-- 销量 -->
  36. <div class="salesCount mb-5 parameterColor"><span class="l-s-10">已售</span>:{{ state.goodsInfo?.salesCount || 0 }}</div>
  37. <!-- 属性选择组件 -->
  38. <selectSku
  39. v-if="showSelectSku"
  40. class="mb-7"
  41. :goodsInfo="state.goodsInfo"
  42. @change="onSkuChange"
  43. @buy="onBuy"
  44. @addCart="onAddCart"
  45. ></selectSku>
  46. </div>
  47. </div>
  48. </v-card>
  49. <!-- 详情描述 detail-content-card -->
  50. <v-card class="carousel border-radius-8 white-bgc pa-5 mt-3" style="width: 100%;">
  51. <div>
  52. <div class="mb-3">
  53. <v-tabs v-model="describeTab" align-tabs="start" color="primary" bg-color="#f7f8fa" @update:model-value="null">
  54. <v-tab :value="0">商品介绍</v-tab>
  55. <v-tab :value="1">商品评价</v-tab>
  56. <v-tab :value="2" v-if="(id-0)=== 648">房券抽奖活动</v-tab>
  57. </v-tabs>
  58. </div>
  59. <describe v-if="describeTab === 0 && state.goodsInfo?.description" :content="state.goodsInfo.description"></describe>
  60. <commentCard v-if="describeTab === 1 && state.goodsId" class="detail-comment-selector" :goodsId="state.goodsId" />
  61. <prizeDrawContent v-if="describeTab === 2" />
  62. </div>
  63. </v-card>
  64. </div>
  65. <!-- 快速登录 -->
  66. <loginPage v-if="showLogin" @loginSuccess="loginSuccess" @close="loginClose"></loginPage>
  67. <!-- 结算 -->
  68. <CtDialog :visible="showSettlement" titleClass="text-h6" :widthType="3" title="订单信息" @submit="handleSubmit" @close="handleClose">
  69. <confirm ref="confirmRef" :data="skuInfo" @orderCreated="orderCreated"></confirm>
  70. </CtDialog>
  71. <!-- 支付 -->
  72. <CtDialog :visible="showPay" titleClass="text-h6" :widthType="3" title="收银台" :footer="false" @close="payCancel">
  73. <pay ref="payRef" :id="payOrderId" @paySuccess="paySuccess"></pay>
  74. </CtDialog>
  75. </template>
  76. <script setup>
  77. defineOptions({name: 'goods-details'})
  78. import Navbar from '@/views/mall/components/navbar.vue'
  79. import { getProductDetail } from '@/api/mall/product'
  80. import { addCart } from '@/api/mall/cart'
  81. import selectSku from './details/s-select-sku.vue'
  82. import describe from './details/describe.vue'
  83. import commentCard from './details/detail-comment-card.vue'
  84. import prizeDrawContent from './details/prizeDrawContent.vue'
  85. import confirm from './details/order/confirm.vue'
  86. import { ref, reactive } from 'vue'
  87. import { useRouter } from 'vue-router'
  88. import Snackbar from '@/plugins/snackbar'
  89. import { getToken } from '@/utils/auth'
  90. import loginPage from '@/views/common/loginDialog.vue'
  91. import pay from '@/views/mall/components/details/order/pay.vue'
  92. const router = useRouter()
  93. const { id } = router.currentRoute.value.params
  94. const describeTab = ref(2)
  95. const selectedSkuPicUrl = ref('')
  96. const selectedSkuPrice = ref('')
  97. const selectedSkuMarketPrice = ref('')
  98. const showSelectSku = ref(false)
  99. const carouselHover = ref(false)
  100. const showSelectedSkuImg = ref(false)
  101. // 获取商品详情
  102. const getData = async () => {
  103. const obj = await getProductDetail({ id })
  104. if (!obj) return Snackbar.warning('未找到商品!')
  105. //
  106. // 加载到商品
  107. obj.sliderPicUrls = obj.sliderPicUrls || []
  108. state.skeletonLoading = false;
  109. state.goodsInfo = obj
  110. showSelectSku.value = true
  111. }
  112. getData()
  113. const calcPrice = (price) => { return price && (price-0) ? (price-0)/100 : '--' }
  114. const state = reactive({
  115. goodsId: id || 0,
  116. skeletonLoading: true, // SPU 加载中
  117. goodsInfo: {}, // SPU 信息
  118. showSelectSku: false, // 是否展示 SKU 选择弹窗
  119. selectedSku: {}, // 选中的 SKU
  120. settlementSku: {}, // 结算的 SKU:由于 selectedSku 不进行默认选中,所以初始使用结算价格最低的 SKU 作为基础展示
  121. showModel: false, // 是否展示 Coupon 优惠劵的弹窗
  122. couponInfo: [], // 可领取的 Coupon 优惠劵的列表
  123. showActivityModel: false, // 【满减送/限时折扣】是否展示 Activity 营销活动的弹窗
  124. rewardActivity: {}, // 【满减送】活动
  125. activityList: [], // 【秒杀/拼团/砍价】可参与的 Activity 营销活动的列表
  126. });
  127. // 规格变更
  128. function onSkuChange(e) {
  129. state.selectedSku = e;
  130. state.settlementSku = e;
  131. // console.log('onSkuChange:', e)
  132. selectedSkuPicUrl.value = state.selectedSku?.picUrl || ''
  133. if (selectedSkuPicUrl.value) showSelectedSkuImg.value = true
  134. selectedSkuPrice.value = calcPrice(state.selectedSku?.price || state.goodsInfo.price)
  135. selectedSkuMarketPrice.value = calcPrice(state.selectedSku?.marketPrice || state.goodsInfo.marketPrice)
  136. }
  137. const showSettlement = ref(false)
  138. const skuInfo = ref(null) // 购买商品规格信息
  139. const onBuy = async (e) => {
  140. if (!getToken()) return handleLogin()
  141. if (!e?.id) return Snackbar.warning('请选择商品规格!')
  142. // console.log('购买规格信息:', e)
  143. //
  144. skuInfo.value = JSON.stringify({
  145. items: [{ skuId: e.id, count: e.goods_num, categoryId: state.goodsInfo.categoryId, }]
  146. })
  147. showSettlement.value = true
  148. }
  149. // 创建订单完成
  150. const payRef = ref()
  151. const showPay = ref(false)
  152. const payOrderId = ref('')
  153. const orderCreated = (id) => {
  154. showSettlement.value = false
  155. payOrderId.value = id
  156. showPay.value = true
  157. }
  158. //
  159. const payCancel = () => {
  160. Snackbar.warning('您已取消支付!')
  161. showPay.value = false
  162. setTimeout(() => { router.push({ path: '/mall/user/order', query: { tab: 0 } }) }, 500);
  163. }
  164. const paySuccess = (e) => {
  165. // Snackbar.success('支付成功,请前往我的订单查看!')
  166. // showPay.value = false
  167. router.push({ path: '/mall/payOver', query: { price: e.price }})
  168. }
  169. // 添加购物车
  170. const onAddCart = async (e) => {
  171. if (!getToken()) return handleLogin()
  172. if (!e.id) {
  173. Snackbar.warning('请选择商品规格')
  174. return;
  175. }
  176. await addCart({ skuId: e.id, count: e.goods_num })
  177. Snackbar.success('已添加到购物车~')
  178. // 刷新购物车列表
  179. }
  180. const confirmRef = ref()
  181. const handleSubmit = () => {
  182. if (confirmRef.value) confirmRef.value.onConfirm()
  183. }
  184. const handleClose = () => {
  185. showSettlement.value = false
  186. }
  187. const showLogin = ref(false)
  188. const handleLogin = () => {
  189. Snackbar.warning('您还未登录,请先登录后再试')
  190. showLogin.value = true // 打开快速登录弹窗
  191. }
  192. // 快速登录
  193. const loginSuccess = () => {
  194. showLogin.value = false
  195. Snackbar.success('登录成功')
  196. // router.go(0)
  197. }
  198. const loginClose = () => {
  199. showLogin.value = false
  200. Snackbar.warning('您已取消登录')
  201. }
  202. </script>
  203. <style lang="scss" scoped>
  204. .border-radius-8 {
  205. border-radius: 8px;
  206. }
  207. .title-name {
  208. font-size: 24px;
  209. line-height: 1.25;
  210. color: #000;
  211. margin-bottom: 12px;
  212. font-weight: bold;
  213. }
  214. .title-introduction {
  215. font-size: 18px;
  216. line-height: 1.5;
  217. color: #6b6b6b;
  218. }
  219. .parameterColor {
  220. color: #7a7a7a;
  221. }
  222. .prices {
  223. display: flex;
  224. align-items: flex-end;
  225. color: #ff5000;
  226. font-size: 28px;
  227. margin: 8px 0;
  228. }
  229. .price {
  230. font-weight: 600;
  231. span {
  232. font-size: 16px;
  233. }
  234. }
  235. .marketPrice {
  236. color: #b7b7b7;
  237. font-size: 16px;
  238. line-height: 34px;
  239. // text-decoration: line-through;
  240. }
  241. .selectedSkuImgBox {
  242. position: relative;
  243. .close {
  244. position: absolute;
  245. bottom: 10px;
  246. right: 10px;
  247. }
  248. }
  249. </style>