index.vue 14 KB

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