index.vue 17 KB

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