index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. 积分:{{ 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. 加入购物车
  80. </button>
  81. <button class="ss-reset-button buy-btn ui-Shadow-Main" @tap="state.showSelectSku = true">
  82. 立即购买
  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. 立即兑换
  89. </template>
  90. <template v-else-if="!state.goodsInfo.spuType">
  91. 立即购买
  92. </template>
  93. <template v-else>
  94. 立即兑换
  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;"> 已售罄 </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. onPageScroll(() => {});
  148. const state = reactive({
  149. goodsId: 0,
  150. skeletonLoading: true, // SPU 加载中
  151. goodsInfo: {}, // SPU 信息
  152. showSelectSku: false, // 是否展示 SKU 选择弹窗
  153. selectedSku: {}, // 选中的 SKU
  154. showModel: false, // 是否展示 Coupon 优惠劵的弹窗
  155. couponInfo: [], // 可领取的 Coupon 优惠劵的列表
  156. showActivityModel: false, // 【满减送/限时折扣】是否展示 Activity 营销活动的弹窗
  157. activityInfo: [], // 【满减送/限时折扣】可参与的 Activity 营销活动的列表
  158. activityList: [], // 【秒杀/拼团/砍价】可参与的 Activity 营销活动的列表
  159. linkId:0, // 分享时的id
  160. });
  161. // 规格变更
  162. function onSkuChange(e) {
  163. state.selectedSku = e;
  164. }
  165. // 添加购物车
  166. function onAddCart(e) {
  167. if (!e.id) {
  168. sheep.$helper.toast('请选择商品规格');
  169. return;
  170. }
  171. if(!isLogin.value){
  172. showAuthModal();
  173. return;
  174. }
  175. sheep.$store('cart').add(e);
  176. }
  177. // 立即购买
  178. function onBuy(e) {
  179. if (!state.selectedSku.id) {
  180. sheep.$helper.toast('请选择商品规格');
  181. return;
  182. }
  183. sheep.$router.go('/pages/order/confirm', {
  184. data: JSON.stringify({
  185. items: [{
  186. skuId: e.id,
  187. count: e.goods_num
  188. }],
  189. spuType: state.goodsInfo.spuType,
  190. spuPayType: state.goodsInfo.spuPayType,
  191. highPrecision: state.goodsInfo.highPrecision,
  192. // TODO 芋艿:后续清理掉这 2 参数
  193. deliveryType: 1,
  194. pointStatus: false,
  195. }),
  196. });
  197. }
  198. // 营销活动
  199. function onActivity() {
  200. state.showActivityModel = true;
  201. }
  202. // 立即领取
  203. async function onGet(id) {
  204. const {
  205. code
  206. } = await CouponApi.takeCoupon(id);
  207. if (code !== 0) {
  208. return;
  209. }
  210. uni.showToast({
  211. title: '领取成功',
  212. });
  213. setTimeout(() => {
  214. getCoupon();
  215. }, 1000);
  216. }
  217. // TODO 芋艿:待测试
  218. const shareInfo = computed(() => {
  219. if (isEmpty(state.goodsInfo)) return {};
  220. return sheep.$platform.share.getShareInfo({
  221. title: state.goodsInfo.name,
  222. image: sheep.$url.cdn(state.goodsInfo.image),
  223. desc: state.goodsInfo.subtitle,
  224. params: {
  225. page: '2',
  226. // query: state.linkId,
  227. },
  228. }, {
  229. type: 'goods', // 商品海报
  230. title: state.goodsInfo.name, // 商品标题
  231. introduction:state.goodsInfo.introduction,
  232. // image: sheep.$url.cdn(state.goodsInfo.image), // 商品主图
  233. image: sheep.$url.cdn(state.goodsInfo.picUrl), // 商品主图
  234. price: fen2yuan(state.goodsInfo.price), // 商品价格
  235. original_price: fen2yuan(state.goodsInfo.maretPrice), // 商品原价
  236. }, );
  237. });
  238. async function getCoupon() {
  239. const {
  240. code,
  241. data
  242. } = await CouponApi.getCouponTemplateList(state.goodsId, 2, 10);
  243. if (code === 0) {
  244. state.couponInfo = data;
  245. }
  246. }
  247. async function getSpuDetail(id){
  248. SpuApi.getSpuDetail(id).then((res) => {
  249. // 未找到商品
  250. if (res.code !== 0 || !res.data) {
  251. state.goodsInfo = null;
  252. return;
  253. }
  254. // 加载到商品
  255. state.skeletonLoading = false;
  256. state.goodsInfo = res.data;
  257. // console.log(state.goodsInfo )
  258. if(!isLogin.value){
  259. return;
  260. }
  261. // 加载是否收藏
  262. FavoriteApi.isFavoriteExists(state.goodsId, 'goods').then((res) => {
  263. if (res.code !== 0) {
  264. return;
  265. }
  266. state.goodsInfo.favorite = res.data;
  267. });
  268. });
  269. }
  270. async function getLink(){
  271. //加载分享时用到的linkId
  272. ShareApi.getLinkId(3,state.goodsId).then((res) => {
  273. if (res.code !== 0) {
  274. return;
  275. }
  276. state.linkId = res.data.linkId
  277. // console.log(state.linkId)
  278. });
  279. }
  280. const isLogin = computed(() => sheep.$store('user').isLogin)
  281. watch(() => isLogin.value, (newVal) => {
  282. if (newVal) {
  283. getSpuDetail(state.goodsId)
  284. }
  285. })
  286. onLoad((options) => {
  287. // console.log("options.id",options.id,"options.linkId",options.linkId)
  288. // 没有spuId和linkId的话 就有问题
  289. if (!options.id && !options.linkId) {
  290. state.goodsInfo = null;
  291. return;
  292. }
  293. state.goodsId = options.id || 0;
  294. // 如果只是没有spuId有linkId的话 要去linkId对应的spuId
  295. if(!options.id && options.linkId){
  296. console.log("如果只是没有spuId有linkId的话 要去linkId对应的spuId")
  297. const linkId = options.linkId.toString()
  298. ShareApi.getObjectIdByLinkId(linkId).then((res) => {
  299. if (res.code !== 0) {
  300. return;
  301. }
  302. state.goodsId = res.data
  303. getSpuDetail(state.goodsId)
  304. getLink()
  305. });
  306. uni.setStorageSync("linkId",options.linkId)
  307. }else{
  308. // 1. 加载商品信息
  309. getSpuDetail(state.goodsId)
  310. }
  311. // 2. 加载优惠劵信息
  312. // getCoupon();
  313. // 3. 加载营销活动信息
  314. ActivityApi.getActivityListBySpuId(state.goodsId).then((res) => {
  315. if (res.code !== 0) {
  316. return;
  317. }
  318. res.data.forEach(activity => {
  319. if ([1, 2, 3].includes(activity.type)) { // 情况一:拼团/秒杀/砍价
  320. state.activityList.push(activity);
  321. } else if (activity.type === 5) { // 情况二:满减送
  322. state.activityInfo.push(activity);
  323. } else { // 情况三:限时折扣 TODO 芋艿
  324. // console.log('待实现!优先级不高');
  325. }
  326. })
  327. });
  328. });
  329. </script>
  330. <style lang="scss" scoped>
  331. .detail-card {
  332. background-color: #ffff;
  333. margin: 14rpx 20rpx;
  334. border-radius: 10rpx;
  335. overflow: hidden;
  336. }
  337. // 价格标题卡片
  338. .title-card {
  339. .price-box {
  340. .price-text {
  341. font-size: 42rpx;
  342. font-weight: 500;
  343. color: #ff3000;
  344. line-height: 30rpx;
  345. font-family: OPPOSANS;
  346. // &::before {
  347. // content: '¥';
  348. // font-size: 30rpx;
  349. // }
  350. }
  351. .origin-price-text {
  352. font-size: 26rpx;
  353. font-weight: 400;
  354. color: $gray-c;
  355. font-family: OPPOSANS;
  356. background: #ffca3e;
  357. padding: 2px 8px;
  358. border-radius: 4px;
  359. color: #597533;
  360. }
  361. }
  362. .sales-text {
  363. font-size: 26rpx;
  364. font-weight: 500;
  365. color: $gray-c;
  366. }
  367. .discounts-box {
  368. .tag-content {
  369. flex: 1;
  370. min-width: 0;
  371. white-space: nowrap;
  372. }
  373. .tag-box {
  374. overflow: hidden;
  375. text-overflow: ellipsis;
  376. }
  377. .tag {
  378. flex-shrink: 0;
  379. padding: 4rpx 10rpx;
  380. font-size: 24rpx;
  381. font-weight: 500;
  382. border-radius: 4rpx;
  383. color: var(--ui-BG-Main);
  384. background: var(--ui-BG-Main-tag);
  385. }
  386. .discounts-title {
  387. font-size: 24rpx;
  388. font-weight: 500;
  389. color: var(--ui-BG-Main);
  390. line-height: normal;
  391. }
  392. .cicon-forward {
  393. color: var(--ui-BG-Main);
  394. font-size: 24rpx;
  395. line-height: normal;
  396. margin-top: 4rpx;
  397. }
  398. }
  399. .title-text {
  400. font-size: 30rpx;
  401. font-weight: bold;
  402. line-height: 42rpx;
  403. }
  404. .subtitle-text {
  405. font-size: 26rpx;
  406. font-weight: 400;
  407. color: $dark-9;
  408. line-height: 42rpx;
  409. }
  410. }
  411. // 购买
  412. .buy-box {
  413. .add-btn {
  414. width: 180rpx;
  415. height: 72rpx;
  416. font-weight: 500;
  417. font-size: 28rpx;
  418. border-radius: 40rpx 0 0 40rpx;
  419. background-color: var(--ui-BG-Main-light);
  420. color: var(--ui-BG-Main);
  421. }
  422. .buy-btn {
  423. width: 180rpx;
  424. height: 72rpx;
  425. font-weight: 500;
  426. font-size: 28rpx;
  427. border-radius: 0 40rpx 40rpx 0;
  428. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  429. color: $white;
  430. }
  431. .disabled-btn {
  432. width: 428rpx;
  433. height: 72rpx;
  434. border-radius: 40rpx;
  435. color: $white;
  436. }
  437. }
  438. .model-box {
  439. height: 60vh;
  440. .model-content {
  441. height: 56vh;
  442. }
  443. .title {
  444. font-size: 36rpx;
  445. font-weight: bold;
  446. color: #333333;
  447. }
  448. .subtitle {
  449. font-size: 26rpx;
  450. font-weight: 500;
  451. color: #333333;
  452. }
  453. }
  454. </style>