home.vue 11 KB

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