index.vue 16 KB

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