details.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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" prepend-icon="mdi-chevron-triple-left" @click="router.go(-1)">返回上一页</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-tabs>
  63. </div>
  64. <describe v-if="describeTab === 0 && state.goodsInfo?.description" :content="state.goodsInfo.description"></describe>
  65. <comment v-if="describeTab === 1 && state.goodsId" class="detail-comment-selector" :goodsId="state.goodsId" />
  66. </div>
  67. </v-card>
  68. </div>
  69. <!-- 快速登录 -->
  70. <loginPage v-if="showLogin" @loginSuccess="loginSuccess" @close="loginClose"></loginPage>
  71. <CtDialog :visible="showPay" titleClass="text-h6" :widthType="3" title="订单信息" @submit="handleSubmit" @close="handleClose">
  72. <confirm ref="confirmRef" :data="skuInfo" @paySuccess="showPay = false"></confirm>
  73. </CtDialog>
  74. </template>
  75. <script setup>
  76. defineOptions({name: 'goods-details'})
  77. import { getProductDetail } from '@/api/mall/product'
  78. import { addCart } from '@/api/mall/cart'
  79. import selectSku from './details/s-select-sku.vue'
  80. import describe from './details/describe.vue'
  81. import comment from './details/detail-comment-card.vue'
  82. import confirm from './details/order/confirm.vue'
  83. import { ref, reactive } from 'vue'
  84. import { useRouter } from 'vue-router'
  85. import Snackbar from '@/plugins/snackbar'
  86. import { getToken } from '@/utils/auth'
  87. import loginPage from '@/views/common/loginDialog.vue'
  88. const router = useRouter()
  89. const { id } = router.currentRoute.value.params
  90. const describeTab = ref(0)
  91. const selectedSkuPicUrl = ref('')
  92. const selectedSkuPrice = ref('')
  93. const selectedSkuMarketPrice = ref('')
  94. const showSelectSku = ref(false)
  95. const carouselHover = ref(false)
  96. const showSelectedSkuImg = ref(false)
  97. // 获取商品详情
  98. const getData = async () => {
  99. const obj = await getProductDetail({ id })
  100. if (!obj) return Snackbar.warning('未找到商品!')
  101. //
  102. // 加载到商品
  103. obj.sliderPicUrls = obj.sliderPicUrls || []
  104. state.skeletonLoading = false;
  105. state.goodsInfo = obj
  106. showSelectSku.value = true
  107. // // 加载是否收藏
  108. // if (isLogin.value) {
  109. // FavoriteApi.isFavoriteExists(state.goodsId, 'goods').then((res) => {
  110. // if (res.code !== 0) {
  111. // return;
  112. // }
  113. // state.goodsInfo.favorite = res.data;
  114. // });
  115. // }
  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 showPay = 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. showPay.value = true
  153. }
  154. // 添加购物车
  155. const onAddCart = async (e) => {
  156. if (!e.id) {
  157. Snackbar.warning('请选择商品规格')
  158. return;
  159. }
  160. await addCart({ skuId: e.id, count: e.goods_num })
  161. Snackbar.success('已添加到购物车~')
  162. // 刷新购物车列表
  163. }
  164. const confirmRef = ref()
  165. const handleSubmit = () => {
  166. if (confirmRef.value) confirmRef.value.onConfirm()
  167. }
  168. const handleClose = () => {
  169. showPay.value = false
  170. }
  171. const showLogin = ref(false)
  172. const handleLogin = () => {
  173. Snackbar.warning('您还未登录,请先登录后再试')
  174. showLogin.value = true // 打开快速登录弹窗
  175. }
  176. // 快速登录
  177. const loginSuccess = () => {
  178. showLogin.value = false
  179. Snackbar.success('登录成功')
  180. // router.go(0)
  181. }
  182. const loginClose = () => {
  183. showLogin.value = false
  184. Snackbar.warning('您已取消登录')
  185. }
  186. </script>
  187. <style lang="scss" scoped>
  188. .border-radius-8 {
  189. border-radius: 8px;
  190. }
  191. .title-name {
  192. font-size: 24px;
  193. line-height: 1.25;
  194. color: #000;
  195. margin-bottom: 12px;
  196. font-weight: bold;
  197. }
  198. .title-introduction {
  199. font-size: 18px;
  200. line-height: 1.5;
  201. color: #6b6b6b;
  202. }
  203. .parameterColor {
  204. color: #7a7a7a;
  205. }
  206. .prices {
  207. display: flex;
  208. align-items: flex-end;
  209. color: #ff5000;
  210. font-size: 28px;
  211. margin: 8px 0;
  212. }
  213. .price {
  214. font-weight: 600;
  215. span {
  216. font-size: 16px;
  217. }
  218. }
  219. .marketPrice {
  220. color: #b7b7b7;
  221. font-size: 16px;
  222. line-height: 34px;
  223. // text-decoration: line-through;
  224. }
  225. .selectedSkuImgBox {
  226. position: relative;
  227. .close {
  228. position: absolute;
  229. bottom: 10px;
  230. right: 10px;
  231. }
  232. }
  233. </style>