index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 营销活动的列表 TODO 芋艿:正在接入中
  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. categoryId: state.goodsInfo.categoryId,
  194. },
  195. ],
  196. }),
  197. });
  198. }
  199. // 营销活动
  200. function onActivity() {
  201. state.showActivityModel = true;
  202. }
  203. // 立即领取
  204. async function onGet(id) {
  205. const { code } = await CouponApi.takeCoupon(id);
  206. if (code !== 0) {
  207. return;
  208. }
  209. uni.showToast({
  210. title: '领取成功',
  211. });
  212. setTimeout(() => {
  213. getCoupon();
  214. }, 1000);
  215. }
  216. const shareInfo = computed(() => {
  217. if (isEmpty(state.goodsInfo)) return {};
  218. return sheep.$platform.share.getShareInfo(
  219. {
  220. title: state.goodsInfo.name,
  221. image: sheep.$url.cdn(state.goodsInfo.picUrl),
  222. desc: state.goodsInfo.introduction,
  223. params: {
  224. page: '2',
  225. query: state.goodsInfo.id,
  226. },
  227. },
  228. {
  229. type: 'goods', // 商品海报
  230. title: state.goodsInfo.name, // 商品名称
  231. image: sheep.$url.cdn(state.goodsInfo.picUrl), // 商品主图
  232. price: fen2yuan(state.goodsInfo.price), // 商品价格
  233. original_price: fen2yuan(state.goodsInfo.marketPrice), // 商品原价
  234. },
  235. );
  236. });
  237. async function getCoupon() {
  238. const { code, data } = await CouponApi.getCouponTemplateList(state.goodsId, 2, 10);
  239. if (code === 0) {
  240. state.couponInfo = data;
  241. }
  242. }
  243. onLoad((options) => {
  244. // 非法参数
  245. if (!options.id) {
  246. state.goodsInfo = null;
  247. return;
  248. }
  249. state.goodsId = options.id;
  250. // 1. 加载商品信息
  251. SpuApi.getSpuDetail(state.goodsId).then((res) => {
  252. // 未找到商品
  253. if (res.code !== 0 || !res.data) {
  254. state.goodsInfo = null;
  255. return;
  256. }
  257. // 加载到商品
  258. state.skeletonLoading = false;
  259. state.goodsInfo = res.data;
  260. // 加载是否收藏
  261. if (isLogin.value) {
  262. FavoriteApi.isFavoriteExists(state.goodsId, 'goods').then((res) => {
  263. if (res.code !== 0) {
  264. return;
  265. }
  266. state.goodsInfo.favorite = res.data;
  267. });
  268. }
  269. });
  270. // 2. 加载优惠劵信息
  271. getCoupon();
  272. // 3. 获得单个商品,进行中的拼团、秒杀、砍价活动信息
  273. ActivityApi.getActivityListBySpuId(state.goodsId).then((res) => {
  274. if (res.code !== 0) {
  275. return;
  276. }
  277. state.activityList = res.data;
  278. });
  279. });
  280. </script>
  281. <style lang="scss" scoped>
  282. .detail-card {
  283. background-color: #ffff;
  284. margin: 14rpx 20rpx;
  285. border-radius: 10rpx;
  286. overflow: hidden;
  287. }
  288. // 价格标题卡片
  289. .title-card {
  290. .price-box {
  291. .price-text {
  292. font-size: 42rpx;
  293. font-weight: 500;
  294. color: #ff3000;
  295. line-height: 30rpx;
  296. font-family: OPPOSANS;
  297. &::before {
  298. content: '¥';
  299. font-size: 30rpx;
  300. }
  301. }
  302. .origin-price-text {
  303. font-size: 26rpx;
  304. font-weight: 400;
  305. text-decoration: line-through;
  306. color: $gray-c;
  307. font-family: OPPOSANS;
  308. &::before {
  309. content: '¥';
  310. }
  311. }
  312. }
  313. .sales-text {
  314. font-size: 26rpx;
  315. font-weight: 500;
  316. color: $gray-c;
  317. }
  318. .discounts-box {
  319. .tag-content {
  320. flex: 1;
  321. min-width: 0;
  322. white-space: nowrap;
  323. }
  324. .tag-box {
  325. overflow: hidden;
  326. text-overflow: ellipsis;
  327. }
  328. .tag {
  329. flex-shrink: 0;
  330. padding: 4rpx 10rpx;
  331. font-size: 24rpx;
  332. font-weight: 500;
  333. border-radius: 4rpx;
  334. color: var(--ui-BG-Main);
  335. background: var(--ui-BG-Main-tag);
  336. }
  337. .discounts-title {
  338. font-size: 24rpx;
  339. font-weight: 500;
  340. color: var(--ui-BG-Main);
  341. line-height: normal;
  342. }
  343. .cicon-forward {
  344. color: var(--ui-BG-Main);
  345. font-size: 24rpx;
  346. line-height: normal;
  347. margin-top: 4rpx;
  348. }
  349. }
  350. .title-text {
  351. font-size: 30rpx;
  352. font-weight: bold;
  353. line-height: 42rpx;
  354. }
  355. .subtitle-text {
  356. font-size: 26rpx;
  357. font-weight: 400;
  358. color: $dark-9;
  359. line-height: 42rpx;
  360. }
  361. }
  362. // 购买
  363. .buy-box {
  364. .add-btn {
  365. width: 214rpx;
  366. height: 72rpx;
  367. font-weight: 500;
  368. font-size: 28rpx;
  369. border-radius: 40rpx 0 0 40rpx;
  370. background-color: var(--ui-BG-Main-light);
  371. color: var(--ui-BG-Main);
  372. }
  373. .buy-btn {
  374. width: 214rpx;
  375. height: 72rpx;
  376. font-weight: 500;
  377. font-size: 28rpx;
  378. border-radius: 0 40rpx 40rpx 0;
  379. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  380. color: $white;
  381. }
  382. .disabled-btn {
  383. width: 428rpx;
  384. height: 72rpx;
  385. border-radius: 40rpx;
  386. background: #999999;
  387. color: $white;
  388. }
  389. }
  390. .model-box {
  391. height: 60vh;
  392. .model-content {
  393. height: 56vh;
  394. }
  395. .title {
  396. font-size: 36rpx;
  397. font-weight: bold;
  398. color: #333333;
  399. }
  400. .subtitle {
  401. font-size: 26rpx;
  402. font-weight: 500;
  403. color: #333333;
  404. }
  405. }
  406. </style>