index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. import OrderApi from '@/sheep/api/trade/order';
  212. const bgColor = {
  213. bgColor: '#E93323',
  214. Color: '#fff',
  215. width: '44rpx',
  216. timeTxtwidth: '16rpx',
  217. isDay: true,
  218. };
  219. const isLogin = computed(() => sheep.$store('user').isLogin);
  220. const state = reactive({
  221. goodsId: 0,
  222. skeletonLoading: true, // SPU 加载中
  223. goodsInfo: {}, // SPU 信息
  224. showSelectSku: false, // 是否展示 SKU 选择弹窗
  225. selectedSku: {}, // 选中的 SKU
  226. settlementSku: {}, // 结算的 SKU:由于 selectedSku 不进行默认选中,所以初始使用结算价格最低的 SKU 作为基础展示
  227. showModel: false, // 是否展示 Coupon 优惠劵的弹窗
  228. couponInfo: [], // 可领取的 Coupon 优惠劵的列表
  229. showActivityModel: false, // 【满减送/限时折扣】是否展示 Activity 营销活动的弹窗
  230. rewardActivity: {}, // 【满减送】活动
  231. activityList: [], // 【秒杀/拼团/砍价】可参与的 Activity 营销活动的列表
  232. });
  233. // 规格变更
  234. function onSkuChange(e) {
  235. state.selectedSku = e;
  236. state.settlementSku = e;
  237. }
  238. // 添加购物车
  239. function onAddCart(e) {
  240. if (!e.id) {
  241. sheep.$helper.toast('请选择商品规格');
  242. return;
  243. }
  244. sheep.$store('cart').add(e);
  245. }
  246. // 立即购买
  247. function onBuy(e) {
  248. if (!e.id) {
  249. sheep.$helper.toast('请选择商品规格');
  250. return;
  251. }
  252. sheep.$router.go('/pages/order/confirm', {
  253. data: JSON.stringify({
  254. items: [
  255. {
  256. skuId: e.id,
  257. count: e.goods_num,
  258. categoryId: state.goodsInfo.categoryId,
  259. },
  260. ],
  261. }),
  262. });
  263. }
  264. // 打开营销弹窗
  265. function onOpenActivity() {
  266. state.showActivityModel = true;
  267. }
  268. // 立即领取优惠劵
  269. async function onTakeCoupon(id) {
  270. const { code } = await CouponApi.takeCoupon(id);
  271. if (code !== 0) {
  272. return;
  273. }
  274. uni.showToast({
  275. title: '领取成功',
  276. });
  277. setTimeout(() => {
  278. getCoupon();
  279. }, 1000);
  280. }
  281. const shareInfo = computed(() => {
  282. if (isEmpty(state.goodsInfo)) return {};
  283. return sheep.$platform.share.getShareInfo(
  284. {
  285. title: state.goodsInfo.name,
  286. image: sheep.$url.cdn(state.goodsInfo.picUrl),
  287. desc: state.goodsInfo.introduction,
  288. params: {
  289. page: '2',
  290. query: state.goodsInfo.id,
  291. },
  292. },
  293. {
  294. type: 'goods', // 商品海报
  295. title: state.goodsInfo.name, // 商品名称
  296. image: sheep.$url.cdn(state.goodsInfo.picUrl), // 商品主图
  297. price: fen2yuan(state.goodsInfo.price), // 商品价格
  298. original_price: fen2yuan(state.goodsInfo.marketPrice), // 商品原价
  299. },
  300. );
  301. });
  302. async function getCoupon() {
  303. const { code, data } = await CouponApi.getCouponTemplateList(state.goodsId, 2, 10);
  304. if (code === 0) {
  305. state.couponInfo = data;
  306. }
  307. }
  308. async function getSettlementByIds(ids) {
  309. let { data, code } = await OrderApi.getSettlementProduct(ids);
  310. if (code !== 0 || data.length !== 1) {
  311. return;
  312. }
  313. data = data[0];
  314. // 补充 SKU 的价格信息
  315. state.goodsInfo.skus.forEach((sku) => {
  316. data.skus.forEach((item) => {
  317. if (sku.id === item.id) {
  318. sku.promotionType = item.promotionType;
  319. sku.promotionPrice = item.promotionPrice;
  320. sku.promotionId = item.promotionId;
  321. sku.promotionEndTime = item.promotionEndTime;
  322. }
  323. });
  324. });
  325. // 选择有 promotionPrice 且最小的
  326. state.settlementSku = state.goodsInfo.skus
  327. .filter((sku) => sku.stock > 0 && sku.promotionPrice > 0)
  328. .reduce((prev, curr) => (prev.promotionPrice < curr.promotionPrice ? prev : curr));
  329. // 设置满减送活动
  330. if (data.rewardActivity) {
  331. state.rewardActivity = data.rewardActivity;
  332. }
  333. }
  334. onLoad((options) => {
  335. // 非法参数
  336. if (!options.id) {
  337. state.goodsInfo = null;
  338. return;
  339. }
  340. state.goodsId = options.id;
  341. // 1. 加载商品信息
  342. SpuApi.getSpuDetail(state.goodsId).then((res) => {
  343. // 未找到商品
  344. if (res.code !== 0 || !res.data) {
  345. state.goodsInfo = null;
  346. return;
  347. }
  348. // 加载到商品
  349. state.skeletonLoading = false;
  350. state.goodsInfo = res.data;
  351. // 加载是否收藏
  352. if (isLogin.value) {
  353. FavoriteApi.isFavoriteExists(state.goodsId, 'goods').then((res) => {
  354. if (res.code !== 0) {
  355. return;
  356. }
  357. state.goodsInfo.favorite = res.data;
  358. });
  359. }
  360. });
  361. // 2. 加载优惠劵信息
  362. getCoupon();
  363. // 3. 加载营销活动信息
  364. ActivityApi.getActivityListBySpuId(state.goodsId).then((res) => {
  365. if (res.code !== 0) {
  366. return;
  367. }
  368. state.activityList = res.data;
  369. });
  370. //获取结算信息
  371. getSettlementByIds(state.goodsId);
  372. });
  373. </script>
  374. <style lang="scss" scoped>
  375. .detail-card {
  376. background-color: #ffff;
  377. margin: 14rpx 20rpx;
  378. border-radius: 10rpx;
  379. overflow: hidden;
  380. }
  381. // 价格标题卡片
  382. .title-card {
  383. .price-box {
  384. .price-text {
  385. font-size: 42rpx;
  386. font-weight: 500;
  387. color: #ff3000;
  388. line-height: 30rpx;
  389. font-family: OPPOSANS;
  390. &::before {
  391. content: '¥';
  392. font-size: 30rpx;
  393. }
  394. }
  395. .origin-price-text {
  396. font-size: 26rpx;
  397. font-weight: 400;
  398. text-decoration: line-through;
  399. color: $gray-c;
  400. font-family: OPPOSANS;
  401. &::before {
  402. content: '¥';
  403. }
  404. }
  405. }
  406. .sales-text {
  407. font-size: 26rpx;
  408. font-weight: 500;
  409. color: $gray-c;
  410. }
  411. .discounts-box {
  412. .tag-content {
  413. flex: 1;
  414. min-width: 0;
  415. white-space: nowrap;
  416. }
  417. .tag-box {
  418. overflow: hidden;
  419. text-overflow: ellipsis;
  420. }
  421. .tag {
  422. flex-shrink: 0;
  423. padding: 4rpx 10rpx;
  424. font-size: 24rpx;
  425. font-weight: 500;
  426. border-radius: 4rpx;
  427. color: var(--ui-BG-Main);
  428. background: var(--ui-BG-Main-tag);
  429. }
  430. .discounts-title {
  431. font-size: 24rpx;
  432. font-weight: 500;
  433. color: var(--ui-BG-Main);
  434. line-height: normal;
  435. }
  436. .cicon-forward {
  437. color: var(--ui-BG-Main);
  438. font-size: 24rpx;
  439. line-height: normal;
  440. margin-top: 4rpx;
  441. }
  442. }
  443. .title-text {
  444. font-size: 30rpx;
  445. font-weight: bold;
  446. line-height: 42rpx;
  447. }
  448. .subtitle-text {
  449. font-size: 26rpx;
  450. font-weight: 400;
  451. color: $dark-9;
  452. line-height: 42rpx;
  453. }
  454. }
  455. // 购买
  456. .buy-box {
  457. .add-btn {
  458. width: 214rpx;
  459. height: 72rpx;
  460. font-weight: 500;
  461. font-size: 28rpx;
  462. border-radius: 40rpx 0 0 40rpx;
  463. background-color: var(--ui-BG-Main-light);
  464. color: var(--ui-BG-Main);
  465. }
  466. .buy-btn {
  467. width: 214rpx;
  468. height: 72rpx;
  469. font-weight: 500;
  470. font-size: 28rpx;
  471. border-radius: 0 40rpx 40rpx 0;
  472. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  473. color: $white;
  474. }
  475. .disabled-btn {
  476. width: 428rpx;
  477. height: 72rpx;
  478. border-radius: 40rpx;
  479. background: #999999;
  480. color: $white;
  481. }
  482. }
  483. .model-box {
  484. height: 60vh;
  485. .model-content {
  486. height: 56vh;
  487. }
  488. .title {
  489. font-size: 36rpx;
  490. font-weight: bold;
  491. color: #333333;
  492. }
  493. .subtitle {
  494. font-size: 26rpx;
  495. font-weight: 500;
  496. color: #333333;
  497. }
  498. }
  499. // 限时折扣
  500. .discount {
  501. width: 750rpx;
  502. height: 100rpx;
  503. // background-color: red;
  504. overflow: hidden;
  505. position: relative;
  506. }
  507. .disImg {
  508. width: 750rpx;
  509. height: 100rpx;
  510. position: absolute;
  511. top: 0;
  512. z-index: -1;
  513. }
  514. .discountCont {
  515. width: 680rpx;
  516. height: 90rpx;
  517. margin: 10rpx auto 0 auto;
  518. // background-color: gold;
  519. }
  520. .disContT {
  521. width: 680rpx;
  522. height: 50rpx;
  523. display: flex;
  524. justify-content: space-between;
  525. }
  526. .disContT1 {
  527. width: 400rpx;
  528. height: 50rpx;
  529. // background-color: green;
  530. display: flex;
  531. justify-content: flex-start;
  532. align-items: center;
  533. }
  534. .disContT2 {
  535. width: 200rpx;
  536. height: 50rpx;
  537. line-height: 50rpx;
  538. // background-color: gold;
  539. font-size: 30rpx;
  540. text-align: end;
  541. color: white;
  542. font-weight: bolder;
  543. font-style: oblique 20deg;
  544. letter-spacing: 0.1rem;
  545. }
  546. .disContT1P {
  547. color: white;
  548. font-weight: bold;
  549. font-size: 28rpx;
  550. }
  551. .disContT1End {
  552. // width: 180rpx;
  553. padding: 0 10rpx;
  554. height: 30rpx;
  555. line-height: 28rpx;
  556. text-align: center;
  557. font-weight: bold;
  558. background-color: white;
  559. color: #ff3000;
  560. font-size: 23rpx;
  561. border-radius: 20rpx;
  562. margin-left: 10rpx;
  563. }
  564. .disContB {
  565. width: 680rpx;
  566. height: 40rpx;
  567. display: flex;
  568. justify-content: space-between;
  569. font-size: 20rpx;
  570. color: white;
  571. align-items: center;
  572. }
  573. .disContB1 {
  574. width: 300rpx;
  575. height: 40rpx;
  576. line-height: 40rpx;
  577. }
  578. .disContB2 {
  579. width: 300rpx;
  580. height: 40rpx;
  581. line-height: 40rpx;
  582. display: flex;
  583. justify-content: flex-end;
  584. }
  585. </style>