index.vue 20 KB

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