index.vue 19 KB

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