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" @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] // 分别代表三个锚点区域的开关 0:关 1:开
  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. this.jumpState = parseInt(option.jumpState)
  158. this.productId = parseInt(option.productId)
  159. this.initProductDetail()
  160. this.getCouponByProduct()
  161. },
  162. onReady() {
  163. this.selectorTimer = setInterval(() => {
  164. if (!this.isRequest) {
  165. this.getAnchorSection()
  166. }
  167. }, 1000)
  168. },
  169. //分享转发
  170. onShareAppMessage(res) {
  171. // 加密参数
  172. const shareData = {
  173. type: 1,
  174. productId: this.productInfo.productId,
  175. inviteUserId: this.userId,
  176. jumpState: this.jumpState
  177. }
  178. // 加密
  179. const state_str = encodeURIComponent(this.$crypto.encrypt(shareData))
  180. console.log(state_str)
  181. return {
  182. title: this.productInfo.name,
  183. path: `pages/tabBar/index/index?state_str=${state_str}`,
  184. imageUrl: this.imageList[0]
  185. }
  186. },
  187. methods: {
  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.getCouponByProduct()
  227. // this.couponVisible = false
  228. },
  229. // 轮播图切换
  230. swiperChange(e) {
  231. this.swiperCurrent = e.detail.current
  232. },
  233. // 图片预览
  234. previewImage(index) {
  235. uni.previewImage({
  236. current: index,
  237. urls: this.imageList
  238. })
  239. },
  240. // 获取锚点元素信息
  241. getAnchorSection() {
  242. const query = uni.createSelectorQuery().in(this)
  243. query
  244. .selectAll('.anchor')
  245. .boundingClientRect(data => {
  246. if (data.length > 0) {
  247. this.anchorList = data
  248. clearInterval(this.selectorTimer)
  249. this.observerAnchor(data)
  250. }
  251. })
  252. .exec()
  253. },
  254. // 滚动到锚点
  255. scrollToAnchor() {
  256. // const selector = '#anchor' + (this.currentTab + 1)
  257. uni.pageScrollTo({
  258. scrollTop: this.anchorList[this.currentTab].top - 40
  259. })
  260. },
  261. // 为需要观测的区域创建观测者
  262. observerAnchor(selectorList = []) {
  263. const height = uni.getSystemInfoSync().windowHeight - 50
  264. selectorList.forEach((selector, index) => {
  265. observers[selector.id] = uni.createIntersectionObserver(this)
  266. observers[selector.id].relativeToViewport({ bottom: -height }).observe(`#${selector.id}`, res => {
  267. if (res.intersectionRatio > 0) {
  268. this.anchorStatus[index] = 1
  269. // console.log(index, `当前区域为${selector.id}标签选择器的区域...`)
  270. } else {
  271. this.anchorStatus[index] = 0
  272. // console.log(index, `离开区域为${selector.id}标签选择器的区域...`)
  273. }
  274. })
  275. })
  276. },
  277. // 设置currentTab
  278. setCurrentTab() {
  279. const index = this.anchorStatus.lastIndexOf(1)
  280. if (this.currentTab !== index) this.currentTab = index
  281. }
  282. }
  283. }
  284. </script>
  285. <style lang="scss" scoped>
  286. $swiper-width: 750rpx;
  287. .fixed {
  288. position: fixed;
  289. width: 100%;
  290. top: 0;
  291. left: 0;
  292. z-index: 99;
  293. }
  294. .product-detail {
  295. min-height: 100vh;
  296. background: #f7f7f7;
  297. box-sizing: border-box;
  298. // padding-top: 80rpx;
  299. padding-bottom: 100rpx;
  300. &.hasBottom {
  301. padding-bottom: 144rpx;
  302. }
  303. }
  304. .swiper-box {
  305. position: relative;
  306. }
  307. .swiper {
  308. width: $swiper-width;
  309. height: $swiper-width;
  310. .image {
  311. width: $swiper-width;
  312. height: $swiper-width;
  313. }
  314. }
  315. .row {
  316. padding: 0 24rpx;
  317. margin-bottom: 24rpx;
  318. background: #ffffff;
  319. &.touch-bar {
  320. line-height: 90rpx;
  321. display: flex;
  322. justify-content: space-between;
  323. align-items: center;
  324. .name {
  325. font-size: 28rpx;
  326. }
  327. }
  328. &.params {
  329. .p1,
  330. .iconfont {
  331. color: #999999;
  332. }
  333. .p2 {
  334. margin: 0 32rpx;
  335. color: #333333;
  336. }
  337. }
  338. &.coupon {
  339. color: #ff457b;
  340. .left {
  341. display: flex;
  342. align-items: center;
  343. justify-content: flex-end;
  344. .tag {
  345. padding: 2rpx 8rpx;
  346. font-size: 20rpx;
  347. background: #fff3f7;
  348. border: 1rpx solid #ff457b;
  349. border-radius: 8rpx;
  350. margin-left: 8rpx;
  351. box-sizing: border-box;
  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>