index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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="state.goodsSwiper" dotStyle="tag" imageMode="widthFix"
  15. 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. {{ state.selectedSkuPrice.price || formatPrice(state.goodsInfo.price) }}
  22. </view>
  23. <view class="origin-price-text" v-if="state.goodsInfo.original_price > 0">
  24. {{ state.selectedSkuPrice.original_price || state.goodsInfo.original_price }}
  25. </view>
  26. </view>
  27. <view class="sales-text">
  28. {{ formatSales(state.goodsInfo.sales_show_type, state.goodsInfo.sales) }}
  29. </view>
  30. </view>
  31. <view class="discounts-box ss-flex ss-row-between ss-m-b-28">
  32. <div class="tag-content">
  33. <view class="tag-box ss-flex">
  34. <view class="tag ss-m-r-10" v-for="promos in state.goodsInfo.promos"
  35. :key="promos.id" @tap="onActivity">
  36. {{ promos.title }}
  37. </view>
  38. </view>
  39. </div>
  40. <view class="get-coupon-box ss-flex ss-col-center ss-m-l-20" @tap="state.showModel = true"
  41. v-if="state.couponInfo.length">
  42. <view class="discounts-title ss-m-r-8">领券</view>
  43. <text class="cicon-forward"></text>
  44. </view>
  45. </view>
  46. <view class="title-text ss-line-2 ss-m-b-6">{{ state.goodsInfo.title }}</view>
  47. <view class="subtitle-text ss-line-1">{{ state.goodsInfo.subtitle }}</view>
  48. </view>
  49. <!-- 功能卡片 -->
  50. <view class="detail-cell-card detail-card ss-flex-col">
  51. <detail-cell-sku v-model="state.selectedSkuPrice.goods_sku_text" :skus="state.goodsInfo.skus"
  52. @tap="state.showSelectSku = true" />
  53. <detail-cell-service v-if="state.goodsInfo.service" v-model="state.goodsInfo.service" />
  54. <detail-cell-params v-if="state.goodsInfo.params" v-model="state.goodsInfo.params" />
  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.goodsInfo.activities" :data="state.goodsInfo"></detail-activity-tip>
  66. <!-- 详情tabbar -->
  67. <detail-tabbar v-model="state.goodsInfo">
  68. <!-- TODO: 缺货中 已售罄 判断 设计-->
  69. <view class="buy-box ss-flex ss-col-center ss-p-r-20" v-if="state.goodsInfo.stock > 0">
  70. <button class="ss-reset-button add-btn ui-Shadow-Main" @tap="state.showSelectSku = true">
  71. 加入购物车
  72. </button>
  73. <button class="ss-reset-button buy-btn ui-Shadow-Main" @tap="state.showSelectSku = true">
  74. 立即购买
  75. </button>
  76. </view>
  77. <view class="buy-box ss-flex ss-col-center ss-p-r-20" v-else>
  78. <button class="ss-reset-button disabled-btn" disabled> 已售罄 </button>
  79. </view>
  80. </detail-tabbar>
  81. <s-coupon-get v-model="state.couponInfo" :show="state.showModel" @close="state.showModel = false"
  82. @get="onGet" />
  83. <s-activity-pop v-model="state.activityInfo" :show="state.showActivityModel"
  84. @close="state.showActivityModel = false" />
  85. </block>
  86. </s-layout>
  87. </view>
  88. </template>
  89. <script setup>
  90. import {
  91. reactive,
  92. computed
  93. } from 'vue';
  94. import {
  95. onLoad,
  96. onPageScroll
  97. } from '@dcloudio/uni-app';
  98. import sheep from '@/sheep';
  99. import {
  100. formatSales,
  101. formatGoodsSwiper,
  102. formatPrice
  103. } from '@/sheep/hooks/useGoods';
  104. import detailNavbar from './components/detail/detail-navbar.vue';
  105. import detailCellSku from './components/detail/detail-cell-sku.vue';
  106. import detailCellService from './components/detail/detail-cell-service.vue';
  107. import detailCellParams from './components/detail/detail-cell-params.vue';
  108. import detailTabbar from './components/detail/detail-tabbar.vue';
  109. import detailSkeleton from './components/detail/detail-skeleton.vue';
  110. import detailCommentCard from './components/detail/detail-comment-card.vue';
  111. import detailContentCard from './components/detail/detail-content-card.vue';
  112. import detailActivityTip from './components/detail/detail-activity-tip.vue';
  113. import {
  114. isEmpty
  115. } from 'lodash';
  116. // import detailActivityTip from './components/detail/detail-activity-tip.vue';
  117. // import detailTab from './components/detail/detail-tab.vue';
  118. // import detailCoupon from './components/detail/detail-coupon.vue';
  119. onPageScroll(() => {});
  120. const state = reactive({
  121. goodsId: 0,
  122. skeletonLoading: true,
  123. goodsInfo: {},
  124. showSelectSku: false,
  125. goodsSwiper: [],
  126. selectedSkuPrice: {},
  127. showModel: false,
  128. total: 0,
  129. couponInfo: [],
  130. showActivityModel: false,
  131. activityInfo: [],
  132. });
  133. // 规格变更
  134. function onSkuChange(e) {
  135. state.selectedSkuPrice = e;
  136. }
  137. // 添加购物车
  138. function onAddCart(e) {
  139. sheep.$store('cart').add(e);
  140. }
  141. // 立即购买
  142. function onBuy(e) {
  143. sheep.$router.go('/pages/order/confirm', {
  144. data: JSON.stringify({
  145. order_type: 'goods',
  146. goods_list: [{
  147. goods_id: e.goods_id,
  148. goods_num: e.goods_num,
  149. goods_sku_price_id: e.id,
  150. }, ],
  151. }),
  152. });
  153. }
  154. //营销活动
  155. function onActivity() {
  156. state.activityInfo = state.goodsInfo.promos;
  157. state.showActivityModel = true;
  158. }
  159. //立即领取
  160. async function onGet(id) {
  161. const {
  162. error,
  163. msg
  164. } = await sheep.$api.coupon.get(id);
  165. if (error === 0) {
  166. uni.showToast({
  167. title: msg,
  168. });
  169. setTimeout(() => {
  170. getCoupon();
  171. }, 1000);
  172. }
  173. }
  174. const shareInfo = computed(() => {
  175. if (isEmpty(state.goodsInfo)) return {};
  176. return sheep.$platform.share.getShareInfo({
  177. title: state.goodsInfo.title,
  178. image: sheep.$url.cdn(state.goodsInfo.image),
  179. desc: state.goodsInfo.subtitle,
  180. params: {
  181. page: '2',
  182. query: state.goodsInfo.id,
  183. },
  184. }, {
  185. type: 'goods', // 商品海报
  186. title: state.goodsInfo.title, // 商品标题
  187. image: sheep.$url.cdn(state.goodsInfo.image), // 商品主图
  188. price: state.goodsInfo.price[0], // 商品价格
  189. original_price: state.goodsInfo.original_price, // 商品原价
  190. }, );
  191. });
  192. onLoad(async (options) => {
  193. console.log('页面被访问')
  194. // 非法参数
  195. if (!options.id) {
  196. state.goodsInfo = null;
  197. return;
  198. }
  199. state.goodsId = options.id;
  200. // 加载商品信息
  201. sheep.$api.goods.detail(state.goodsId).then((res) => {
  202. // 处理数据适配
  203. // let arr = [];
  204. // res.skus = res.data.skus.map(item => {
  205. // arr = [...arr, ...item.properties];
  206. // item.children = item.properties;
  207. // item.goods_id = res.data.id;
  208. // item.name = item.children[0].propertyName;
  209. // return item;
  210. // })
  211. // console.log(arr, '合并');
  212. // console.log(res.data, '替换后订单数据');
  213. state.skeletonLoading = false;
  214. if (res.code === 0) {
  215. // 在此处对数据做出转换
  216. res.data.sales = res.data.salesCount
  217. res.data.original_price = res.data.marketPrice / 100
  218. res.data.subtitle = res.data.introduction
  219. res.data.title = res.data.name
  220. res.data.price = [res.data.price / 100]
  221. state.goodsInfo = res.data;
  222. state.goodsSwiper = formatGoodsSwiper(state.goodsInfo.sliderPicUrls);
  223. } else {
  224. // 未找到商品
  225. state.goodsInfo = null;
  226. }
  227. });
  228. const {
  229. error,
  230. data
  231. } = await sheep.$api.coupon.listByGoods(state.goodsId);
  232. if (error === 0) {
  233. state.couponInfo = data;
  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>