goods-detail.vue 17 KB

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