home.vue 9.9 KB

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