goods-detail.vue 15 KB

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