product-detail.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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"
  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" mode="center" @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 :productInfo="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. :productInfo="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. const observers = {}
  99. export default {
  100. components: {
  101. Parser,
  102. CmProductPrice,
  103. CmProductInfo,
  104. CmCouponList,
  105. CmProductParams,
  106. CmGoodsNav
  107. },
  108. data() {
  109. return {
  110. tabs: [{ name: '商品' }, { name: '详情' }, { name: '服务项目' }],
  111. currentTab: 0,
  112. productId: 1014,
  113. imageList: [
  114. 'https://picsum.photos/750/750?random=1',
  115. 'https://picsum.photos/750/750?random=2',
  116. 'https://picsum.photos/750/750?random=3'
  117. ],
  118. swiperCurrent: 0,
  119. productInfo: {},
  120. isRequest: true,
  121. scrollTop: 0,
  122. couponList: [],
  123. couponVisible: false,
  124. paramsVisible: false,
  125. jumpState: 1,
  126. selectorTimer: null,
  127. anchorList: [],
  128. anchorStatus: [0, 0, 0]
  129. }
  130. },
  131. computed: {
  132. ...mapGetters(['isIphoneX', 'kindCount', 'userId']),
  133. // 优惠券标签
  134. couponTags() {
  135. const result = []
  136. for (let i = 0; i < this.couponList.length; i++) {
  137. if (i >= 3) return result
  138. if (this.couponList[i].noThresholdFlag === 1) {
  139. result.push(`减${this.couponList[i].couponAmount}`)
  140. } else {
  141. result.push(`满${this.couponList[i].touchPrice}减${this.couponList[i].couponAmount}`)
  142. }
  143. }
  144. return result
  145. },
  146. // 商品详情html
  147. detailHtml() {
  148. return this.productInfo.hasOwnProperty('productDetail') ? this.productInfo.productDetail.detailInfo : ''
  149. }
  150. },
  151. onPageScroll(e) {
  152. this.scrollTop = e.scrollTop
  153. // this.observerAnchor()
  154. this.setCurrentTab()
  155. },
  156. onLoad(option) {
  157. // 收集分享信息
  158. if (option.type == 'share') {
  159. this.setInviteUserId(option.inviteUserId)
  160. }
  161. this.jumpState = parseInt(option.jumpState)
  162. this.productId = parseInt(option.productId)
  163. this.initProductDetail()
  164. this.getCouponByProduct()
  165. },
  166. onReady() {
  167. this.selectorTimer = setInterval(() => {
  168. if (!this.isRequest) {
  169. this.getAnchorSection()
  170. }
  171. }, 1000)
  172. },
  173. //分享转发
  174. onShareAppMessage(res) {
  175. const queryString = this.$util.makeQueryStr({
  176. type: 'share',
  177. productId: this.productInfo.productId,
  178. inviteUserId: this.userId
  179. })
  180. return {
  181. title: this.productInfo.name,
  182. path: `pages/goods/product-detail?${queryString}`,
  183. imageUrl: this.imageList[0]
  184. }
  185. },
  186. methods: {
  187. ...mapMutations('user', ['setInviteUserId']),
  188. // tab切换
  189. tabChange(e) {
  190. this.currentTab = e.index
  191. this.scrollToAnchor()
  192. },
  193. // 加入购物车 / 立即下单信息
  194. initProductInfo(data) {
  195. // jumpState 1: 非活动页商品详情 2: 活动页面商品详情
  196. data.heUserId = this.jumpState === 1 ? 0 : this.userId
  197. return data
  198. },
  199. // 初始化商品详情
  200. initProductDetail() {
  201. this.ProductService.QueryProductDetils({
  202. productId: this.productId,
  203. userId: this.userId
  204. }).then(res => {
  205. const data = res.data
  206. this.imageList = data.imageList
  207. this.productInfo = this.initProductInfo(data)
  208. this.isRequest = false
  209. })
  210. },
  211. // 获取当前商品可用优惠券列表
  212. getCouponByProduct() {
  213. this.CouponService.GetCouponByProduct({
  214. productId: this.productId,
  215. userId: this.userId
  216. }).then(res => {
  217. this.couponList = res.data
  218. })
  219. },
  220. // 关闭优惠券列表
  221. closeCouponList() {
  222. this.couponVisible = false
  223. },
  224. // 优惠券列表按钮点击事件
  225. couponClick() {
  226. this.couponVisible = false
  227. },
  228. // 轮播图切换
  229. swiperChange(e) {
  230. this.swiperCurrent = e.detail.current
  231. },
  232. // 图片预览
  233. previewImage(index) {
  234. uni.previewImage({
  235. current: index,
  236. urls: this.imageList
  237. })
  238. },
  239. // 获取锚点元素信息
  240. getAnchorSection() {
  241. const query = uni.createSelectorQuery().in(this)
  242. query
  243. .selectAll('.anchor')
  244. .boundingClientRect(data => {
  245. console.log(data)
  246. if (data.length > 0) {
  247. console.log(data)
  248. this.anchorList = data
  249. clearInterval(this.selectorTimer)
  250. this.observerAnchor(data)
  251. }
  252. })
  253. .exec()
  254. },
  255. // 滚动到锚点
  256. scrollToAnchor() {
  257. // const selector = '#anchor' + (this.currentTab + 1)
  258. uni.pageScrollTo({
  259. scrollTop: this.anchorList[this.currentTab].top - 40
  260. })
  261. },
  262. // 创建观测者
  263. observerAnchor(selectorList = []) {
  264. const height = uni.getSystemInfoSync().windowHeight - 50
  265. selectorList.forEach((selector, index) => {
  266. observers[selector.id] = uni.createIntersectionObserver(this)
  267. observers[selector.id].relativeToViewport({ bottom: - height }).observe(`#${selector.id}`, res => {
  268. if (res.intersectionRatio > 0) {
  269. this.anchorStatus[index] = 1
  270. console.log(index, `当前区域为${selector.id}标签选择器的区域...`)
  271. } else {
  272. this.anchorStatus[index] = 0
  273. console.log(index, `离开区域为${selector.id}标签选择器的区域...`)
  274. }
  275. })
  276. })
  277. },
  278. // 设置currentTab
  279. setCurrentTab(){
  280. const index = this.anchorStatus.lastIndexOf(1)
  281. if(this.currentTab !== index) this.currentTab = index
  282. }
  283. }
  284. }
  285. </script>
  286. <style lang="scss" scoped>
  287. $swiper-width: 750rpx;
  288. .fixed {
  289. position: fixed;
  290. width: 100%;
  291. top: 0;
  292. left: 0;
  293. z-index: 99;
  294. }
  295. .product-detail {
  296. min-height: 100vh;
  297. background: #f7f7f7;
  298. box-sizing: border-box;
  299. // padding-top: 80rpx;
  300. padding-bottom: 100rpx;
  301. &.hasBottom {
  302. padding-bottom: 144rpx;
  303. }
  304. }
  305. .swiper-box {
  306. position: relative;
  307. }
  308. .swiper {
  309. width: $swiper-width;
  310. height: $swiper-width;
  311. .image {
  312. width: $swiper-width;
  313. height: $swiper-width;
  314. }
  315. }
  316. .row {
  317. padding: 0 24rpx;
  318. margin-bottom: 24rpx;
  319. background: #ffffff;
  320. &.touch-bar {
  321. line-height: 90rpx;
  322. display: flex;
  323. justify-content: space-between;
  324. align-items: center;
  325. .name {
  326. font-size: 28rpx;
  327. }
  328. }
  329. &.params {
  330. .p1,
  331. .iconfont {
  332. color: #999999;
  333. }
  334. .p2 {
  335. margin: 0 32rpx;
  336. color: #333333;
  337. }
  338. }
  339. &.coupon {
  340. color: #ff457b;
  341. .left {
  342. display: flex;
  343. align-items: center;
  344. justify-content: flex-end;
  345. .tag {
  346. padding: 2rpx 8rpx;
  347. font-size: 20rpx;
  348. background: #fff3f7;
  349. border: 1rpx solid #ff457b;
  350. border-radius: 8rpx;
  351. margin-left: 8rpx;
  352. }
  353. .iconfont {
  354. margin-left: 24rpx;
  355. }
  356. }
  357. }
  358. &.product-info {
  359. padding-bottom: 24rpx;
  360. .title {
  361. padding: 40rpx 0 32rpx;
  362. font-size: 28rpx;
  363. font-weight: bold;
  364. color: #333333;
  365. }
  366. .product-rich-text-none {
  367. box-sizing: border-box;
  368. text-align: left;
  369. font-size: 24rpx;
  370. color: #999999;
  371. line-height: 60rpx;
  372. }
  373. .content-none {
  374. height: 80rpx;
  375. line-height: 80rpx;
  376. text-align: left;
  377. font-size: 26rpx;
  378. color: #999999;
  379. box-sizing: border-box;
  380. }
  381. }
  382. }
  383. </style>