goods-detail.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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" v-if="returnGoodsStutas">
  40. <goods-return-instructions :content="helpContent"></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. returnGoodsStutas() {
  185. return this.productInfo.returnGoodsStutas && this.productInfo.returnGoodsStutas === 2
  186. },
  187. helpContent() {
  188. return this.productInfo.helpContent
  189. }
  190. },
  191. onPageScroll(e) {
  192. this.scrollTop = e.scrollTop
  193. this.getAnchorList()
  194. },
  195. onShareAppMessage() {
  196. const shareData = {
  197. type: 1,
  198. productId: this.productInfo.productId,
  199. inviteUserId: this.userId,
  200. jumpState: this.jumpState
  201. }
  202. return shareDataResult(shareData, this.productInfo.name, this.imageList[0])
  203. },
  204. onLoad(options) {
  205. this.productId = parseInt(options.productId)
  206. this.jumpState = parseInt(options.jumpState)
  207. },
  208. onShow() {
  209. this.initPage()
  210. },
  211. onHide() {
  212. this.autoplay = false
  213. },
  214. methods: {
  215. ...mapActions('cart', ['addToCart']),
  216. // 选择规格
  217. onUnitClick() {
  218. this.useType = 'unit'
  219. this.goodsBuyPopup = true
  220. },
  221. async initPage() {
  222. this.autoplay = true
  223. // 获取商品详情
  224. this.fetchProductDetail()
  225. // 获取商品可用优惠券
  226. this.couponList = await fetchCouponListByProduct(this.productId)
  227. },
  228. // 获取商品详情
  229. async fetchProductDetail() {
  230. this.productInfo = await fetchPorductInfo(this.productId)
  231. this.isRequest = false
  232. this.productInfo.heUserId = this.jumpState === 1 ? 0 : this.userId
  233. this.initNavbarButton()
  234. },
  235. // 活动价弹窗
  236. handleShowActivity(current) {
  237. if (!current.ladderPriceList) return
  238. this.ladderPriceList = current.ladderPriceList
  239. this.$refs.receiveBuyPopup.open()
  240. },
  241. // 商品提交
  242. onSubmit(detail) {
  243. // 用户未登录
  244. if (!this.userId) {
  245. const pages = getCurrentPages()
  246. const page = pages[pages.length - 1]
  247. uni.setStorageSync('LOGIN_REDIRECT_URL', page.$page.fullPath)
  248. uni.redirectTo({ url: '/pages/authorize/login-custom' })
  249. this.goodsBuyPopup = false
  250. return
  251. }
  252. // 加入购物车
  253. if (detail.type === 'cart') {
  254. this.addToCart({
  255. skuId: detail.sku.skuId,
  256. productCount: detail.count,
  257. heUserId: this.productInfo.heUserId
  258. })
  259. this.goodsBuyPopup = false
  260. return
  261. }
  262. console.log(detail)
  263. // 立即购买
  264. if (detail.type === 'buy') {
  265. const submitData = {
  266. productId: detail.sku.productId, // 产品id
  267. skuId: detail.sku.skuId, // sku id
  268. productCount: detail.count, // 产品购买数量
  269. heUserId: this.productInfo.heUserId, // 协销用户id
  270. collageFlag: this.groupBuyFlag ? 1 : 0, // 是否拼团购买
  271. collageId: this.collageId, // 拼团id
  272. couponId: detail.sku.couponId, // 默认使用优惠券id
  273. allCount: detail.count // 商品总数
  274. }
  275. uni.setStorageSync('COMMIT_PRODUCT_INFO', submitData)
  276. this.$router.navigateTo('order/order-create?type=product')
  277. this.goodsBuyPopup = false
  278. return
  279. }
  280. },
  281. // 优惠券点击事件
  282. onCouponClick(couponData) {
  283. if (couponData.controlType === 'receive') {
  284. this.fetchCouponList()
  285. }
  286. },
  287. // 分享事件
  288. onShare() {
  289. const query = queryStringify({
  290. type: 1,
  291. productId: this.productInfo.productId,
  292. inviteUserId: this.userId,
  293. jumpState: this.jumpState
  294. })
  295. this.posterData = {
  296. porductName: this.productInfo.name,
  297. productPrice: this.productInfo.price,
  298. productOriginPrice:
  299. this.productInfo.normalPrice === this.productInfo.price ? 0 : this.productInfo.normalPrice,
  300. productImage: this.productInfo.mainImage,
  301. query: query
  302. }
  303. this.$refs.sharePopup.open()
  304. },
  305. // 轮播图事件
  306. onSwiperClick() {
  307. uni.previewImage({
  308. urls: this.imageList,
  309. current: this.current,
  310. loop: true
  311. })
  312. },
  313. // 轮播图切换
  314. onSwiperChange(current) {
  315. this.current = current
  316. },
  317. // 初始化导航按钮文案
  318. initNavbarButton() {
  319. const navbarButton = generateNavbarButtonText(this.productInfo)
  320. this.leftButton = navbarButton.left
  321. this.rightButton = navbarButton.right
  322. },
  323. // 导航菜单右侧按钮点击
  324. navbarRightClick(index) {
  325. this.navbarTypeFlag = index > 0 || this.productInfo.collageStatus > 0 ? 'buy' : 'cart'
  326. if (this.productInfo.activityType === 'group') {
  327. this.groupBuyFlag = index > 0
  328. }
  329. this.useType = 'buy'
  330. this.buttonType = index === 0 ? 'left' : 'right'
  331. this.goodsBuyPopup = true
  332. },
  333. // 导航栏菜单左侧按钮点击
  334. navbarLeftClick(index) {
  335. if (index === 0) {
  336. this.$router.switchTab('home')
  337. }
  338. if (index === 2) {
  339. if (!this.userId) {
  340. const pages = getCurrentPages()
  341. const page = pages[pages.length - 1]
  342. uni.setStorageSync('LOGIN_REDIRECT_URL', page.$page.fullPath)
  343. uni.redirectTo({ url: '/pages/authorize/login-custom' })
  344. return
  345. }
  346. this.$router.navigateTo('cart/cart')
  347. }
  348. },
  349. // 顶部tab切换
  350. onTabChange(index) {
  351. const offset = this.anchorList[index].top
  352. uni.pageScrollTo({
  353. scrollTop: this.scrollTop + offset - 40 - 10,
  354. duration: 300
  355. })
  356. },
  357. // 初始化锚点
  358. getAnchorList: debounce(
  359. function() {
  360. const query = uni.createSelectorQuery().in(this)
  361. query
  362. .selectAll('.anchor')
  363. .boundingClientRect(data => {
  364. this.anchorList = data
  365. this.setCurrentTabIndex()
  366. })
  367. .exec()
  368. },
  369. 300,
  370. false
  371. ),
  372. // 设置tab索引
  373. setCurrentTabIndex() {
  374. this.anchorList.forEach((item, index) => {
  375. if (item.bottom < 100) {
  376. this.currentTab = index
  377. }
  378. })
  379. }
  380. }
  381. }
  382. </script>
  383. <style lang="scss">
  384. .section.detail {
  385. image {
  386. display: block;
  387. }
  388. }
  389. </style>
  390. <style lang="scss" scoped>
  391. .product-detail {
  392. min-height: 100vh;
  393. padding-bottom: 100rpx;
  394. box-sizing: border-box;
  395. .section {
  396. margin: 24rpx 0;
  397. background-color: #fff;
  398. &.detail {
  399. padding: 24rpx;
  400. }
  401. &.service-items {
  402. padding: 24rpx;
  403. }
  404. .title {
  405. margin-bottom: 24rpx;
  406. font-size: 26rpx;
  407. font-weight: bold;
  408. }
  409. .section-empty {
  410. font-size: 24rpx;
  411. color: #999;
  412. }
  413. }
  414. .navbar {
  415. line-height: 1;
  416. text-align: center;
  417. &.group,
  418. &.normal {
  419. .prefix {
  420. display: none !important;
  421. }
  422. }
  423. .left-btn {
  424. color: #ff457b;
  425. &.text1 {
  426. font-size: 24rpx;
  427. margin-bottom: 4rpx;
  428. }
  429. .text2 {
  430. font-size: 24rpx;
  431. font-weight: bold;
  432. }
  433. .prefix {
  434. font-size: 20rpx;
  435. }
  436. }
  437. .right-btn {
  438. color: #fff;
  439. &.text1 {
  440. font-size: 24rpx;
  441. margin-bottom: 4rpx;
  442. }
  443. .text2 {
  444. font-size: 24rpx;
  445. font-weight: bold;
  446. }
  447. .prefix {
  448. font-size: 20rpx;
  449. }
  450. }
  451. }
  452. }
  453. </style>