goods-detail.vue 16 KB

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