goods-detail.vue 18 KB

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