index.vue 18 KB

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