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