index.vue 11 KB

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