goods-detail.vue 15 KB

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