home.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. },
  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. if (!banner.link) return
  153. this.$router.navigateTo(`others/web-view?websit=${banner.link}`)
  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. const link = this.bannerData[index].link
  167. if (!link) return
  168. this.$router.navigateTo(`others/web-view?websit=${link}`)
  169. },
  170. // 获取轮播图列表
  171. async fetchCarousel() {
  172. try {
  173. const resultCarousel = await fetchHomeCarousel() // 轮播图
  174. this.bannerData = resultCarousel.data
  175. } catch (e) {
  176. console.log('获取轮播图列表失败')
  177. }
  178. },
  179. // 获取金刚区菜单
  180. async fetchNavbar() {
  181. try {
  182. const resultNavbar = await fetchHomeNavbar() // 轮播图
  183. this.navbarList = resultNavbar.data
  184. } catch (e) {
  185. console.log('获取金刚区菜单失败')
  186. }
  187. },
  188. // 获取楼层信息
  189. async fetchFloorData() {
  190. try {
  191. const resultFloorData = await fetchHomeFloorData({ userId: this.userId }) // 商品楼层信息
  192. this.floorInfo = resultFloorData.data.page
  193. this.floorList = resultFloorData.data.floorList
  194. // 楼层信息加载成功
  195. this.isRequest = false
  196. uni.stopPullDownRefresh()
  197. } catch (e) {
  198. console.log('获取楼层信息失败')
  199. }
  200. },
  201. // 获取优惠券列表
  202. async fetchCouponList() {
  203. try {
  204. await this.fetchCouponDisplay() // 获取可领取优惠券类型
  205. const resultCouponData = await fetchHomeCouponList({ userId: this.userId })
  206. this.hasCouponWillReceive = resultCouponData.data && resultCouponData.data.length > 0
  207. this.couponList = [
  208. ...resultCouponData.data.map(item => {
  209. item.controlType = 'receive'
  210. return item
  211. }),
  212. ...this.generateCouponTips()
  213. ]
  214. } catch (e) {
  215. console.log('获取优惠券列表失败')
  216. }
  217. },
  218. // 获取可领取优惠券类型
  219. async fetchCouponDisplay() {
  220. try {
  221. const res = await fetchCouponDisplay({ userId: this.userId })
  222. this.couponTipStr = res.data
  223. return res
  224. } catch (e) {
  225. console.log('获取优惠券类型失败')
  226. return e
  227. }
  228. },
  229. // 生成优惠券说明
  230. generateCouponTips() {
  231. const result = [
  232. {
  233. type: 'tip',
  234. id: 1,
  235. name: '好友邀请券福利'
  236. },
  237. {
  238. type: 'tip',
  239. id: 2,
  240. name: '好友消费券福利'
  241. },
  242. {
  243. type: 'tip',
  244. id: 3,
  245. name: '分享券福利'
  246. }
  247. ]
  248. return result.filter(item => this.couponTipStr.indexOf(item.id.toString()) > -1)
  249. },
  250. // 分享
  251. onShare() {
  252. this.posterData = { query: queryStringify({ type: 0 }) }
  253. this.$refs.sharePopup.open()
  254. }
  255. }
  256. }
  257. </script>
  258. <style lang="scss" scoped>
  259. .grid {
  260. height: 24rpx;
  261. background-color: #f7f7f7;
  262. }
  263. .container {
  264. min-height: 100vh;
  265. background: url(https://static.caimei365.com/app/mini-hehe/icon/icon-index-bg.png) no-repeat top center;
  266. background-size: 750rpx auto;
  267. background-position-y: -220rpx;
  268. .search-input {
  269. width: 100%;
  270. padding: 24rpx;
  271. box-sizing: border-box;
  272. background: linear-gradient(180deg, #fa55bf 0%, #f8458b 100%);
  273. .search-content {
  274. @extend .cm-flex-center;
  275. justify-content: flex-start;
  276. width: 702rpx;
  277. height: 66rpx;
  278. border-radius: 33rpx;
  279. color: #8a8a8a;
  280. background: #fff;
  281. box-sizing: border-box;
  282. padding: 0 24rpx;
  283. .iconfont {
  284. font-size: 34rpx;
  285. margin-right: 12rpx;
  286. }
  287. .search-text {
  288. font-size: 24rpx;
  289. }
  290. }
  291. }
  292. // 加入我们
  293. .join-us {
  294. @extend .cm-flex-between;
  295. padding: 24rpx;
  296. background-color: #fff;
  297. .join-icon {
  298. width: 136rpx;
  299. height: 136rpx;
  300. display: block;
  301. }
  302. .join-content {
  303. @extend .cm-flex-center;
  304. flex-direction: column;
  305. align-items: flex-start;
  306. .join-label-title {
  307. font-size: 30rpx;
  308. font-weight: bold;
  309. color: #333333;
  310. margin-bottom: 24rpx;
  311. }
  312. .join-label-text {
  313. font-size: 24rpx;
  314. color: #333333;
  315. }
  316. }
  317. .join-button {
  318. width: 136rpx;
  319. height: 56rpx;
  320. text-align: center;
  321. line-height: 56rpx;
  322. font-size: 26rpx;
  323. font-weight: 400;
  324. color: #ffffff;
  325. background: #ff457b;
  326. border-radius: 8rpx;
  327. }
  328. }
  329. // 优惠券专区
  330. .coupon-area {
  331. padding-top: 24rpx;
  332. }
  333. .cm-floor {
  334. margin-bottom: 60rpx;
  335. }
  336. }
  337. </style>