product-detail.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <template>
  2. <view class="product-detail" :class="{ hasBottom: isIphoneX }">
  3. <tui-skeleton :loadingType="2" v-if="isRequest"></tui-skeleton>
  4. <template v-else>
  5. <!-- 顶部tabs -->
  6. <tui-tabs
  7. v-show="scrollTop > 40 && showTabs"
  8. class="fixed"
  9. :tabs="tabs"
  10. :currentTab="currentTab"
  11. @change="tabChange"
  12. color="#666666"
  13. bold
  14. selectedColor="#FF457B"
  15. sliderBgColor="#FF457B"
  16. ></tui-tabs>
  17. <view id="anchor1" class="anchor">
  18. <!-- 商品图片 -->
  19. <uni-swiper-dot :info="imageList" :current="swiperCurrent" mode="left" class="swiper-box">
  20. <swiper @change="swiperChange" class="swiper">
  21. <swiper-item v-for="(image, index) in imageList" :key="index">
  22. <image :src="image" class="image" @click="previewImage(index)"></image>
  23. </swiper-item>
  24. </swiper>
  25. </uni-swiper-dot>
  26. <view class="row">
  27. <!-- 价格 -->
  28. <cm-product-price :productInfo="productInfo" class="cm-product-price"></cm-product-price>
  29. <!-- 商品信息 -->
  30. <cm-product-info :productInfo="productInfo" class="cm-product-info"></cm-product-info>
  31. </view>
  32. <!-- 商品参数 -->
  33. <view class="row touch-bar params" @click="paramsVisible = true">
  34. <view class="name">
  35. <text class="p1">参数:</text><text class="p2">品牌</text><text class="p2">分类...</text>
  36. </view>
  37. <text class="iconfont icon-chakangengduo"></text>
  38. </view>
  39. <!-- 优惠券 -->
  40. <view class="row touch-bar coupon" @click="couponVisible = true" v-if="couponList.length > 0">
  41. <view class="name">优惠券:</view>
  42. <view class="left">
  43. <view class="coupon-tags">
  44. <text class="tag" v-for="(tagName, index) in couponTags" :key="index">{{ tagName }}</text>
  45. </view>
  46. <text class="iconfont icon-chakangengduo"></text>
  47. </view>
  48. </view>
  49. </view>
  50. <!-- 商品详情 -->
  51. <view class="row product-info anchor" id="anchor2">
  52. <view class="title">商品详情</view>
  53. <view class="product-rich-text">
  54. <parser :html="detailHtml" :img-mode="widthFix" v-if="detailHtml"></parser>
  55. <view class="product-rich-text-none" v-else>暂无商品信息</view>
  56. </view>
  57. </view>
  58. <!-- 服务项目 -->
  59. <view class="row product-info anchor" id="anchor3">
  60. <!-- 服务项目 -->
  61. <view class="title">服务项目</view>
  62. <template v-if="product.productDetail.orderInfo || product.productDetail.serviceInfo">
  63. <cm-service :product="product.productDetail"></cm-service>
  64. </template>
  65. <view class="content-none" v-else> <text>暂无服务项目</text> </view>
  66. </view>
  67. </template>
  68. <!-- 商品导航 -->
  69. <cm-goods-nav :product-info="productInfo"></cm-goods-nav>
  70. <!-- 回到顶部 -->
  71. <tui-scroll-top :scrollTop="scrollTop" :bottom="0" :duration="500"></tui-scroll-top>
  72. <!-- 商品参数 -->
  73. <cm-product-params
  74. :visible="paramsVisible"
  75. :product-info="productInfo"
  76. @close="paramsVisible = false"
  77. ></cm-product-params>
  78. <!-- 优惠券列表 TODO-->
  79. <cm-coupon-list
  80. title="获取优惠券"
  81. listType="receive"
  82. :couponList="couponList"
  83. :visible="couponVisible"
  84. :showStatus="true"
  85. @close="closeCouponList"
  86. @couponClick="couponClick"
  87. ></cm-coupon-list>
  88. </view>
  89. </template>
  90. <script>
  91. import { mapGetters, mapActions, mapMutations } from 'vuex'
  92. import CmProductPrice from '@/components/cm-module/cm-product-price/cm-product-price.vue'
  93. import CmProductInfo from '@/components/cm-module/cm-product-info/cm-product-info.vue'
  94. import CmCouponList from '@/components/cm-module/cm-coupon-list/cm-coupon-list'
  95. import CmProductParams from '@/components/cm-module/cm-product-params/cm-product-params.vue'
  96. import CmGoodsNav from '@/components/cm-module/cm-goods-nav/cm-goods-nav.vue'
  97. import Parser from '@/components/jyf-Parser/index' //富文本处理
  98. import { debounce } from '@/common/common.js'
  99. const observers = {}
  100. export default {
  101. components: {
  102. Parser,
  103. CmProductPrice,
  104. CmProductInfo,
  105. CmCouponList,
  106. CmProductParams,
  107. CmGoodsNav
  108. },
  109. data() {
  110. return {
  111. tabs: [{ name: '商品' }, { name: '详情' }, { name: '服务项目' }],
  112. showTabs: false,
  113. currentTab: 0,
  114. productId: 1014,
  115. imageList: [
  116. 'https://picsum.photos/750/750?random=1',
  117. 'https://picsum.photos/750/750?random=2',
  118. 'https://picsum.photos/750/750?random=3'
  119. ],
  120. swiperCurrent: 0,
  121. productInfo: {},
  122. isRequest: true,
  123. scrollTop: 0,
  124. couponList: [],
  125. couponVisible: false,
  126. paramsVisible: false,
  127. jumpState: 1,
  128. selectorTimer: null,
  129. anchorList: [], // 锚点元素节点信息
  130. anchorStatus: [0, 0, 0] // 分别代表三个锚点区域的开关 0:关 1:开
  131. }
  132. },
  133. computed: {
  134. ...mapGetters(['isIphoneX', 'kindCount', 'userId']),
  135. // 优惠券标签
  136. couponTags() {
  137. const result = []
  138. for (let i = 0; i < this.couponList.length; i++) {
  139. if (i >= 3) return result
  140. if (this.couponList[i].noThresholdFlag === 1) {
  141. result.push(`减${this.couponList[i].couponAmount}`)
  142. } else {
  143. result.push(`满${this.couponList[i].touchPrice}减${this.couponList[i].couponAmount}`)
  144. }
  145. }
  146. return result
  147. },
  148. // 商品详情html
  149. detailHtml() {
  150. return this.productInfo.hasOwnProperty('productDetail') ? this.productInfo.productDetail.detailInfo : ''
  151. }
  152. },
  153. onPageScroll(e) {
  154. this.scrollTop = e.scrollTop
  155. // this.observerAnchor()
  156. this.setCurrentTab()
  157. },
  158. onLoad(option) {
  159. this.jumpState = parseInt(option.jumpState)
  160. this.productId = parseInt(option.productId)
  161. this.initProductDetail()
  162. this.getCouponByProduct()
  163. },
  164. onReady() {
  165. console.log('onReady')
  166. // if (!this.isRequest) {
  167. // this.getAnchorSection()
  168. // }
  169. setTimeout(() => {
  170. if (!this.isRequest) {
  171. this.getAnchorSection()
  172. }
  173. }, 2000)
  174. },
  175. //分享转发
  176. onShareAppMessage(res) {
  177. // 加密参数
  178. const shareData = {
  179. type: 1,
  180. productId: this.productInfo.productId,
  181. inviteUserId: this.userId,
  182. jumpState: this.jumpState
  183. }
  184. // 加密
  185. const state_str = encodeURIComponent(this.$crypto.encrypt(shareData))
  186. console.log(state_str)
  187. return {
  188. title: this.productInfo.name,
  189. path: `pages/tabBar/index/index?state_str=${state_str}`,
  190. imageUrl: this.imageList[0]
  191. }
  192. },
  193. methods: {
  194. // tab切换
  195. tabChange(e) {
  196. console.log(e)
  197. this.currentTab = e.index
  198. this.scrollToAnchor()
  199. },
  200. // 加入购物车 / 立即下单信息
  201. initProductInfo(data) {
  202. // jumpState 1: 非活动页商品详情 2: 活动页面商品详情
  203. data.heUserId = this.jumpState === 1 ? 0 : this.userId
  204. return data
  205. },
  206. // 初始化商品详情
  207. initProductDetail() {
  208. this.ProductService.QueryProductDetils({
  209. productId: this.productId,
  210. userId: this.userId
  211. }).then(res => {
  212. const data = res.data
  213. this.imageList = data.imageList
  214. this.productInfo = this.initProductInfo(data)
  215. this.isRequest = false
  216. })
  217. },
  218. // 获取当前商品可用优惠券列表
  219. getCouponByProduct() {
  220. this.CouponService.GetCouponByProduct({
  221. productId: this.productId,
  222. userId: this.userId
  223. }).then(res => {
  224. this.couponList = res.data
  225. })
  226. },
  227. // 关闭优惠券列表
  228. closeCouponList() {
  229. this.couponVisible = false
  230. },
  231. // 优惠券列表按钮点击事件
  232. couponClick() {
  233. this.getCouponByProduct()
  234. // this.couponVisible = false
  235. },
  236. // 轮播图切换
  237. swiperChange(e) {
  238. this.swiperCurrent = e.detail.current
  239. },
  240. // 图片预览
  241. previewImage(index) {
  242. uni.previewImage({
  243. current: index,
  244. urls: this.imageList
  245. })
  246. },
  247. // 获取锚点元素信息
  248. getAnchorSection() {
  249. const query = uni.createSelectorQuery().in(this)
  250. query
  251. .selectAll('.anchor')
  252. .boundingClientRect(data => {
  253. if (data.length > 0) {
  254. this.anchorList = data
  255. this.showTabs = true
  256. console.log(data)
  257. console.log('tabs is ready')
  258. // clearInterval(this.selectorTimer)
  259. this.observerAnchor(data)
  260. }
  261. })
  262. .exec()
  263. },
  264. // 滚动到锚点
  265. scrollToAnchor() {
  266. // const selector = '#anchor' + (this.currentTab + 1)
  267. let scrollTop = 0
  268. if (this.currentTab > 0) {
  269. scrollTop = this.anchorList[this.currentTab - 1].height - 40
  270. }
  271. uni.pageScrollTo({
  272. scrollTop: scrollTop
  273. })
  274. },
  275. // 为需要观测的区域创建观测者
  276. observerAnchor(selectorList = []) {
  277. const height = uni.getSystemInfoSync().windowHeight - 70
  278. selectorList.forEach((selector, index) => {
  279. observers[selector.id] = uni.createIntersectionObserver(this)
  280. observers[selector.id].relativeToViewport({ bottom: -height }).observe(`#${selector.id}`, res => {
  281. if (res.intersectionRatio > 0) {
  282. this.anchorStatus[index] = 1
  283. // console.log(index, `当前区域为${selector.id}标签选择器的区域...`)
  284. } else {
  285. this.anchorStatus[index] = 0
  286. // console.log(index, `离开区域为${selector.id}标签选择器的区域...`)
  287. }
  288. })
  289. })
  290. },
  291. // 设置currentTab
  292. setCurrentTab: debounce(function() {
  293. const index = this.anchorStatus.lastIndexOf(1)
  294. if (this.currentTab !== index) this.currentTab = index
  295. }, 200)
  296. }
  297. }
  298. </script>
  299. <style lang="scss" scoped>
  300. $swiper-width: 750rpx;
  301. .fixed {
  302. position: fixed;
  303. width: 100%;
  304. top: 0;
  305. left: 0;
  306. z-index: 99;
  307. }
  308. .product-detail {
  309. min-height: 100vh;
  310. background: #f7f7f7;
  311. box-sizing: border-box;
  312. // padding-top: 80rpx;
  313. padding-bottom: 100rpx;
  314. &.hasBottom {
  315. padding-bottom: 144rpx;
  316. }
  317. }
  318. .swiper-box {
  319. position: relative;
  320. }
  321. .swiper {
  322. width: $swiper-width;
  323. height: $swiper-width;
  324. .image {
  325. width: $swiper-width;
  326. height: $swiper-width;
  327. }
  328. }
  329. .row {
  330. padding: 0 24rpx;
  331. margin-bottom: 24rpx;
  332. background: #ffffff;
  333. &.touch-bar {
  334. line-height: 90rpx;
  335. display: flex;
  336. justify-content: space-between;
  337. align-items: center;
  338. .name {
  339. font-size: 28rpx;
  340. }
  341. }
  342. &.params {
  343. .p1,
  344. .iconfont {
  345. color: #999999;
  346. }
  347. .p2 {
  348. margin: 0 32rpx;
  349. color: #333333;
  350. }
  351. }
  352. &.coupon {
  353. color: #ff457b;
  354. .left {
  355. display: flex;
  356. align-items: center;
  357. justify-content: flex-end;
  358. .tag {
  359. padding: 2rpx 8rpx;
  360. font-size: 20rpx;
  361. background: #fff3f7;
  362. border: 1rpx solid #ff457b;
  363. border-radius: 8rpx;
  364. margin-left: 8rpx;
  365. box-sizing: border-box;
  366. }
  367. .iconfont {
  368. margin-left: 24rpx;
  369. }
  370. }
  371. }
  372. &.product-info {
  373. padding-bottom: 24rpx;
  374. .title {
  375. padding: 40rpx 0 32rpx;
  376. font-size: 28rpx;
  377. font-weight: bold;
  378. color: #333333;
  379. }
  380. .product-rich-text-none {
  381. box-sizing: border-box;
  382. text-align: left;
  383. font-size: 24rpx;
  384. color: #999999;
  385. line-height: 60rpx;
  386. }
  387. .content-none {
  388. height: 80rpx;
  389. line-height: 80rpx;
  390. text-align: left;
  391. font-size: 26rpx;
  392. color: #999999;
  393. box-sizing: border-box;
  394. }
  395. }
  396. }
  397. </style>