index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. <template>
  2. <view>
  3. <s-layout :onShareAppMessage="shareInfo" navbar="goods">
  4. <!-- 标题栏 -->
  5. <detailNavbar />
  6. <!-- 骨架屏 -->
  7. <detailSkeleton v-if="state.skeletonLoading" />
  8. <!-- 下架/售罄提醒 -->
  9. <s-empty v-else-if="state.goodsInfo === null" text="商品不存在或已下架" icon="/static/soldout-empty.png" showAction
  10. actionText="再逛逛" actionUrl="/pages/goods/list" />
  11. <block v-else>
  12. <view class="detail-swiper-selector">
  13. <!-- 商品轮播图 -->
  14. <su-swiper class="ss-m-b-14" isPreview :list="formatGoodsSwiper(state.goodsInfo.sliderPicUrls)"
  15. otStyle="tag" imageMode="widthFix" dotCur="bg-mask-40" :seizeHeight="750" />
  16. <!-- 限时折扣 -->
  17. <view class="discount" v-if="setShow">
  18. <image class="disImg" src="../../static/images/dis.png"></image>
  19. <view class="discountCont">
  20. <view class="disContT">
  21. <view class="disContT1">
  22. <view class="disContT1P">
  23. ¥{{fen2yuan(settleData.price)}}
  24. </view>
  25. <view class="disContT1End">
  26. 直降¥{{fen2yuan( state.goodsInfo.price - settleData.price)}}
  27. </view>
  28. </view>
  29. <view class="disContT2">
  30. 限时折扣
  31. </view>
  32. </view>
  33. <view class="disContB">
  34. <view class="disContB1">
  35. 价格:¥{{fen2yuan(state.goodsInfo.price)}} 丨 剩余:{{settleData.stock}}
  36. </view>
  37. <view class="disContB2">
  38. 距结束仅剩
  39. <countDown :tipText="' '" :bgColor="bgColor" :dayText="':'" :hourText="':'"
  40. :minuteText="':'" :secondText="' '" :datatime="settleData.endTime / 1000"
  41. :isDay="false" />
  42. </view>
  43. </view>
  44. </view>
  45. </view>
  46. <!-- 限时折扣 -->
  47. <!-- 价格+标题 -->
  48. <view class="title-card detail-card ss-p-y-40 ss-p-x-20">
  49. <view class="ss-flex ss-row-between ss-col-center ss-m-b-26" v-if="!setShow">
  50. <view class="price-box ss-flex ss-col-bottom">
  51. <view class="price-text ss-m-r-16">
  52. {{ fen2yuan(state.selectedSku.price || state.goodsInfo.price) }}
  53. </view>
  54. <view class="origin-price-text" v-if="state.goodsInfo.marketPrice > 0">
  55. {{ fen2yuan(state.selectedSku.marketPrice || state.goodsInfo.marketPrice) }}
  56. </view>
  57. </view>
  58. <view class="sales-text">
  59. {{ formatSales('exact', state.goodsInfo.salesCount) }}
  60. </view>
  61. </view>
  62. <view class="discounts-box ss-flex ss-row-between ss-m-b-28">
  63. <!-- 满减送/限时折扣活动的提示 -->
  64. <div class="tag-content">
  65. <view class="tag-box ss-flex">
  66. <view class="tag ss-m-r-10" v-for="promos in state.activityInfo" :key="promos.id"
  67. @tap="onActivity">
  68. {{ promos.name }}
  69. </view>
  70. </view>
  71. </div>
  72. <!-- 优惠劵@tap="state.showModel = true" -->
  73. <view class="get-coupon-box ss-flex ss-col-center ss-m-l-20" @tap="onActivity"
  74. v-if="state.couponInfo.length">
  75. <view class="discounts-title ss-m-r-8">领券</view>
  76. <text class="cicon-forward"></text>
  77. </view>
  78. </view>
  79. <view class="title-text ss-line-2 ss-m-b-6">{{ state.goodsInfo.name }}</view>
  80. <view class="subtitle-text ss-line-1">{{ state.goodsInfo.introduction }}</view>
  81. </view>
  82. <!-- 功能卡片 -->
  83. <view class="detail-cell-card detail-card ss-flex-col">
  84. <detail-cell-sku v-model="state.selectedSku.goods_sku_text" :sku="state.selectedSku"
  85. @tap="state.showSelectSku = true" />
  86. </view>
  87. <!-- 规格与数量弹框 -->
  88. <s-select-sku :goodsInfo="state.goodsInfo" :show="state.showSelectSku" @addCart="onAddCart"
  89. @buy="onBuy" @change="onSkuChange" @close="state.showSelectSku = false" />
  90. </view>
  91. <!-- 评价 -->
  92. <detail-comment-card class="detail-comment-selector" :goodsId="state.goodsId" />
  93. <!-- 详情 -->
  94. <detail-content-card class="detail-content-selector" :content="state.goodsInfo.description" />
  95. <!-- 活动跳转:拼团/秒杀/砍价活动 -->
  96. <detail-activity-tip v-if="state.activityList.length > 0" :activity-list="state.activityList" />
  97. <!-- 详情 tabbar -->
  98. <detail-tabbar v-model="state.goodsInfo">
  99. <view class="buy-box ss-flex ss-col-center ss-p-r-20" v-if="state.goodsInfo.stock > 0">
  100. <button class="ss-reset-button add-btn ui-Shadow-Main" @tap="state.showSelectSku = true">
  101. 加入购物车
  102. </button>
  103. <button class="ss-reset-button buy-btn ui-Shadow-Main" @tap="state.showSelectSku = true">
  104. 立即购买
  105. </button>
  106. </view>
  107. <view class="buy-box ss-flex ss-col-center ss-p-r-20" v-else>
  108. <button class="ss-reset-button disabled-btn" disabled> 已售罄 </button>
  109. </view>
  110. </detail-tabbar>
  111. <!-- 优惠劵弹窗 -->
  112. <!-- <s-coupon-get v-model="state.couponInfo" :show="state.showModel" @close="state.showModel = false"
  113. @get="onGet" /> -->
  114. <!-- 满减送/限时折扣活动弹窗 -->
  115. <s-activity-pop v-model="state" :show="state.showActivityModel"
  116. @close="state.showActivityModel = false" @get="onGet" />
  117. </block>
  118. </s-layout>
  119. </view>
  120. </template>
  121. <script setup>
  122. import {
  123. reactive,
  124. computed,
  125. ref
  126. } from 'vue';
  127. import {
  128. onLoad,
  129. onPageScroll
  130. } from '@dcloudio/uni-app';
  131. import sheep from '@/sheep';
  132. import CouponApi from '@/sheep/api/promotion/coupon';
  133. import ActivityApi from '@/sheep/api/promotion/activity';
  134. import FavoriteApi from '@/sheep/api/product/favorite';
  135. import {
  136. formatSales,
  137. formatGoodsSwiper,
  138. fen2yuan,
  139. handList,
  140. handListPrice
  141. } from '@/sheep/hooks/useGoods';
  142. import detailNavbar from './components/detail/detail-navbar.vue';
  143. import detailCellSku from './components/detail/detail-cell-sku.vue';
  144. import detailTabbar from './components/detail/detail-tabbar.vue';
  145. import detailSkeleton from './components/detail/detail-skeleton.vue';
  146. import detailCommentCard from './components/detail/detail-comment-card.vue';
  147. import detailContentCard from './components/detail/detail-content-card.vue';
  148. import detailActivityTip from './components/detail/detail-activity-tip.vue';
  149. import {
  150. isEmpty
  151. } from 'lodash-es';
  152. import SpuApi from '@/sheep/api/product/spu';
  153. onPageScroll(() => {
  154. });
  155. import countDown from '@/sheep/components/countDown/index.vue'
  156. const bgColor = {
  157. 'bgColor': '#E93323',
  158. 'Color': '#fff',
  159. 'width': '44rpx',
  160. 'timeTxtwidth': '16rpx',
  161. 'isDay': true
  162. }
  163. const isLogin = computed(() => sheep.$store('user').isLogin);
  164. const state = reactive({
  165. goodsId: 0,
  166. skeletonLoading: true, // SPU 加载中
  167. goodsInfo: {}, // SPU 信息
  168. showSelectSku: false, // 是否展示 SKU 选择弹窗
  169. selectedSku: {}, // 选中的 SKU
  170. showModel: false, // 是否展示 Coupon 优惠劵的弹窗
  171. couponInfo: [], // 可领取的 Coupon 优惠劵的列表
  172. showActivityModel: false, // 【满减送/限时折扣】是否展示 Activity 营销活动的弹窗
  173. activityInfo: [], // 【满减送/限时折扣】可参与的 Activity 营销活动的列表
  174. activityList: [], // 【秒杀/拼团/砍价】可参与的 Activity 营销活动的列表
  175. });
  176. // 规格变更
  177. function onSkuChange(e) {
  178. if (e.type == 4) {
  179. settleData.value = e
  180. setShow.value = true
  181. } else {
  182. state.selectedSku = e;
  183. setShow.value = false
  184. }
  185. }
  186. // 添加购物车
  187. function onAddCart(e) {
  188. if (!e.id) {
  189. sheep.$helper.toast('请选择商品规格');
  190. return;
  191. }
  192. sheep.$store('cart').add(e);
  193. }
  194. // 立即购买
  195. function onBuy(e) {
  196. if (!e.id) {
  197. sheep.$helper.toast('请选择商品规格');
  198. return;
  199. }
  200. sheep.$router.go('/pages/order/confirm', {
  201. data: JSON.stringify({
  202. items: [{
  203. skuId: e.id,
  204. count: e.goods_num,
  205. categoryId: state.goodsInfo.categoryId
  206. }]
  207. }),
  208. });
  209. }
  210. // 营销活动
  211. function onActivity() {
  212. state.showActivityModel = true;
  213. }
  214. // 立即领取
  215. async function onGet(id) {
  216. const {
  217. code
  218. } = await CouponApi.takeCoupon(id);
  219. if (code !== 0) {
  220. return;
  221. }
  222. uni.showToast({
  223. title: '领取成功',
  224. });
  225. setTimeout(() => {
  226. getCoupon();
  227. }, 1000);
  228. }
  229. // TODO 芋艿:待测试
  230. const shareInfo = computed(() => {
  231. if (isEmpty(state.goodsInfo)) return {};
  232. return sheep.$platform.share.getShareInfo({
  233. title: state.goodsInfo.name,
  234. image: sheep.$url.cdn(state.goodsInfo.picUrl),
  235. desc: state.goodsInfo.introduction,
  236. params: {
  237. page: '2',
  238. query: state.goodsInfo.id,
  239. },
  240. }, {
  241. type: 'goods', // 商品海报
  242. title: state.goodsInfo.name, // 商品名称
  243. image: sheep.$url.cdn(state.goodsInfo.picUrl), // 商品主图
  244. price: fen2yuan(state.goodsInfo.price), // 商品价格
  245. original_price: fen2yuan(state.goodsInfo.marketPrice), // 商品原价
  246. });
  247. });
  248. async function getCoupon() {
  249. const {
  250. code,
  251. data
  252. } = await CouponApi.getCouponTemplateList(state.goodsId, 2, 10);
  253. if (code === 0) {
  254. state.couponInfo = data;
  255. }
  256. }
  257. //获取结算信息
  258. const setShow = ref(false)
  259. const settleData = ref()
  260. async function getSettlementByIds(ids) {
  261. const { data } = await SpuApi.getSettlementProduct(ids);
  262. settleData.value = handle(data)
  263. state.goodsInfo.skus = handListPrice(state.goodsInfo.skus, data[0].skus)
  264. }
  265. //判断是否有限时折扣信息
  266. function handle(array) {
  267. let setList = {}
  268. array.some(item => {
  269. return item.skus.some(items => {
  270. if (items.type === 4) {
  271. setShow.value = true;
  272. setList = items;
  273. return true; // 返回true以结束some循环
  274. }
  275. return false; // 继续遍历
  276. });
  277. });
  278. // 将库存信息加入
  279. state.goodsInfo.skus.forEach(item => {
  280. if (item.id == setList.skuId) {
  281. setList.stock = item.stock
  282. }
  283. })
  284. return setList
  285. }
  286. onLoad((options) => {
  287. // 非法参数
  288. if (!options.id) {
  289. state.goodsInfo = null;
  290. return;
  291. }
  292. state.goodsId = options.id;
  293. // 1. 加载商品信息
  294. SpuApi.getSpuDetail(state.goodsId).then((res) => {
  295. // 未找到商品
  296. if (res.code !== 0 || !res.data) {
  297. state.goodsInfo = null;
  298. return;
  299. }
  300. // 加载到商品
  301. state.skeletonLoading = false;
  302. state.goodsInfo = res.data;
  303. // 加载是否收藏
  304. if (isLogin.value) {
  305. FavoriteApi.isFavoriteExists(state.goodsId, 'goods').then((res) => {
  306. if (res.code !== 0) {
  307. return;
  308. }
  309. state.goodsInfo.favorite = res.data;
  310. });
  311. }
  312. });
  313. // 2. 加载优惠劵信息
  314. getCoupon();
  315. // 3. 加载营销活动信息
  316. ActivityApi.getActivityListBySpuId(state.goodsId).then((res) => {
  317. if (res.code !== 0) {
  318. return;
  319. }
  320. let activData = handList(res.data)
  321. activData.forEach(activity => {
  322. if ([1, 2, 3].includes(activity.type)) { // 情况一:拼团/秒杀/砍价
  323. state.activityList.push(activity);
  324. } else if (activity.type === 5) { // 情况二:满减送
  325. state.activityInfo.push(activity);
  326. } else { // 情况三:限时折扣 TODO 芋艿
  327. // console.log('待实现!优先级不高');
  328. }
  329. })
  330. });
  331. //获取结算信息
  332. getSettlementByIds(state.goodsId)
  333. });
  334. </script>
  335. <style lang="scss" scoped>
  336. .detail-card {
  337. background-color: #ffff;
  338. margin: 14rpx 20rpx;
  339. border-radius: 10rpx;
  340. overflow: hidden;
  341. }
  342. // 价格标题卡片
  343. .title-card {
  344. .price-box {
  345. .price-text {
  346. font-size: 42rpx;
  347. font-weight: 500;
  348. color: #ff3000;
  349. line-height: 30rpx;
  350. font-family: OPPOSANS;
  351. &::before {
  352. content: '¥';
  353. font-size: 30rpx;
  354. }
  355. }
  356. .origin-price-text {
  357. font-size: 26rpx;
  358. font-weight: 400;
  359. text-decoration: line-through;
  360. color: $gray-c;
  361. font-family: OPPOSANS;
  362. &::before {
  363. content: '¥';
  364. }
  365. }
  366. }
  367. .sales-text {
  368. font-size: 26rpx;
  369. font-weight: 500;
  370. color: $gray-c;
  371. }
  372. .discounts-box {
  373. .tag-content {
  374. flex: 1;
  375. min-width: 0;
  376. white-space: nowrap;
  377. }
  378. .tag-box {
  379. overflow: hidden;
  380. text-overflow: ellipsis;
  381. }
  382. .tag {
  383. flex-shrink: 0;
  384. padding: 4rpx 10rpx;
  385. font-size: 24rpx;
  386. font-weight: 500;
  387. border-radius: 4rpx;
  388. color: var(--ui-BG-Main);
  389. background: var(--ui-BG-Main-tag);
  390. }
  391. .discounts-title {
  392. font-size: 24rpx;
  393. font-weight: 500;
  394. color: var(--ui-BG-Main);
  395. line-height: normal;
  396. }
  397. .cicon-forward {
  398. color: var(--ui-BG-Main);
  399. font-size: 24rpx;
  400. line-height: normal;
  401. margin-top: 4rpx;
  402. }
  403. }
  404. .title-text {
  405. font-size: 30rpx;
  406. font-weight: bold;
  407. line-height: 42rpx;
  408. }
  409. .subtitle-text {
  410. font-size: 26rpx;
  411. font-weight: 400;
  412. color: $dark-9;
  413. line-height: 42rpx;
  414. }
  415. }
  416. // 购买
  417. .buy-box {
  418. .add-btn {
  419. width: 214rpx;
  420. height: 72rpx;
  421. font-weight: 500;
  422. font-size: 28rpx;
  423. border-radius: 40rpx 0 0 40rpx;
  424. background-color: var(--ui-BG-Main-light);
  425. color: var(--ui-BG-Main);
  426. }
  427. .buy-btn {
  428. width: 214rpx;
  429. height: 72rpx;
  430. font-weight: 500;
  431. font-size: 28rpx;
  432. border-radius: 0 40rpx 40rpx 0;
  433. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  434. color: $white;
  435. }
  436. .disabled-btn {
  437. width: 428rpx;
  438. height: 72rpx;
  439. border-radius: 40rpx;
  440. background: #999999;
  441. color: $white;
  442. }
  443. }
  444. .model-box {
  445. height: 60vh;
  446. .model-content {
  447. height: 56vh;
  448. }
  449. .title {
  450. font-size: 36rpx;
  451. font-weight: bold;
  452. color: #333333;
  453. }
  454. .subtitle {
  455. font-size: 26rpx;
  456. font-weight: 500;
  457. color: #333333;
  458. }
  459. }
  460. // 限时折扣
  461. .discount {
  462. width: 750rpx;
  463. height: 100rpx;
  464. // background-color: red;
  465. overflow: hidden;
  466. position: relative;
  467. }
  468. .disImg {
  469. width: 750rpx;
  470. height: 100rpx;
  471. position: absolute;
  472. top: 0;
  473. z-index: -1;
  474. }
  475. .discountCont {
  476. width: 680rpx;
  477. height: 90rpx;
  478. margin: 10rpx auto 0 auto;
  479. // background-color: gold;
  480. }
  481. .disContT {
  482. width: 680rpx;
  483. height: 50rpx;
  484. display: flex;
  485. justify-content: space-between;
  486. }
  487. .disContT1 {
  488. width: 400rpx;
  489. height: 50rpx;
  490. // background-color: green;
  491. display: flex;
  492. justify-content: flex-start;
  493. align-items: center;
  494. }
  495. .disContT2 {
  496. width: 200rpx;
  497. height: 50rpx;
  498. line-height: 50rpx;
  499. // background-color: gold;
  500. font-size: 30rpx;
  501. text-align: end;
  502. color: white;
  503. font-weight: bolder;
  504. font-style: oblique 20deg;
  505. letter-spacing: .1rem;
  506. }
  507. .disContT1P {
  508. color: white;
  509. font-weight: bold;
  510. font-size: 28rpx;
  511. }
  512. .disContT1End {
  513. // width: 180rpx;
  514. padding: 0 10rpx;
  515. height: 30rpx;
  516. line-height: 28rpx;
  517. text-align: center;
  518. font-weight: bold;
  519. background-color: white;
  520. color: #ff3000;
  521. font-size: 23rpx;
  522. border-radius: 20rpx;
  523. margin-left: 10rpx;
  524. }
  525. .disContB {
  526. width: 680rpx;
  527. height: 40rpx;
  528. display: flex;
  529. justify-content: space-between;
  530. font-size: 20rpx;
  531. color: white;
  532. align-items: center;
  533. }
  534. .disContB1 {
  535. width: 300rpx;
  536. height: 40rpx;
  537. line-height: 40rpx;
  538. }
  539. .disContB2 {
  540. width: 300rpx;
  541. height: 40rpx;
  542. line-height: 40rpx;
  543. display: flex;
  544. justify-content: flex-end;
  545. }
  546. </style>