goods-detail.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <template>
  2. <view class="product-detail">
  3. <tui-skeleton v-if="isRequest" :loadingType="3" :isLoading="true"></tui-skeleton>
  4. <!-- 顶部导航 -->
  5. <goods-top-tabs @change="onTabChange" :current="currentTab" v-show="scorllTop > 100"></goods-top-tabs>
  6. <!-- 锚点0 -->
  7. <view id="anchor-0" class="anchor"></view>
  8. <!-- 轮播 -->
  9. <goods-image-swiper :list="imageList" @click="onSwiperClick" @change="onSwiperChange"></goods-image-swiper>
  10. <!-- 价格 -->
  11. <goods-price :productData="productInfo"></goods-price>
  12. <!-- 活动优惠券 -->
  13. <goods-coupon-list
  14. @click="couponVisiable = true"
  15. :couponList="couponList"
  16. v-if="couponList.length > 0"
  17. ></goods-coupon-list>
  18. <!-- 商品基本信息:商品名称 && 分享 && 标签 && 备注 && 服务-->
  19. <goods-info @share="onShare" :productData="productInfo"></goods-info>
  20. <!-- 参数 -->
  21. <view class="section" v-if="productInfo.parametersList.length > 0">
  22. <goods-params-section :paramList="productInfo.parametersList"></goods-params-section>
  23. </view>
  24. <!-- 优惠券 -->
  25. <view class="section" v-if="couponList.length > 0">
  26. <goods-coupon-section @click="couponVisiable = true" :couponList="couponList"></goods-coupon-section>
  27. </view>
  28. <!-- 锚点1 -->
  29. <view id="anchor-1" class="anchor"></view>
  30. <!-- 商品详情 -->
  31. <view class="section detail">
  32. <view class="title">商品详情</view>
  33. <view v-if="productDetail && productDetail.detailInfo" v-html="productDetail.detailInfo"></view>
  34. <!-- 空 -->
  35. <view class="section-empty" v-else>暂无商品详情</view>
  36. </view>
  37. <!-- 锚点2 -->
  38. <view id="anchor-2" class="anchor"></view>
  39. <!-- 服务项目 -->
  40. <view class="section service-items">
  41. <view class="title">服务项目</view>
  42. <view v-if="productDetail && productDetail.serviceInfo" v-html="productDetail.serviceInfo"></view>
  43. <!-- 空 -->
  44. <view class="section-empty">暂无服务项目</view>
  45. </view>
  46. <!-- 商品操作导航 -->
  47. <goods-navbar class="navbar" :class="navbarType" @rightClick="navbarRightClick" @leftClick="navbarLeftClick">
  48. <template v-slot:left>
  49. <view class="left-btn text1" v-text="leftButton[0]"></view>
  50. <view class="left-btn" v-if="leftButton[1]">
  51. <text class="prefix">券后</text>
  52. <text class="text2" v-text="leftButton[1]"></text>
  53. </view>
  54. </template>
  55. <template v-slot:right>
  56. <view class="right-btn text1" v-text="rightButton[0]"></view>
  57. <view class="right-btn" v-if="rightButton[1]">
  58. <text class="prefix">券后</text>
  59. <text class="text2" v-text="rightButton[1]"></text>
  60. </view>
  61. </template>
  62. </goods-navbar>
  63. <!-- 活动价 -->
  64. <goods-activity-popup
  65. ref="activitypPopup"
  66. :ladderList="ladderList"
  67. @open="$refs.receiveBuyPopup.close()"
  68. @close="$refs.receiveBuyPopup.open()"
  69. ></goods-activity-popup>
  70. <!-- 领券购买 -->
  71. <goods-receive-buy-popup
  72. ref="receiveBuyPopup"
  73. :productData="productInfo"
  74. :currentCoupon="currentCoupon"
  75. :navbarType="navbarTypeFlag"
  76. @detail="$refs.activitypPopup.open()"
  77. @submit="onSubmit"
  78. ></goods-receive-buy-popup>
  79. <!-- 分享弹窗 -->
  80. <cm-share-popup ref="sharePopup" :data="posterData" type="product"></cm-share-popup>
  81. <!-- 优惠券弹窗 -->
  82. <cm-coupon-popup
  83. :list="couponList"
  84. :visiable="couponVisiable"
  85. :hasSafeArea="true"
  86. @close="couponVisiable = false"
  87. @couponClick="onCouponClick"
  88. ></cm-coupon-popup>
  89. <!-- 安全区域 -->
  90. <cm-safe-area-bottom></cm-safe-area-bottom>
  91. </view>
  92. </template>
  93. <script>
  94. // 配置
  95. import { navbarButtonGroup } from './config/config.js'
  96. import { debounce } from '@/common/utils.js'
  97. import { shareDataResult } from '@/common/share.helper.js'
  98. import { fetchProductDetail } from '@/services/api/goods.js'
  99. import { fetchCouponListByProductId } from '@/services/api/coupon.js'
  100. import { mapGetters, mapActions } from 'vuex'
  101. export default {
  102. data() {
  103. return {
  104. isRequest: true,
  105. jumpState: 0,
  106. productId: '',
  107. // 轮播图
  108. currentTab: 0,
  109. leftButton: {},
  110. rightButton: {},
  111. // 锚点列表
  112. anchorList: [],
  113. scorllTop: 0,
  114. productInfo: {},
  115. couponList: [],
  116. couponVisiable: false,
  117. navbarTypeFlag: '',
  118. // 海报数据
  119. posterData: {}
  120. }
  121. },
  122. computed: {
  123. ...mapGetters(['userId']),
  124. // 轮播图
  125. imageList() {
  126. return this.productInfo.imageList
  127. },
  128. // 活动价
  129. ladderList() {
  130. return this.productInfo.ladderList
  131. },
  132. // 商品详情
  133. productDetail() {
  134. return this.productInfo.productDetail
  135. },
  136. // 商品导航类型
  137. navbarType() {
  138. return this.generateNavbarType()
  139. },
  140. // 当前商品默认可以使的优惠券
  141. currentCoupon() {
  142. return this.couponList.find(coupon => coupon.couponId === this.productInfo.couponId)
  143. }
  144. },
  145. onPageScroll(e) {
  146. this.scorllTop = e.scrollTop
  147. this.getAnchorList()
  148. },
  149. onShareAppMessage() {
  150. const shareData = {
  151. type: 1,
  152. productId: this.productInfo.productId,
  153. inviteUserId: this.userId,
  154. jumpState: this.jumpState
  155. }
  156. return shareDataResult(shareData, this.productInfo.name, this.imageList[0])
  157. },
  158. onLoad(options) {
  159. this.productId = parseInt(options.productId)
  160. this.jumpState = parseInt(options.jumpState)
  161. this.fetchProductDetail()
  162. },
  163. methods: {
  164. ...mapActions('cart', ['addToCart']),
  165. // 立即购买
  166. buyNow(count) {
  167. let productStp = {
  168. productCount: count,
  169. productId: this.productInfo.productId,
  170. heUserId: this.productInfo.heUserId,
  171. collageFlag: 0,
  172. collageId: 0,
  173. allCount: count
  174. }
  175. uni.setStorageSync('COMMIT_PRODUCT_INFO', productStp)
  176. this.toCreateOrder()
  177. },
  178. // 拼团购买
  179. buyGroupNow(count) {
  180. console.log('拼团购买')
  181. let productStp = {
  182. productCount: count,
  183. productId: this.productInfo.productId,
  184. heUserId: this.productInfo.heUserId,
  185. collageFlag: 1,
  186. collageId: 0,
  187. allCount: count
  188. }
  189. uni.setStorageSync('COMMIT_PRODUCT_INFO', productStp)
  190. this.toCreateOrder()
  191. },
  192. // 跳转到创建订单
  193. toCreateOrder() {
  194. this.$router.navigateTo('order/order-create?type=product')
  195. },
  196. // 商品提交
  197. onSubmit(count) {
  198. console.log(count)
  199. if (this.navbarTypeFlag === 'cart') {
  200. this.addToCart({
  201. productId: this.productInfo.productId,
  202. productCount: count,
  203. heUserId: this.productInfo.heUserId
  204. })
  205. return this.$refs.receiveBuyPopup.close()
  206. }
  207. if (this.navbarTypeFlag === 'buy') {
  208. if (this.productInfo.collageStatus > 0) {
  209. this.buyGroupNow(count)
  210. } else {
  211. this.buyNow(count)
  212. }
  213. return this.$refs.receiveBuyPopup.close()
  214. }
  215. },
  216. // 优惠券点击事件
  217. onCouponClick(couponData) {
  218. const coupon = { ...couponData }
  219. if (coupon.controlType === 'receive') {
  220. const index = this.couponList.findIndex(item => item.couponId === coupon.couponId)
  221. this.couponList.splice(index, 1)
  222. coupon.controlType = 'buy'
  223. coupon.couponStatus = 'received'
  224. this.$set(this.couponList, index, coupon)
  225. }
  226. },
  227. // 分享事件
  228. onShare() {
  229. this.posterData = {
  230. porductName: '肌本演绎360愉悦修护套组标准版防晒修复补水',
  231. productPrice: 500,
  232. productOriginPrice: 800,
  233. productImage: 'https://picsum.photos/1000/1000?random=1'
  234. }
  235. this.$refs.sharePopup.open()
  236. },
  237. // 轮播图事件
  238. onSwiperClick() {
  239. uni.previewImage({
  240. urls: this.imageList,
  241. current: this.current,
  242. loop: true
  243. })
  244. },
  245. // 轮播图切换
  246. onSwiperChange(current) {
  247. this.current = current
  248. },
  249. // 初始化导航按钮文案
  250. initNavbarButton() {
  251. const navbarButton = navbarButtonGroup[this.navbarType]
  252. // 拼团商品 + 其他商品
  253. if (this.navbarType === 'groupByCoupon') {
  254. navbarButton.left[1] = `¥${this.productInfo.normalCouponPrice.toFixed(2)}`
  255. } else {
  256. navbarButton.right[1] = `¥${this.productInfo.couponPrice.toFixed(2)}`
  257. }
  258. this.leftButton = navbarButton.left
  259. this.rightButton = navbarButton.right
  260. },
  261. // 导航菜单右侧按钮点击
  262. navbarRightClick(index) {
  263. this.navbarTypeFlag = index > 0 || this.productInfo.collageStatus > 0 ? 'buy' : 'cart'
  264. this.$refs.receiveBuyPopup.open()
  265. },
  266. // 导航栏菜单左侧按钮点击
  267. navbarLeftClick(index) {
  268. if (index === 0) {
  269. this.$router.switchTab('home')
  270. }
  271. if (index === 2) {
  272. this.$router.navigateTo('cart/cart')
  273. }
  274. },
  275. // 顶部tab切换
  276. onTabChange(index) {
  277. // const selector = `.product-detail #anchor-${index}`
  278. const offset = this.anchorList[index].top
  279. uni.pageScrollTo({
  280. scrollTop: this.scorllTop + offset - 40 - 10,
  281. duration: 300
  282. })
  283. },
  284. // 初始化锚点
  285. getAnchorList: debounce(
  286. function() {
  287. const query = uni.createSelectorQuery().in(this)
  288. query
  289. .selectAll('.anchor')
  290. .boundingClientRect(data => {
  291. this.anchorList = data
  292. this.setCurrentTabIndex()
  293. })
  294. .exec()
  295. },
  296. 300,
  297. false
  298. ),
  299. // 设置tab索引
  300. setCurrentTabIndex() {
  301. this.anchorList.forEach((item, index) => {
  302. if (item.bottom < 100) {
  303. this.currentTab = index
  304. }
  305. })
  306. },
  307. // 获取商品详情
  308. async fetchProductDetail() {
  309. try {
  310. const res = await fetchProductDetail({ productId: this.productId, userId: this.userId })
  311. this.productInfo = res.data
  312. this.isRequest = false
  313. this.productInfo.heUserId = this.jumpState === 1 ? 0 : this.userId
  314. this.initNavbarButton()
  315. this.fetchCouponList()
  316. } catch (e) {
  317. console.log(e)
  318. console.log('获取商品详情失败')
  319. }
  320. },
  321. // 生成导航菜单类型
  322. generateNavbarType() {
  323. const { couponStatus = 0, collageStatus = 0, activeStatus = 0, discountStatus = 0 } = this.productInfo
  324. if ((activeStatus > 0 || discountStatus > 0) && couponStatus > 0) {
  325. // (限时活动 / 活动价) + 优惠券
  326. return 'activity'
  327. } else if (collageStatus > 0) {
  328. if (couponStatus > 0) {
  329. // 拼团 + 优惠券
  330. return 'groupByCoupon'
  331. } else {
  332. // 仅拼团
  333. return 'group'
  334. }
  335. } else {
  336. // 无优惠券
  337. return 'normal'
  338. }
  339. },
  340. // 创建优惠券
  341. generateCoupon(coupon) {
  342. const obj = Object.assign({}, coupon)
  343. // 添加标题
  344. if (coupon.noThresholdFlag > 0) {
  345. obj.couponTitle = `减${coupon.couponAmount}`
  346. } else {
  347. obj.couponTitle = `满${coupon.touchPrice}减${coupon.couponAmount}`
  348. }
  349. // 添加优惠券状态
  350. if (obj.useStatus === 0) {
  351. obj.controlType = 'receive'
  352. } else if (obj.useStatus === 1) {
  353. obj.couponStatus = 'received'
  354. obj.controlType = 'search'
  355. }
  356. return obj
  357. },
  358. // 获取商品可用优惠券
  359. async fetchCouponList() {
  360. try {
  361. const res = await fetchCouponListByProductId({ productId: this.productId, userId: this.userId })
  362. this.couponList = res.data.map(coupon => this.generateCoupon(coupon))
  363. } catch (e) {
  364. //TODO handle the exception
  365. console.log(e)
  366. console.log('获取优惠券列表失败')
  367. }
  368. }
  369. }
  370. }
  371. </script>
  372. <style lang="scss" scoped>
  373. .product-detail {
  374. min-height: 100vh;
  375. padding-bottom: 100rpx;
  376. box-sizing: border-box;
  377. .section {
  378. margin: 24rpx 0;
  379. background-color: #fff;
  380. &.detail {
  381. padding: 24rpx;
  382. }
  383. &.service-items {
  384. padding: 24rpx;
  385. }
  386. .title {
  387. margin-bottom: 24rpx;
  388. font-size: 26rpx;
  389. font-weight: bold;
  390. }
  391. .section-empty {
  392. font-size: 24rpx;
  393. color: #999;
  394. }
  395. }
  396. .navbar {
  397. line-height: 1;
  398. text-align: center;
  399. &.group,
  400. &.normal {
  401. .prefix {
  402. display: none !important;
  403. }
  404. }
  405. .left-btn {
  406. color: #ff457b;
  407. &.text1 {
  408. font-size: 24rpx;
  409. margin-bottom: 4rpx;
  410. }
  411. .text2 {
  412. font-size: 24rpx;
  413. font-weight: bold;
  414. }
  415. .prefix {
  416. font-size: 20rpx;
  417. }
  418. }
  419. .right-btn {
  420. color: #fff;
  421. &.text1 {
  422. font-size: 24rpx;
  423. margin-bottom: 4rpx;
  424. }
  425. .text2 {
  426. font-size: 24rpx;
  427. font-weight: bold;
  428. }
  429. .prefix {
  430. font-size: 20rpx;
  431. }
  432. }
  433. }
  434. }
  435. </style>