goods-detail.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. const coupon = { ...couponData }
  230. if (coupon.controlType === 'receive') {
  231. const index = this.couponList.findIndex(item => item.couponId === coupon.couponId)
  232. this.couponList.splice(index, 1)
  233. coupon.controlType = 'buy'
  234. coupon.couponStatus = 'received'
  235. this.$set(this.couponList, index, coupon)
  236. }
  237. },
  238. // 分享事件
  239. onShare() {
  240. const query = queryStringify({
  241. type: 1,
  242. productId: this.productInfo.productId,
  243. inviteUserId: this.userId,
  244. jumpState: this.jumpState
  245. })
  246. this.posterData = {
  247. porductName: this.productInfo.name,
  248. productPrice: this.productInfo.price,
  249. productOriginPrice:
  250. this.productInfo.normalPrice === this.productInfo.price ? 0 : this.productInfo.normalPrice,
  251. productImage: this.productInfo.mainImage,
  252. query: query
  253. }
  254. this.$refs.sharePopup.open()
  255. },
  256. // 轮播图事件
  257. onSwiperClick() {
  258. uni.previewImage({
  259. urls: this.imageList,
  260. current: this.current,
  261. loop: true
  262. })
  263. },
  264. // 轮播图切换
  265. onSwiperChange(current) {
  266. this.current = current
  267. },
  268. // 初始化导航按钮文案
  269. initNavbarButton() {
  270. const navbarButton = generateNavbarButtonText(this.productInfo)
  271. this.leftButton = navbarButton.left
  272. this.rightButton = navbarButton.right
  273. },
  274. // 导航菜单右侧按钮点击
  275. navbarRightClick(index) {
  276. this.navbarTypeFlag = index > 0 || this.productInfo.collageStatus > 0 ? 'buy' : 'cart'
  277. if (this.productInfo.activityType === 'group') {
  278. this.groupBuyFlag = index > 0
  279. }
  280. this.$refs.receiveBuyPopup.open()
  281. },
  282. // 导航栏菜单左侧按钮点击
  283. navbarLeftClick(index) {
  284. if (index === 0) {
  285. this.$router.switchTab('home')
  286. }
  287. if (index === 2) {
  288. if (!this.userId) {
  289. const pages = getCurrentPages()
  290. const page = pages[pages.length - 1]
  291. uni.setStorageSync('LOGIN_REDIRECT_URL', page.$page.fullPath)
  292. uni.redirectTo({ url: '/pages/authorize/login-custom' })
  293. return
  294. }
  295. this.$router.navigateTo('cart/cart')
  296. }
  297. },
  298. // 顶部tab切换
  299. onTabChange(index) {
  300. // const selector = `.product-detail #anchor-${index}`
  301. const offset = this.anchorList[index].top
  302. uni.pageScrollTo({
  303. scrollTop: this.scrollTop + offset - 40 - 10,
  304. duration: 300
  305. })
  306. },
  307. // 初始化锚点
  308. getAnchorList: debounce(
  309. function() {
  310. const query = uni.createSelectorQuery().in(this)
  311. query
  312. .selectAll('.anchor')
  313. .boundingClientRect(data => {
  314. this.anchorList = data
  315. this.setCurrentTabIndex()
  316. })
  317. .exec()
  318. },
  319. 300,
  320. false
  321. ),
  322. // 设置tab索引
  323. setCurrentTabIndex() {
  324. this.anchorList.forEach((item, index) => {
  325. if (item.bottom < 100) {
  326. this.currentTab = index
  327. }
  328. })
  329. },
  330. // 获取商品详情
  331. async fetchProductDetail() {
  332. try {
  333. const res = await fetchProductDetail({ productId: this.productId, userId: this.userId })
  334. this.productInfo = this.generateProductInfo(res.data)
  335. this.isRequest = false
  336. this.productInfo.heUserId = this.jumpState === 1 ? 0 : this.userId
  337. this.initNavbarButton()
  338. this.fetchCouponList()
  339. } catch (e) {
  340. console.log(e)
  341. console.log('获取商品详情失败')
  342. }
  343. },
  344. // 生成导航菜单类型
  345. generateNavbarType() {
  346. const { couponStatus = 0, collageStatus = 0, activeStatus = 0, discountStatus = 0 } = this.productInfo
  347. // 拼团价
  348. if (collageStatus > 0) {
  349. if (couponStatus === 1) {
  350. return 'groupWithCoupon' // 拼团券后价
  351. } else {
  352. return 'group' // 拼团价
  353. }
  354. }
  355. // 限时活动
  356. else if (discountStatus > 0 || activeStatus > 0) {
  357. if (couponStatus === 1) {
  358. return 'activityWithCoupon' // 券后价
  359. } else {
  360. return 'normal' // 限时活动价格
  361. }
  362. }
  363. // 无活动价
  364. else {
  365. if (couponStatus === 1) {
  366. return 'normalWithCoupon' // 普通券后价
  367. } else {
  368. return 'normal' // 普通价
  369. }
  370. }
  371. },
  372. // 创建优惠券
  373. generateCoupon(coupon) {
  374. const obj = Object.assign({}, coupon)
  375. // 添加标题
  376. if (coupon.noThresholdFlag > 0) {
  377. obj.couponTitle = `减¥${coupon.couponAmount.toFixed(2)}`
  378. } else {
  379. obj.couponTitle = `满¥${coupon.touchPrice.toFixed(2)}减¥${coupon.couponAmount.toFixed(2)}`
  380. }
  381. // 添加优惠券状态
  382. if (obj.useStatus === 0) {
  383. obj.controlType = 'receive'
  384. } else if (obj.useStatus === 1) {
  385. obj.couponStatus = 'received'
  386. obj.controlType = 'search'
  387. }
  388. return obj
  389. },
  390. // 获取商品可用优惠券
  391. async fetchCouponList() {
  392. try {
  393. const res = await fetchCouponListByProductId({ productId: this.productId, userId: this.userId })
  394. this.couponList = res.data.map(coupon => this.generateCoupon(coupon))
  395. } catch (e) {
  396. //TODO handle the exception
  397. console.log(e)
  398. console.log('获取优惠券列表失败')
  399. }
  400. },
  401. // 处理商品信息
  402. generateProductInfo(product = {}) {
  403. // 商品活动类型
  404. product.activityType = generateActivityType(product)
  405. product.priceType = generatePriceType(product)
  406. return product
  407. }
  408. }
  409. }
  410. </script>
  411. <style lang="scss" scoped>
  412. .product-detail {
  413. min-height: 100vh;
  414. padding-bottom: 100rpx;
  415. box-sizing: border-box;
  416. .section {
  417. margin: 24rpx 0;
  418. background-color: #fff;
  419. &.detail {
  420. padding: 24rpx;
  421. }
  422. &.service-items {
  423. padding: 24rpx;
  424. }
  425. .title {
  426. margin-bottom: 24rpx;
  427. font-size: 26rpx;
  428. font-weight: bold;
  429. }
  430. .section-empty {
  431. font-size: 24rpx;
  432. color: #999;
  433. }
  434. }
  435. .navbar {
  436. line-height: 1;
  437. text-align: center;
  438. &.group,
  439. &.normal {
  440. .prefix {
  441. display: none !important;
  442. }
  443. }
  444. .left-btn {
  445. color: #ff457b;
  446. &.text1 {
  447. font-size: 24rpx;
  448. margin-bottom: 4rpx;
  449. }
  450. .text2 {
  451. font-size: 24rpx;
  452. font-weight: bold;
  453. }
  454. .prefix {
  455. font-size: 20rpx;
  456. }
  457. }
  458. .right-btn {
  459. color: #fff;
  460. &.text1 {
  461. font-size: 24rpx;
  462. margin-bottom: 4rpx;
  463. }
  464. .text2 {
  465. font-size: 24rpx;
  466. font-weight: bold;
  467. }
  468. .prefix {
  469. font-size: 20rpx;
  470. }
  471. }
  472. }
  473. }
  474. </style>