goods-detail.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. // 配置
  109. import { generateNavbarButtonText } from './commons/helper.js'
  110. import { generateActivityType, generatePriceType } from '@/common/goods.helper.js'
  111. import uParse from '@/components/uni/uParse/src/wxParse'
  112. import { debounce } from '@/common/utils.js'
  113. import { shareDataResult } from '@/common/share.helper.js'
  114. import { fetchProductDetail } from '@/services/api/goods.js'
  115. import { fetchCouponListByProductId } from '@/services/api/coupon.js'
  116. import { queryStringify } from '@/common/utils.js'
  117. import { mapGetters, mapActions } from 'vuex'
  118. export default {
  119. components: {
  120. uParse
  121. },
  122. data() {
  123. return {
  124. isRequest: true,
  125. jumpState: 0,
  126. productId: '',
  127. // 轮播图
  128. currentTab: 0,
  129. current: 0,
  130. leftButton: {},
  131. rightButton: {},
  132. // 锚点列表
  133. anchorList: [],
  134. scrollTop: 0,
  135. productInfo: {},
  136. couponList: [],
  137. couponVisiable: false,
  138. navbarTypeFlag: '',
  139. // 海报数据
  140. posterData: {},
  141. collageId: '',
  142. groupBuyFlag: false, // 用户是否拼团购买
  143. autoplay: true,
  144. // 购买商品弹窗
  145. goodsBuyPopup: false,
  146. useType: 'buy',
  147. buttonType: 'left'
  148. }
  149. },
  150. computed: {
  151. ...mapGetters(['userId']),
  152. // 轮播图
  153. imageList() {
  154. return this.productInfo.imageList
  155. },
  156. // 活动价
  157. ladderList() {
  158. return this.productInfo.ladderList
  159. },
  160. // 商品详情
  161. productDetail() {
  162. return this.productInfo.productDetail
  163. },
  164. // 商品导航类型
  165. navbarType() {
  166. return this.generateNavbarType()
  167. },
  168. // 当前商品默认可以使的优惠券
  169. couponTip() {
  170. if (this.productInfo.couponId) {
  171. const currentCoupon = this.couponList.find(coupon => this.productInfo.couponId === coupon.couponId)
  172. return currentCoupon?.couponTitle
  173. } else {
  174. return this.productInfo?.couponInfo?.split('|')[1]
  175. }
  176. }
  177. },
  178. onPageScroll(e) {
  179. this.scrollTop = e.scrollTop
  180. this.getAnchorList()
  181. },
  182. onShareAppMessage() {
  183. const shareData = {
  184. type: 1,
  185. productId: this.productInfo.productId,
  186. inviteUserId: this.userId,
  187. jumpState: this.jumpState
  188. }
  189. return shareDataResult(shareData, this.productInfo.name, this.imageList[0])
  190. },
  191. onLoad(options) {
  192. this.productId = parseInt(options.productId)
  193. this.jumpState = parseInt(options.jumpState)
  194. },
  195. onShow() {
  196. this.autoplay = true
  197. this.fetchProductDetail()
  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. // 商品提交
  210. onSubmit(detail) {
  211. if (detail.type === 'cart') {
  212. this.addToCart({
  213. skuId: detail.sku.skuId,
  214. productCount: detail.count,
  215. heUserId: this.productInfo.heUserId
  216. })
  217. } else if (detail.type === 'buy') {
  218. if (!this.userId) {
  219. const pages = getCurrentPages()
  220. const page = pages[pages.length - 1]
  221. uni.setStorageSync('LOGIN_REDIRECT_URL', page.$page.fullPath)
  222. uni.redirectTo({ url: '/pages/authorize/login-custom' })
  223. return
  224. }
  225. const submitData = {
  226. productId: detail.sku.productId, // 产品id
  227. skuId: detail.sku.skuId, // sku id
  228. productCount: detail.count, // 产品购买数量
  229. heUserId: this.productInfo.heUserId, // 协销用户id
  230. collageFlag: this.groupBuyFlag ? 1 : 0, // 是否拼团购买
  231. collageId: this.collageId, // 拼团id
  232. couponId: detail.sku.couponId, // 默认使用优惠券id
  233. allCount: detail.count // 商品总数
  234. }
  235. uni.setStorageSync('COMMIT_PRODUCT_INFO', submitData)
  236. this.$router.navigateTo('order/order-create?type=product')
  237. }
  238. this.goodsBuyPopup = false
  239. },
  240. // 优惠券点击事件
  241. onCouponClick(couponData) {
  242. if (couponData.controlType === 'receive') {
  243. this.fetchCouponList()
  244. }
  245. },
  246. // 分享事件
  247. onShare() {
  248. const query = queryStringify({
  249. type: 1,
  250. productId: this.productInfo.productId,
  251. inviteUserId: this.userId,
  252. jumpState: this.jumpState
  253. })
  254. this.posterData = {
  255. porductName: this.productInfo.name,
  256. productPrice: this.productInfo.price,
  257. productOriginPrice:
  258. this.productInfo.normalPrice === this.productInfo.price ? 0 : this.productInfo.normalPrice,
  259. productImage: this.productInfo.mainImage,
  260. query: query
  261. }
  262. this.$refs.sharePopup.open()
  263. },
  264. // 轮播图事件
  265. onSwiperClick() {
  266. uni.previewImage({
  267. urls: this.imageList,
  268. current: this.current,
  269. loop: true
  270. })
  271. },
  272. // 轮播图切换
  273. onSwiperChange(current) {
  274. this.current = current
  275. },
  276. // 初始化导航按钮文案
  277. initNavbarButton() {
  278. const navbarButton = generateNavbarButtonText(this.productInfo)
  279. this.leftButton = navbarButton.left
  280. this.rightButton = navbarButton.right
  281. },
  282. // 导航菜单右侧按钮点击
  283. navbarRightClick(index) {
  284. this.navbarTypeFlag = index > 0 || this.productInfo.collageStatus > 0 ? 'buy' : 'cart'
  285. if (this.productInfo.activityType === 'group') {
  286. this.groupBuyFlag = index > 0
  287. }
  288. this.useType = 'buy'
  289. this.buttonType = index === 0 ? 'left' : 'right'
  290. this.goodsBuyPopup = true
  291. },
  292. // 导航栏菜单左侧按钮点击
  293. navbarLeftClick(index) {
  294. if (index === 0) {
  295. this.$router.switchTab('home')
  296. }
  297. if (index === 2) {
  298. if (!this.userId) {
  299. const pages = getCurrentPages()
  300. const page = pages[pages.length - 1]
  301. uni.setStorageSync('LOGIN_REDIRECT_URL', page.$page.fullPath)
  302. uni.redirectTo({ url: '/pages/authorize/login-custom' })
  303. return
  304. }
  305. this.$router.navigateTo('cart/cart')
  306. }
  307. },
  308. // 顶部tab切换
  309. onTabChange(index) {
  310. const offset = this.anchorList[index].top
  311. uni.pageScrollTo({
  312. scrollTop: this.scrollTop + offset - 40 - 10,
  313. duration: 300
  314. })
  315. },
  316. // 初始化锚点
  317. getAnchorList: debounce(
  318. function() {
  319. const query = uni.createSelectorQuery().in(this)
  320. query
  321. .selectAll('.anchor')
  322. .boundingClientRect(data => {
  323. this.anchorList = data
  324. this.setCurrentTabIndex()
  325. })
  326. .exec()
  327. },
  328. 300,
  329. false
  330. ),
  331. // 设置tab索引
  332. setCurrentTabIndex() {
  333. this.anchorList.forEach((item, index) => {
  334. if (item.bottom < 100) {
  335. this.currentTab = index
  336. }
  337. })
  338. },
  339. // 获取商品详情
  340. async fetchProductDetail() {
  341. try {
  342. const res = await fetchProductDetail({ productId: this.productId, userId: this.userId })
  343. this.isRequest = false
  344. this.productInfo.heUserId = this.jumpState === 1 ? 0 : this.userId
  345. this.productInfo = this.generateProductInfo(res.data)
  346. this.initNavbarButton()
  347. this.fetchCouponList()
  348. } catch (e) {
  349. console.log(e)
  350. console.log('获取商品详情失败')
  351. }
  352. },
  353. // 生成导航菜单类型
  354. generateNavbarType() {
  355. const { couponStatus = 0, collageStatus = 0, activeStatus = 0, discountStatus = 0 } = this.productInfo
  356. // 拼团价
  357. if (collageStatus > 0) {
  358. if (couponStatus === 1) {
  359. return 'groupWithCoupon' // 拼团券后价
  360. } else {
  361. return 'group' // 拼团价
  362. }
  363. }
  364. // 限时活动
  365. else if (discountStatus > 0 || activeStatus > 0) {
  366. if (couponStatus === 1) {
  367. return 'activityWithCoupon' // 券后价
  368. } else {
  369. return 'normal' // 限时活动价格
  370. }
  371. }
  372. // 无活动价
  373. else {
  374. if (couponStatus === 1) {
  375. return 'normalWithCoupon' // 普通券后价
  376. } else {
  377. return 'normal' // 普通价
  378. }
  379. }
  380. },
  381. // 创建优惠券
  382. generateCoupon(coupon) {
  383. const obj = Object.assign({}, coupon)
  384. // 添加标题
  385. if (coupon.noThresholdFlag > 0) {
  386. obj.couponTitle = `减¥${coupon.couponAmount}元`
  387. } else {
  388. obj.couponTitle = `满¥${coupon.touchPrice}元减¥${coupon.couponAmount}元`
  389. }
  390. // 添加优惠券状态
  391. if (obj.useStatus === 0) {
  392. obj.controlType = 'receive'
  393. } else if (obj.useStatus === 1) {
  394. obj.couponStatus = 'received'
  395. obj.controlType = 'search'
  396. }
  397. return obj
  398. },
  399. // 获取商品可用优惠券
  400. async fetchCouponList() {
  401. try {
  402. const res = await fetchCouponListByProductId({ productId: this.productId, userId: this.userId })
  403. this.couponList = res.data.map(coupon => this.generateCoupon(coupon))
  404. } catch (e) {
  405. //TODO handle the exception
  406. console.log(e)
  407. console.log('获取优惠券列表失败')
  408. }
  409. },
  410. // 处理商品信息
  411. generateProductInfo(product) {
  412. if (!product) return product
  413. // 商品活动类型
  414. product.activityType = generateActivityType(product)
  415. product.priceType = generatePriceType(product)
  416. product.skus = product.skus.map(sku => {
  417. sku.activityType = generateActivityType(sku)
  418. sku.priceType = generatePriceType(sku)
  419. return sku
  420. })
  421. return product
  422. }
  423. }
  424. }
  425. </script>
  426. <style lang="scss">
  427. .section.detail {
  428. image {
  429. display: block;
  430. }
  431. }
  432. </style>
  433. <style lang="scss" scoped>
  434. .product-detail {
  435. min-height: 100vh;
  436. padding-bottom: 100rpx;
  437. box-sizing: border-box;
  438. .section {
  439. margin: 24rpx 0;
  440. background-color: #fff;
  441. &.detail {
  442. padding: 24rpx;
  443. }
  444. &.service-items {
  445. padding: 24rpx;
  446. }
  447. .title {
  448. margin-bottom: 24rpx;
  449. font-size: 26rpx;
  450. font-weight: bold;
  451. }
  452. .section-empty {
  453. font-size: 24rpx;
  454. color: #999;
  455. }
  456. }
  457. .navbar {
  458. line-height: 1;
  459. text-align: center;
  460. &.group,
  461. &.normal {
  462. .prefix {
  463. display: none !important;
  464. }
  465. }
  466. .left-btn {
  467. color: #ff457b;
  468. &.text1 {
  469. font-size: 24rpx;
  470. margin-bottom: 4rpx;
  471. }
  472. .text2 {
  473. font-size: 24rpx;
  474. font-weight: bold;
  475. }
  476. .prefix {
  477. font-size: 20rpx;
  478. }
  479. }
  480. .right-btn {
  481. color: #fff;
  482. &.text1 {
  483. font-size: 24rpx;
  484. margin-bottom: 4rpx;
  485. }
  486. .text2 {
  487. font-size: 24rpx;
  488. font-weight: bold;
  489. }
  490. .prefix {
  491. font-size: 20rpx;
  492. }
  493. }
  494. }
  495. }
  496. </style>