index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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-es';
  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. categoryId: state.goodsInfo.categoryId
  145. }]
  146. }),
  147. });
  148. }
  149. // 营销活动
  150. function onActivity() {
  151. state.showActivityModel = true;
  152. }
  153. // 立即领取
  154. async function onGet(id) {
  155. const { code } = await CouponApi.takeCoupon(id);
  156. if (code !== 0) {
  157. return;
  158. }
  159. uni.showToast({
  160. title: '领取成功',
  161. });
  162. setTimeout(() => {
  163. getCoupon();
  164. }, 1000);
  165. }
  166. // TODO 芋艿:待测试
  167. const shareInfo = computed(() => {
  168. if (isEmpty(state.goodsInfo)) return {};
  169. return sheep.$platform.share.getShareInfo({
  170. title: state.goodsInfo.name,
  171. image: sheep.$url.cdn(state.goodsInfo.picUrl),
  172. desc: state.goodsInfo.introduction,
  173. params: {
  174. page: '2',
  175. query: state.goodsInfo.id,
  176. },
  177. }, {
  178. type: 'goods', // 商品海报
  179. title: state.goodsInfo.name, // 商品名称
  180. image: sheep.$url.cdn(state.goodsInfo.picUrl), // 商品主图
  181. price: fen2yuan(state.goodsInfo.price), // 商品价格
  182. original_price: fen2yuan(state.goodsInfo.marketPrice), // 商品原价
  183. });
  184. });
  185. async function getCoupon() {
  186. const { code, data } = await CouponApi.getCouponTemplateList(state.goodsId, 2, 10);
  187. if (code === 0) {
  188. state.couponInfo = data;
  189. }
  190. }
  191. onLoad((options) => {
  192. // 非法参数
  193. if (!options.id) {
  194. state.goodsInfo = null;
  195. return;
  196. }
  197. state.goodsId = options.id;
  198. // 1. 加载商品信息
  199. SpuApi.getSpuDetail(state.goodsId).then((res) => {
  200. // 未找到商品
  201. if (res.code !== 0 || !res.data) {
  202. state.goodsInfo = null;
  203. return;
  204. }
  205. // 加载到商品
  206. state.skeletonLoading = false;
  207. state.goodsInfo = res.data;
  208. // 加载是否收藏
  209. if (isLogin.value) {
  210. FavoriteApi.isFavoriteExists(state.goodsId, 'goods').then((res) => {
  211. if (res.code !== 0) {
  212. return;
  213. }
  214. state.goodsInfo.favorite = res.data;
  215. });
  216. }
  217. });
  218. // 2. 加载优惠劵信息
  219. getCoupon();
  220. // 3. 加载营销活动信息
  221. ActivityApi.getActivityListBySpuId(state.goodsId).then((res) => {
  222. if (res.code !== 0) {
  223. return;
  224. }
  225. res.data.forEach(activity => {
  226. if ([1, 2, 3].includes(activity.type)) { // 情况一:拼团/秒杀/砍价
  227. state.activityList.push(activity);
  228. } else if (activity.type === 5) { // 情况二:满减送
  229. state.activityInfo.push(activity);
  230. } else { // 情况三:限时折扣 TODO 芋艿
  231. console.log('待实现!优先级不高');
  232. }
  233. })
  234. });
  235. });
  236. </script>
  237. <style lang="scss" scoped>
  238. .detail-card {
  239. background-color: #ffff;
  240. margin: 14rpx 20rpx;
  241. border-radius: 10rpx;
  242. overflow: hidden;
  243. }
  244. // 价格标题卡片
  245. .title-card {
  246. .price-box {
  247. .price-text {
  248. font-size: 42rpx;
  249. font-weight: 500;
  250. color: #ff3000;
  251. line-height: 30rpx;
  252. font-family: OPPOSANS;
  253. &::before {
  254. content: '¥';
  255. font-size: 30rpx;
  256. }
  257. }
  258. .origin-price-text {
  259. font-size: 26rpx;
  260. font-weight: 400;
  261. text-decoration: line-through;
  262. color: $gray-c;
  263. font-family: OPPOSANS;
  264. &::before {
  265. content: '¥';
  266. }
  267. }
  268. }
  269. .sales-text {
  270. font-size: 26rpx;
  271. font-weight: 500;
  272. color: $gray-c;
  273. }
  274. .discounts-box {
  275. .tag-content {
  276. flex: 1;
  277. min-width: 0;
  278. white-space: nowrap;
  279. }
  280. .tag-box {
  281. overflow: hidden;
  282. text-overflow: ellipsis;
  283. }
  284. .tag {
  285. flex-shrink: 0;
  286. padding: 4rpx 10rpx;
  287. font-size: 24rpx;
  288. font-weight: 500;
  289. border-radius: 4rpx;
  290. color: var(--ui-BG-Main);
  291. background: var(--ui-BG-Main-tag);
  292. }
  293. .discounts-title {
  294. font-size: 24rpx;
  295. font-weight: 500;
  296. color: var(--ui-BG-Main);
  297. line-height: normal;
  298. }
  299. .cicon-forward {
  300. color: var(--ui-BG-Main);
  301. font-size: 24rpx;
  302. line-height: normal;
  303. margin-top: 4rpx;
  304. }
  305. }
  306. .title-text {
  307. font-size: 30rpx;
  308. font-weight: bold;
  309. line-height: 42rpx;
  310. }
  311. .subtitle-text {
  312. font-size: 26rpx;
  313. font-weight: 400;
  314. color: $dark-9;
  315. line-height: 42rpx;
  316. }
  317. }
  318. // 购买
  319. .buy-box {
  320. .add-btn {
  321. width: 214rpx;
  322. height: 72rpx;
  323. font-weight: 500;
  324. font-size: 28rpx;
  325. border-radius: 40rpx 0 0 40rpx;
  326. background-color: var(--ui-BG-Main-light);
  327. color: var(--ui-BG-Main);
  328. }
  329. .buy-btn {
  330. width: 214rpx;
  331. height: 72rpx;
  332. font-weight: 500;
  333. font-size: 28rpx;
  334. border-radius: 0 40rpx 40rpx 0;
  335. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  336. color: $white;
  337. }
  338. .disabled-btn {
  339. width: 428rpx;
  340. height: 72rpx;
  341. border-radius: 40rpx;
  342. background: #999999;
  343. color: $white;
  344. }
  345. }
  346. .model-box {
  347. height: 60vh;
  348. .model-content {
  349. height: 56vh;
  350. }
  351. .title {
  352. font-size: 36rpx;
  353. font-weight: bold;
  354. color: #333333;
  355. }
  356. .subtitle {
  357. font-size: 26rpx;
  358. font-weight: 500;
  359. color: #333333;
  360. }
  361. }
  362. </style>