details.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <!-- 商品详情 -->
  2. <template>
  3. <Navbar />
  4. <div class="default-width pb-5 mt-3" 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="showPrize">房券抽奖活动</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 && lotteryId" :lotteryId="lotteryId" />
  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. import { getPrizeByGoodsId } from '@/api/mall/prize'
  93. const router = useRouter()
  94. const { id } = router.currentRoute.value.params
  95. const describeTab = ref(2)
  96. const selectedSkuPicUrl = ref('')
  97. const selectedSkuPrice = ref('')
  98. const selectedSkuMarketPrice = ref('')
  99. const showSelectSku = ref(false)
  100. const carouselHover = ref(false)
  101. const showSelectedSkuImg = ref(false)
  102. const showPrize = ref(false)
  103. const lotteryId = ref('')
  104. // 获取商品详情
  105. const getData = async () => {
  106. const obj = await getProductDetail({ id })
  107. if (!obj) return Snackbar.warning('未找到商品!')
  108. //
  109. // 加载到商品
  110. obj.sliderPicUrls = obj.sliderPicUrls || []
  111. state.skeletonLoading = false;
  112. state.goodsInfo = obj
  113. showSelectSku.value = true
  114. // 查询当前商品是否参与抽奖活动
  115. const data = await getPrizeByGoodsId(id)
  116. showPrize.value = data && Object.keys(data).length > 0
  117. lotteryId.value = data.id
  118. }
  119. getData()
  120. const calcPrice = (price) => { return price && (price-0) ? (price-0)/100 : '--' }
  121. const state = reactive({
  122. goodsId: id || 0,
  123. skeletonLoading: true, // SPU 加载中
  124. goodsInfo: {}, // SPU 信息
  125. showSelectSku: false, // 是否展示 SKU 选择弹窗
  126. selectedSku: {}, // 选中的 SKU
  127. settlementSku: {}, // 结算的 SKU:由于 selectedSku 不进行默认选中,所以初始使用结算价格最低的 SKU 作为基础展示
  128. showModel: false, // 是否展示 Coupon 优惠劵的弹窗
  129. couponInfo: [], // 可领取的 Coupon 优惠劵的列表
  130. showActivityModel: false, // 【满减送/限时折扣】是否展示 Activity 营销活动的弹窗
  131. rewardActivity: {}, // 【满减送】活动
  132. activityList: [], // 【秒杀/拼团/砍价】可参与的 Activity 营销活动的列表
  133. });
  134. // 规格变更
  135. function onSkuChange(e) {
  136. state.selectedSku = e;
  137. state.settlementSku = e;
  138. // console.log('onSkuChange:', e)
  139. selectedSkuPicUrl.value = state.selectedSku?.picUrl || ''
  140. if (selectedSkuPicUrl.value) showSelectedSkuImg.value = true
  141. selectedSkuPrice.value = calcPrice(state.selectedSku?.price || state.goodsInfo.price)
  142. selectedSkuMarketPrice.value = calcPrice(state.selectedSku?.marketPrice || state.goodsInfo.marketPrice)
  143. }
  144. const showSettlement = ref(false)
  145. const skuInfo = ref(null) // 购买商品规格信息
  146. const onBuy = async (e) => {
  147. if (!getToken()) return handleLogin()
  148. if (!e?.id) return Snackbar.warning('请选择商品规格!')
  149. // console.log('购买规格信息:', e)
  150. //
  151. skuInfo.value = JSON.stringify({
  152. items: [{ skuId: e.id, count: e.goods_num, categoryId: state.goodsInfo.categoryId, }]
  153. })
  154. showSettlement.value = true
  155. }
  156. // 创建订单完成
  157. const payRef = ref()
  158. const showPay = ref(false)
  159. const payOrderId = ref('')
  160. const orderCreated = (id) => {
  161. showSettlement.value = false
  162. payOrderId.value = id
  163. showPay.value = true
  164. }
  165. //
  166. const payCancel = () => {
  167. Snackbar.warning('您已取消支付!')
  168. showPay.value = false
  169. setTimeout(() => { router.push({ path: '/mall/user/order', query: { tab: 0 } }) }, 500);
  170. }
  171. const paySuccess = (e) => {
  172. // Snackbar.success('支付成功,请前往我的订单查看!')
  173. // showPay.value = false
  174. router.push({ path: '/mall/payOver', query: { price: e.price, spuId: id }})
  175. }
  176. // 添加购物车
  177. const onAddCart = async (e) => {
  178. if (!getToken()) return handleLogin()
  179. if (!e.id) {
  180. Snackbar.warning('请选择商品规格')
  181. return;
  182. }
  183. await addCart({ skuId: e.id, count: e.goods_num })
  184. Snackbar.success('已添加到购物车~')
  185. // 刷新购物车列表
  186. }
  187. const confirmRef = ref()
  188. const handleSubmit = () => {
  189. if (confirmRef.value) confirmRef.value.onConfirm()
  190. }
  191. const handleClose = () => {
  192. showSettlement.value = false
  193. }
  194. const showLogin = ref(false)
  195. const handleLogin = () => {
  196. Snackbar.warning('您还未登录,请先登录后再试')
  197. showLogin.value = true // 打开快速登录弹窗
  198. }
  199. // 快速登录
  200. const loginSuccess = () => {
  201. showLogin.value = false
  202. Snackbar.success('登录成功')
  203. // router.go(0)
  204. }
  205. const loginClose = () => {
  206. showLogin.value = false
  207. Snackbar.warning('您已取消登录')
  208. }
  209. </script>
  210. <style lang="scss" scoped>
  211. .border-radius-8 {
  212. border-radius: 8px;
  213. }
  214. .title-name {
  215. font-size: 24px;
  216. line-height: 1.25;
  217. color: #000;
  218. margin-bottom: 12px;
  219. font-weight: bold;
  220. }
  221. .title-introduction {
  222. font-size: 18px;
  223. line-height: 1.5;
  224. color: #6b6b6b;
  225. }
  226. .parameterColor {
  227. color: #7a7a7a;
  228. }
  229. .prices {
  230. display: flex;
  231. align-items: flex-end;
  232. color: #ff5000;
  233. font-size: 28px;
  234. margin: 8px 0;
  235. }
  236. .price {
  237. font-weight: 600;
  238. span {
  239. font-size: 16px;
  240. }
  241. }
  242. .marketPrice {
  243. color: #b7b7b7;
  244. font-size: 16px;
  245. line-height: 34px;
  246. // text-decoration: line-through;
  247. }
  248. .selectedSkuImgBox {
  249. position: relative;
  250. .close {
  251. position: absolute;
  252. bottom: 10px;
  253. right: 10px;
  254. }
  255. }
  256. </style>