goods-detail.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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: this.productInfo.normalPrice,
  227. productImage: 'https://picsum.photos/1000/1000?random=1',
  228. query: query
  229. }
  230. this.$refs.sharePopup.open()
  231. },
  232. // 轮播图事件
  233. onSwiperClick() {
  234. uni.previewImage({
  235. urls: this.imageList,
  236. current: this.current,
  237. loop: true
  238. })
  239. },
  240. // 轮播图切换
  241. onSwiperChange(current) {
  242. this.current = current
  243. },
  244. // 初始化导航按钮文案
  245. initNavbarButton() {
  246. const navbarButton = navbarButtonGroup[this.navbarType]
  247. if (this.navbarType === 'groupWithCoupon') {
  248. navbarButton.left[1] = `¥${this.productInfo.normalCouponPrice.toFixed(2)}`
  249. navbarButton.right[1] = `¥${this.productInfo.couponPrice.toFixed(2)}`
  250. } else if (this.navbarType === 'group') {
  251. navbarButton.left[1] = `¥${this.productInfo.normalPrice.toFixed(2)}`
  252. navbarButton.right[1] = `¥${this.productInfo.price.toFixed(2)}`
  253. } else if (this.navbarType === 'activityWithCoupon') {
  254. navbarButton.right[1] = `¥${this.productInfo.couponPrice.toFixed(2)}`
  255. } else if (this.navbarType === 'normalWithCoupon') {
  256. navbarButton.right[1] = `¥${this.productInfo.couponPrice.toFixed(2)}`
  257. } else {
  258. navbarButton.right[1] = ''
  259. }
  260. this.leftButton = navbarButton.left
  261. this.rightButton = navbarButton.right
  262. },
  263. // 导航菜单右侧按钮点击
  264. navbarRightClick(index) {
  265. this.navbarTypeFlag = index > 0 || this.productInfo.collageStatus > 0 ? 'buy' : 'cart'
  266. this.$refs.receiveBuyPopup.open()
  267. },
  268. // 导航栏菜单左侧按钮点击
  269. navbarLeftClick(index) {
  270. if (index === 0) {
  271. this.$router.switchTab('home')
  272. }
  273. if (index === 2) {
  274. if (!this.userId) {
  275. const pages = getCurrentPages()
  276. const page = pages[pages.length - 1]
  277. uni.setStorageSync('LOGIN_REDIRECT_URL', page.$page.fullPath)
  278. uni.redirectTo({ url: '/pages/authorize/login-custom' })
  279. return
  280. }
  281. this.$router.navigateTo('cart/cart')
  282. }
  283. },
  284. // 顶部tab切换
  285. onTabChange(index) {
  286. // const selector = `.product-detail #anchor-${index}`
  287. const offset = this.anchorList[index].top
  288. uni.pageScrollTo({
  289. scrollTop: this.scorllTop + offset - 40 - 10,
  290. duration: 300
  291. })
  292. },
  293. // 初始化锚点
  294. getAnchorList: debounce(
  295. function() {
  296. const query = uni.createSelectorQuery().in(this)
  297. query
  298. .selectAll('.anchor')
  299. .boundingClientRect(data => {
  300. this.anchorList = data
  301. this.setCurrentTabIndex()
  302. })
  303. .exec()
  304. },
  305. 300,
  306. false
  307. ),
  308. // 设置tab索引
  309. setCurrentTabIndex() {
  310. this.anchorList.forEach((item, index) => {
  311. if (item.bottom < 100) {
  312. this.currentTab = index
  313. }
  314. })
  315. },
  316. // 获取商品详情
  317. async fetchProductDetail() {
  318. try {
  319. const res = await fetchProductDetail({ productId: this.productId, userId: this.userId })
  320. this.productInfo = res.data
  321. this.isRequest = false
  322. this.productInfo.heUserId = this.jumpState === 1 ? 0 : this.userId
  323. this.initNavbarButton()
  324. this.fetchCouponList()
  325. } catch (e) {
  326. console.log(e)
  327. console.log('获取商品详情失败')
  328. }
  329. },
  330. // 生成导航菜单类型
  331. generateNavbarType() {
  332. const { couponStatus = 0, collageStatus = 0, activeStatus = 0, discountStatus = 0 } = this.productInfo
  333. if ((activeStatus > 0 || discountStatus > 0) && couponStatus > 0) {
  334. // (限时活动 / 活动价) + 优惠券
  335. return 'activityWithCoupon'
  336. } else if (collageStatus > 0) {
  337. if (couponStatus > 0) {
  338. // 拼团 + 优惠券
  339. return 'groupWithCoupon'
  340. } else {
  341. // 仅拼团
  342. return 'group'
  343. }
  344. } else {
  345. if (couponStatus > 0) {
  346. return 'normalWithCoupon'
  347. } else {
  348. // 无优惠券
  349. return 'normal'
  350. }
  351. }
  352. },
  353. // 创建优惠券
  354. generateCoupon(coupon) {
  355. const obj = Object.assign({}, coupon)
  356. // 添加标题
  357. if (coupon.noThresholdFlag > 0) {
  358. obj.couponTitle = `减${coupon.couponAmount}`
  359. } else {
  360. obj.couponTitle = `满${coupon.touchPrice}减${coupon.couponAmount}`
  361. }
  362. // 添加优惠券状态
  363. if (obj.useStatus === 0) {
  364. obj.controlType = 'receive'
  365. } else if (obj.useStatus === 1) {
  366. obj.couponStatus = 'received'
  367. obj.controlType = 'search'
  368. }
  369. return obj
  370. },
  371. // 获取商品可用优惠券
  372. async fetchCouponList() {
  373. try {
  374. const res = await fetchCouponListByProductId({ productId: this.productId, userId: this.userId })
  375. this.couponList = res.data.map(coupon => this.generateCoupon(coupon))
  376. } catch (e) {
  377. //TODO handle the exception
  378. console.log(e)
  379. console.log('获取优惠券列表失败')
  380. }
  381. }
  382. }
  383. }
  384. </script>
  385. <style lang="scss" scoped>
  386. .product-detail {
  387. min-height: 100vh;
  388. padding-bottom: 100rpx;
  389. box-sizing: border-box;
  390. .section {
  391. margin: 24rpx 0;
  392. background-color: #fff;
  393. &.detail {
  394. padding: 24rpx;
  395. }
  396. &.service-items {
  397. padding: 24rpx;
  398. }
  399. .title {
  400. margin-bottom: 24rpx;
  401. font-size: 26rpx;
  402. font-weight: bold;
  403. }
  404. .section-empty {
  405. font-size: 24rpx;
  406. color: #999;
  407. }
  408. }
  409. .navbar {
  410. line-height: 1;
  411. text-align: center;
  412. &.group,
  413. &.normal {
  414. .prefix {
  415. display: none !important;
  416. }
  417. }
  418. .left-btn {
  419. color: #ff457b;
  420. &.text1 {
  421. font-size: 24rpx;
  422. margin-bottom: 4rpx;
  423. }
  424. .text2 {
  425. font-size: 24rpx;
  426. font-weight: bold;
  427. }
  428. .prefix {
  429. font-size: 20rpx;
  430. }
  431. }
  432. .right-btn {
  433. color: #fff;
  434. &.text1 {
  435. font-size: 24rpx;
  436. margin-bottom: 4rpx;
  437. }
  438. .text2 {
  439. font-size: 24rpx;
  440. font-weight: bold;
  441. }
  442. .prefix {
  443. font-size: 20rpx;
  444. }
  445. }
  446. }
  447. }
  448. </style>