home.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <template>
  2. <view class="container">
  3. <tui-skeleton v-if="isRequest" :loadingType="3" :isLoading="true"></tui-skeleton>
  4. <!-- 首页自定义导航栏 -->
  5. <view class="search-input sticky-top">
  6. <view class="search-content" @click="handleToSearch">
  7. <text class="iconfont icon-sousuo"></text>
  8. <view class="search-text">搜索商品</view>
  9. </view>
  10. </view>
  11. <!-- 轮播图 -->
  12. <cm-banner
  13. :list="bannerList"
  14. :current="current"
  15. :autoplay="autoplay"
  16. @change="index => (current = index)"
  17. @click="onSwiperClick"
  18. ></cm-banner>
  19. <!-- 金刚区菜单 -->
  20. <cm-navbar :modal="navbarList" @click="onNavbarClick"></cm-navbar>
  21. <!-- 加入我们 -->
  22. <view class="grid"></view>
  23. <view class="join-us">
  24. <image :src="staticUrl + 'icon-join-us.png'" class="join-icon"></image>
  25. <view class="join-content">
  26. <view class="join-label-title">颜选美学福利群</view>
  27. <view class="join-label-text">进群第一时间得知优惠福利!</view>
  28. </view>
  29. <view class="join-button" @click="handleJoinUs">加入我们</view>
  30. </view>
  31. <view class="grid"></view>
  32. <!-- 优惠券专区 -->
  33. <view class="coupon-area" v-if="couponList.length > 0">
  34. <cm-floor-title title="优惠券专区" @click="onMoreCoupon"></cm-floor-title>
  35. <cm-coupon-area :couponList="couponList" @click="onCouponClick"></cm-coupon-area>
  36. </view>
  37. <!-- 楼层区域 -->
  38. <view class="cm-floor" v-for="floorData in floorList" :key="floorData.id">
  39. <cm-floor-template
  40. :floorData="floorData"
  41. @onBannerClick="onBannerClick"
  42. @more="onMoreProduct"
  43. ></cm-floor-template>
  44. </view>
  45. <!-- 优惠券活动弹窗 -->
  46. <cm-active-alert></cm-active-alert>
  47. <!-- 返回顶部 -->
  48. <tui-scroll-top
  49. :scrollTop="scrollTop"
  50. :isShare="true"
  51. :bottom="60"
  52. :customShare="true"
  53. @share="onShare"
  54. ></tui-scroll-top>
  55. <!-- 分享弹窗 -->
  56. <cm-share-popup ref="sharePopup" :data="posterData" type="normal"></cm-share-popup>
  57. </view>
  58. </template>
  59. <script>
  60. import { fetchHomeCarousel, fetchHomeNavbar, fetchHomeFloorData, fetchHomeCouponList } from '@/services/api/home.js'
  61. import { fetchCouponDisplay } from '@/services/api/coupon.js'
  62. import { shareDataResult } from '@/common/share.helper.js'
  63. import { mapActions, mapGetters } from 'vuex'
  64. import { queryStringify } from '@/common/utils.js'
  65. export default {
  66. data() {
  67. return {
  68. isRequest: true,
  69. bannerData: [],
  70. navbarList: [],
  71. floorInfo: {},
  72. floorList: [],
  73. couponList: [],
  74. hasCouponWillReceive: false,
  75. current: 0,
  76. isRefresh: false,
  77. couponTipStr: '',
  78. scrollTop: 0,
  79. posterData: {},
  80. autoplay: true
  81. }
  82. },
  83. computed: {
  84. ...mapGetters(['userId']),
  85. bannerList() {
  86. return this.bannerData.map(item => item.image)
  87. }
  88. },
  89. onPageScroll(e) {
  90. this.scrollTop = e.scrollTop
  91. },
  92. onPullDownRefresh() {
  93. this.initHomeInfo()
  94. },
  95. onShareAppMessage() {
  96. // 加密
  97. const state_str = encodeURIComponent(this.$crypto.encrypt({ type: 0 }))
  98. return {
  99. title: '护肤上颜选,正品有好货~',
  100. path: `/pages/index/index?state_str=${state_str}`,
  101. imageUrl: this.staticUrl + 'icon-share.png'
  102. }
  103. },
  104. onShow() {
  105. this.checkCouponAlert()
  106. this.fetchCartKindCount() // 购物车商品数量
  107. this.fetchCouponList()
  108. this.autoplay = true
  109. },
  110. onHide() {
  111. this.autoplay = false
  112. this.$refs.sharePopup.close()
  113. },
  114. onLoad() {
  115. this.initHomeInfo()
  116. },
  117. methods: {
  118. ...mapActions('cart', ['fetchCartKindCount']),
  119. ...mapActions('coupon', ['checkCouponAlert']),
  120. // 初始化首页信息
  121. initHomeInfo() {
  122. this.fetchCarousel()
  123. this.fetchNavbar()
  124. this.fetchFloorData()
  125. // this.fetchCouponList()
  126. },
  127. // 搜索
  128. handleToSearch() {
  129. this.$router.navigateTo('goods/goods-search')
  130. },
  131. // 金刚区菜单导航
  132. onNavbarClick(navbar) {
  133. this.$setStorage('NAVBAR', { type: 'navbar', id: navbar.id, title: navbar.name })
  134. this.$router.navigateTo('goods/goods-list')
  135. },
  136. // 更多优惠券
  137. onMoreCoupon() {
  138. this.$router.navigateTo('coupon/coupon-receive')
  139. },
  140. // 优惠券点击(领取)
  141. onCouponClick(couponData) {
  142. const coupon = { ...couponData }
  143. if (coupon.controlType === 'receive') {
  144. const index = this.couponList.findIndex(item => item.couponId === coupon.couponId)
  145. this.couponList.splice(index, 1)
  146. coupon.controlType = 'use'
  147. this.$set(this.couponList, index, coupon)
  148. }
  149. },
  150. // 商品楼层banner点击事件
  151. onBannerClick(banner) {
  152. console.log(banner)
  153. this.jumpAction(banner)
  154. },
  155. // 更多商品
  156. onMoreProduct(floor) {
  157. this.$setStorage('NAVBAR', { type: 'floor', id: floor.id, title: floor.title })
  158. this.$router.navigateTo('goods/goods-list')
  159. },
  160. // 加入我们
  161. handleJoinUs() {
  162. this.$router.navigateTo('others/join-us')
  163. },
  164. // 轮播图跳转
  165. onSwiperClick(index) {
  166. this.jumpAction(this.bannerData[index])
  167. },
  168. // 获取轮播图列表
  169. async fetchCarousel() {
  170. try {
  171. const resultCarousel = await fetchHomeCarousel() // 轮播图
  172. this.bannerData = resultCarousel.data
  173. } catch (e) {
  174. console.log('获取轮播图列表失败')
  175. }
  176. },
  177. // 获取金刚区菜单
  178. async fetchNavbar() {
  179. try {
  180. const resultNavbar = await fetchHomeNavbar() // 轮播图
  181. this.navbarList = resultNavbar.data
  182. } catch (e) {
  183. console.log('获取金刚区菜单失败')
  184. }
  185. },
  186. // 获取楼层信息
  187. async fetchFloorData() {
  188. try {
  189. const resultFloorData = await fetchHomeFloorData({ userId: this.userId }) // 商品楼层信息
  190. this.floorInfo = resultFloorData.data.page
  191. this.floorList = resultFloorData.data.floorList
  192. // 楼层信息加载成功
  193. this.isRequest = false
  194. uni.stopPullDownRefresh()
  195. } catch (e) {
  196. console.log('获取楼层信息失败')
  197. }
  198. },
  199. // 获取优惠券列表
  200. async fetchCouponList() {
  201. try {
  202. await this.fetchCouponDisplay() // 获取可领取优惠券类型
  203. const resultCouponData = await fetchHomeCouponList({ userId: this.userId })
  204. this.hasCouponWillReceive = resultCouponData.data && resultCouponData.data.length > 0
  205. this.couponList = [
  206. ...resultCouponData.data.map(item => {
  207. item.controlType = 'receive'
  208. return item
  209. }),
  210. ...this.generateCouponTips()
  211. ]
  212. } catch (e) {
  213. console.log('获取优惠券列表失败')
  214. }
  215. },
  216. // 获取可领取优惠券类型
  217. async fetchCouponDisplay() {
  218. try {
  219. const res = await fetchCouponDisplay({ userId: this.userId })
  220. this.couponTipStr = res.data
  221. return res
  222. } catch (e) {
  223. console.log('获取优惠券类型失败')
  224. return e
  225. }
  226. },
  227. // 生成优惠券说明
  228. generateCouponTips() {
  229. const result = [
  230. {
  231. type: 'tip',
  232. id: 1,
  233. name: '好友邀请券福利'
  234. },
  235. {
  236. type: 'tip',
  237. id: 2,
  238. name: '好友消费券福利'
  239. },
  240. {
  241. type: 'tip',
  242. id: 3,
  243. name: '分享券福利'
  244. }
  245. ]
  246. return result.filter(item => this.couponTipStr.indexOf(item.id.toString()) > -1)
  247. },
  248. // 分享
  249. onShare() {
  250. this.posterData = { query: queryStringify({ type: 0 }) }
  251. this.$refs.sharePopup.open()
  252. }
  253. }
  254. }
  255. </script>
  256. <style lang="scss" scoped>
  257. .grid {
  258. height: 24rpx;
  259. background-color: #f7f7f7;
  260. }
  261. .container {
  262. min-height: 100vh;
  263. background: url(https://static.caimei365.com/app/mini-hehe/icon/icon-index-bg.png) no-repeat top center;
  264. background-size: 750rpx auto;
  265. background-position-y: -220rpx;
  266. .search-input {
  267. width: 100%;
  268. padding: 24rpx;
  269. box-sizing: border-box;
  270. background: linear-gradient(180deg, #fa55bf 0%, #f8458b 100%);
  271. .search-content {
  272. @extend .cm-flex-center;
  273. justify-content: flex-start;
  274. width: 702rpx;
  275. height: 66rpx;
  276. border-radius: 33rpx;
  277. color: #8a8a8a;
  278. background: #fff;
  279. box-sizing: border-box;
  280. padding: 0 24rpx;
  281. .iconfont {
  282. font-size: 34rpx;
  283. margin-right: 12rpx;
  284. }
  285. .search-text {
  286. font-size: 24rpx;
  287. }
  288. }
  289. }
  290. // 加入我们
  291. .join-us {
  292. @extend .cm-flex-between;
  293. padding: 24rpx;
  294. background-color: #fff;
  295. .join-icon {
  296. width: 136rpx;
  297. height: 136rpx;
  298. display: block;
  299. }
  300. .join-content {
  301. @extend .cm-flex-center;
  302. flex-direction: column;
  303. align-items: flex-start;
  304. .join-label-title {
  305. font-size: 30rpx;
  306. font-weight: bold;
  307. color: #333333;
  308. margin-bottom: 24rpx;
  309. }
  310. .join-label-text {
  311. font-size: 24rpx;
  312. color: #333333;
  313. }
  314. }
  315. .join-button {
  316. width: 136rpx;
  317. height: 56rpx;
  318. text-align: center;
  319. line-height: 56rpx;
  320. font-size: 26rpx;
  321. font-weight: 400;
  322. color: #ffffff;
  323. background: #ff457b;
  324. border-radius: 8rpx;
  325. }
  326. }
  327. // 优惠券专区
  328. .coupon-area {
  329. padding-top: 24rpx;
  330. }
  331. .cm-floor {
  332. margin-bottom: 60rpx;
  333. }
  334. }
  335. </style>