goods-detail.vue 17 KB

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