goods-detail.vue 15 KB

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