goods-detail.vue 18 KB

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