details.vue 9.5 KB

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