home.vue 11 KB

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