index.vue 19 KB

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