index.vue 11 KB

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