index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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="title-card detail-card ss-p-y-40 ss-p-x-20">
  31. <view class="ss-flex ss-row-between ss-col-center ss-m-b-26">
  32. <view class="price-box ss-flex ss-col-bottom">
  33. <view class="price-text ss-m-r-16">
  34. {{ fen2yuan(state.selectedSku.price || state.goodsInfo.price) }}
  35. </view>
  36. <view class="origin-price-text" v-if="state.goodsInfo.marketPrice > 0">
  37. {{ fen2yuan(state.selectedSku.marketPrice || state.goodsInfo.marketPrice) }}
  38. </view>
  39. </view>
  40. <view class="sales-text">
  41. {{ formatSales('exact', state.goodsInfo.salesCount) }}
  42. </view>
  43. </view>
  44. <view class="discounts-box ss-flex ss-row-between ss-m-b-28">
  45. <!-- 满减送/限时折扣活动的提示 -->
  46. <div class="tag-content">
  47. <view class="tag-box ss-flex">
  48. <view
  49. class="tag ss-m-r-10"
  50. v-for="promos in state.activityInfo"
  51. :key="promos.id"
  52. @tap="onActivity"
  53. >
  54. {{ promos.name }}
  55. </view>
  56. </view>
  57. </div>
  58. <!-- 优惠劵 -->
  59. <view
  60. class="get-coupon-box ss-flex ss-col-center ss-m-l-20"
  61. @tap="state.showModel = true"
  62. v-if="state.couponInfo.length"
  63. >
  64. <view class="discounts-title ss-m-r-8">领券</view>
  65. <text class="cicon-forward"></text>
  66. </view>
  67. </view>
  68. <view class="title-text ss-line-2 ss-m-b-6">{{ state.goodsInfo.name }}</view>
  69. <view class="subtitle-text ss-line-1">{{ state.goodsInfo.introduction }}</view>
  70. </view>
  71. <!-- 功能卡片 -->
  72. <view class="detail-cell-card detail-card ss-flex-col">
  73. <detail-cell-sku
  74. v-model="state.selectedSku.goods_sku_text"
  75. :sku="state.selectedSku"
  76. @tap="state.showSelectSku = true"
  77. />
  78. </view>
  79. <!-- 规格与数量弹框 -->
  80. <s-select-sku
  81. :goodsInfo="state.goodsInfo"
  82. :show="state.showSelectSku"
  83. @addCart="onAddCart"
  84. @buy="onBuy"
  85. @change="onSkuChange"
  86. @close="state.showSelectSku = false"
  87. />
  88. </view>
  89. <!-- 评价 -->
  90. <detail-comment-card class="detail-comment-selector" :goodsId="state.goodsId" />
  91. <!-- 详情 -->
  92. <detail-content-card
  93. class="detail-content-selector"
  94. :content="state.goodsInfo.description"
  95. />
  96. <!-- 活动跳转:拼团/秒杀/砍价活动 -->
  97. <detail-activity-tip
  98. v-if="state.activityList.length > 0"
  99. :activity-list="state.activityList"
  100. />
  101. <!-- 详情 tabbar -->
  102. <detail-tabbar v-model="state.goodsInfo">
  103. <view class="buy-box ss-flex ss-col-center ss-p-r-20" v-if="state.goodsInfo.stock > 0">
  104. <button
  105. class="ss-reset-button add-btn ui-Shadow-Main"
  106. @tap="state.showSelectSku = true"
  107. >
  108. 加入购物车
  109. </button>
  110. <button
  111. class="ss-reset-button buy-btn ui-Shadow-Main"
  112. @tap="state.showSelectSku = true"
  113. >
  114. 立即购买
  115. </button>
  116. </view>
  117. <view class="buy-box ss-flex ss-col-center ss-p-r-20" v-else>
  118. <button class="ss-reset-button disabled-btn" disabled> 已售罄 </button>
  119. </view>
  120. </detail-tabbar>
  121. <!-- 优惠劵弹窗 -->
  122. <s-coupon-get
  123. v-model="state.couponInfo"
  124. :show="state.showModel"
  125. @close="state.showModel = false"
  126. @get="onGet"
  127. />
  128. <!-- 满减送/限时折扣活动弹窗 -->
  129. <s-activity-pop
  130. v-model="state.activityInfo"
  131. :show="state.showActivityModel"
  132. @close="state.showActivityModel = false"
  133. />
  134. </block>
  135. </s-layout>
  136. </view>
  137. </template>
  138. <script setup>
  139. import { reactive, computed } from 'vue';
  140. import { onLoad, onPageScroll } from '@dcloudio/uni-app';
  141. import sheep from '@/sheep';
  142. import CouponApi from '@/sheep/api/promotion/coupon';
  143. import ActivityApi from '@/sheep/api/promotion/activity';
  144. import FavoriteApi from '@/sheep/api/product/favorite';
  145. import { formatSales, formatGoodsSwiper, fen2yuan } from '@/sheep/hooks/useGoods';
  146. import detailNavbar from './components/detail/detail-navbar.vue';
  147. import detailCellSku from './components/detail/detail-cell-sku.vue';
  148. import detailTabbar from './components/detail/detail-tabbar.vue';
  149. import detailSkeleton from './components/detail/detail-skeleton.vue';
  150. import detailCommentCard from './components/detail/detail-comment-card.vue';
  151. import detailContentCard from './components/detail/detail-content-card.vue';
  152. import detailActivityTip from './components/detail/detail-activity-tip.vue';
  153. import { isEmpty } from 'lodash-es';
  154. import SpuApi from '@/sheep/api/product/spu';
  155. onPageScroll(() => {});
  156. const isLogin = computed(() => sheep.$store('user').isLogin);
  157. const state = reactive({
  158. goodsId: 0,
  159. skeletonLoading: true, // SPU 加载中
  160. goodsInfo: {}, // SPU 信息
  161. showSelectSku: false, // 是否展示 SKU 选择弹窗
  162. selectedSku: {}, // 选中的 SKU
  163. showModel: false, // 是否展示 Coupon 优惠劵的弹窗
  164. couponInfo: [], // 可领取的 Coupon 优惠劵的列表
  165. showActivityModel: false, // 【满减送/限时折扣】是否展示 Activity 营销活动的弹窗
  166. activityInfo: [], // 【满减送/限时折扣】可参与的 Activity 营销活动的列表
  167. activityList: [], // 【秒杀/拼团/砍价】可参与的 Activity 营销活动的列表
  168. });
  169. // 规格变更
  170. function onSkuChange(e) {
  171. state.selectedSku = e;
  172. }
  173. // 添加购物车
  174. function onAddCart(e) {
  175. if (!e.id) {
  176. sheep.$helper.toast('请选择商品规格');
  177. return;
  178. }
  179. sheep.$store('cart').add(e);
  180. }
  181. // 立即购买
  182. function onBuy(e) {
  183. if (!state.selectedSku.id) {
  184. sheep.$helper.toast('请选择商品规格');
  185. return;
  186. }
  187. sheep.$router.go('/pages/order/confirm', {
  188. data: JSON.stringify({
  189. items: [
  190. {
  191. skuId: e.id,
  192. count: e.goods_num,
  193. },
  194. ],
  195. // TODO 芋艿:后续清理掉这 2 参数
  196. deliveryType: 1,
  197. pointStatus: false,
  198. }),
  199. });
  200. }
  201. // 营销活动
  202. function onActivity() {
  203. state.showActivityModel = true;
  204. }
  205. // 立即领取
  206. async function onGet(id) {
  207. const { code } = await CouponApi.takeCoupon(id);
  208. if (code !== 0) {
  209. return;
  210. }
  211. uni.showToast({
  212. title: '领取成功',
  213. });
  214. setTimeout(() => {
  215. getCoupon();
  216. }, 1000);
  217. }
  218. // TODO 芋艿:待测试
  219. const shareInfo = computed(() => {
  220. if (isEmpty(state.goodsInfo)) return {};
  221. return sheep.$platform.share.getShareInfo(
  222. {
  223. title: state.goodsInfo.name,
  224. image: sheep.$url.cdn(state.goodsInfo.picUrl),
  225. desc: state.goodsInfo.introduction,
  226. params: {
  227. page: '2',
  228. query: state.goodsInfo.id,
  229. },
  230. },
  231. {
  232. type: 'goods', // 商品海报
  233. title: state.goodsInfo.name, // 商品名称
  234. image: sheep.$url.cdn(state.goodsInfo.picUrl), // 商品主图
  235. price: fen2yuan(state.goodsInfo.price), // 商品价格
  236. original_price: fen2yuan(state.goodsInfo.marketPrice), // 商品原价
  237. },
  238. );
  239. });
  240. async function getCoupon() {
  241. const { code, data } = await CouponApi.getCouponTemplateList(state.goodsId, 2, 10);
  242. if (code === 0) {
  243. state.couponInfo = data;
  244. }
  245. }
  246. onLoad((options) => {
  247. // 非法参数
  248. if (!options.id) {
  249. state.goodsInfo = null;
  250. return;
  251. }
  252. state.goodsId = options.id;
  253. // 1. 加载商品信息
  254. SpuApi.getSpuDetail(state.goodsId).then((res) => {
  255. // 未找到商品
  256. if (res.code !== 0 || !res.data) {
  257. state.goodsInfo = null;
  258. return;
  259. }
  260. // 加载到商品
  261. state.skeletonLoading = false;
  262. state.goodsInfo = res.data;
  263. // 加载是否收藏
  264. if (isLogin.value) {
  265. FavoriteApi.isFavoriteExists(state.goodsId, 'goods').then((res) => {
  266. if (res.code !== 0) {
  267. return;
  268. }
  269. state.goodsInfo.favorite = res.data;
  270. });
  271. }
  272. });
  273. // 2. 加载优惠劵信息
  274. getCoupon();
  275. // 3. 加载营销活动信息
  276. ActivityApi.getActivityListBySpuId(state.goodsId).then((res) => {
  277. if (res.code !== 0) {
  278. return;
  279. }
  280. res.data.forEach((activity) => {
  281. if ([1, 2, 3].includes(activity.type)) {
  282. // 情况一:拼团/秒杀/砍价
  283. state.activityList.push(activity);
  284. } else if (activity.type === 5) {
  285. // 情况二:满减送
  286. state.activityInfo.push(activity);
  287. } else {
  288. // 情况三:限时折扣 TODO 芋艿
  289. console.log('待实现!优先级不高');
  290. }
  291. });
  292. });
  293. });
  294. </script>
  295. <style lang="scss" scoped>
  296. .detail-card {
  297. background-color: #ffff;
  298. margin: 14rpx 20rpx;
  299. border-radius: 10rpx;
  300. overflow: hidden;
  301. }
  302. // 价格标题卡片
  303. .title-card {
  304. .price-box {
  305. .price-text {
  306. font-size: 42rpx;
  307. font-weight: 500;
  308. color: #ff3000;
  309. line-height: 30rpx;
  310. font-family: OPPOSANS;
  311. &::before {
  312. content: '¥';
  313. font-size: 30rpx;
  314. }
  315. }
  316. .origin-price-text {
  317. font-size: 26rpx;
  318. font-weight: 400;
  319. text-decoration: line-through;
  320. color: $gray-c;
  321. font-family: OPPOSANS;
  322. &::before {
  323. content: '¥';
  324. }
  325. }
  326. }
  327. .sales-text {
  328. font-size: 26rpx;
  329. font-weight: 500;
  330. color: $gray-c;
  331. }
  332. .discounts-box {
  333. .tag-content {
  334. flex: 1;
  335. min-width: 0;
  336. white-space: nowrap;
  337. }
  338. .tag-box {
  339. overflow: hidden;
  340. text-overflow: ellipsis;
  341. }
  342. .tag {
  343. flex-shrink: 0;
  344. padding: 4rpx 10rpx;
  345. font-size: 24rpx;
  346. font-weight: 500;
  347. border-radius: 4rpx;
  348. color: var(--ui-BG-Main);
  349. background: var(--ui-BG-Main-tag);
  350. }
  351. .discounts-title {
  352. font-size: 24rpx;
  353. font-weight: 500;
  354. color: var(--ui-BG-Main);
  355. line-height: normal;
  356. }
  357. .cicon-forward {
  358. color: var(--ui-BG-Main);
  359. font-size: 24rpx;
  360. line-height: normal;
  361. margin-top: 4rpx;
  362. }
  363. }
  364. .title-text {
  365. font-size: 30rpx;
  366. font-weight: bold;
  367. line-height: 42rpx;
  368. }
  369. .subtitle-text {
  370. font-size: 26rpx;
  371. font-weight: 400;
  372. color: $dark-9;
  373. line-height: 42rpx;
  374. }
  375. }
  376. // 购买
  377. .buy-box {
  378. .add-btn {
  379. width: 214rpx;
  380. height: 72rpx;
  381. font-weight: 500;
  382. font-size: 28rpx;
  383. border-radius: 40rpx 0 0 40rpx;
  384. background-color: var(--ui-BG-Main-light);
  385. color: var(--ui-BG-Main);
  386. }
  387. .buy-btn {
  388. width: 214rpx;
  389. height: 72rpx;
  390. font-weight: 500;
  391. font-size: 28rpx;
  392. border-radius: 0 40rpx 40rpx 0;
  393. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  394. color: $white;
  395. }
  396. .disabled-btn {
  397. width: 428rpx;
  398. height: 72rpx;
  399. border-radius: 40rpx;
  400. background: #999999;
  401. color: $white;
  402. }
  403. }
  404. .model-box {
  405. height: 60vh;
  406. .model-content {
  407. height: 56vh;
  408. }
  409. .title {
  410. font-size: 36rpx;
  411. font-weight: bold;
  412. color: #333333;
  413. }
  414. .subtitle {
  415. font-size: 26rpx;
  416. font-weight: 500;
  417. color: #333333;
  418. }
  419. }
  420. </style>