index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. const app = getApp()
  2. import {
  3. getHotrecommendList,
  4. } from "~/api/works"
  5. import {
  6. getBannerList
  7. } from '~/api/global'
  8. import reachBottom from '~/mixins/reachBottom'
  9. import event from '~/mixins/event'
  10. import share from '~/mixins/share'
  11. import {
  12. createStoreBindings
  13. } from 'mobx-miniprogram-bindings'
  14. import {
  15. store
  16. } from '~/store/index'
  17. Page({
  18. behaviors: [reachBottom, share,event],
  19. data: {
  20. navBarHeight: app.globalData.navBarHeight,
  21. background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
  22. currentType: '3',
  23. // 控制一级分类是否固定
  24. isFixed: false,
  25. desktopTips: app.globalData.desktopTips,
  26. bannerList: []
  27. },
  28. onLoad(options) {
  29. this.getLocUserInfo()
  30. if (Object.keys(this.data.userInfo).length > 0) {
  31. this.requestAgain()
  32. } else {
  33. getApp().callBack = (res) => {
  34. this.getLocUserInfo()
  35. this.requestAgain()
  36. }
  37. }
  38. let {
  39. desktopTips
  40. } = app.globalData
  41. if (desktopTips) {
  42. setTimeout(() => {
  43. this.setData({
  44. desktopTips: false
  45. })
  46. wx.setStorage({
  47. key: "preDesktopTime",
  48. data: new Date()
  49. })
  50. }, 6000)
  51. }
  52. },
  53. onShow() {
  54. if (typeof this.getTabBar === 'function') {
  55. this.getTabBar().setData({
  56. selected: 2
  57. })
  58. }
  59. },
  60. onUnload() {
  61. this.storeBindings.destroyStoreBindings()
  62. },
  63. onReachBottom() {
  64. this.loadMore()
  65. },
  66. async getLocUserInfo() {
  67. this.storeBindings = createStoreBindings(this, {
  68. store,
  69. fields: {
  70. userInfo: 'userInfo'
  71. },
  72. })
  73. this.storeBindings.updateStoreBindings()
  74. },
  75. async loadMore() {
  76. if (!this.data.userInfo.grade) {
  77. return
  78. }
  79. if (this.data.currentType == '3') {
  80. this.localGetData(getHotrecommendList, {
  81. grade: this.data.userInfo.grade
  82. })
  83. }
  84. },
  85. jumpSearch() {
  86. wx.navigateTo({
  87. url: '/pages/childClassify/index?type=search',
  88. })
  89. },
  90. /**
  91. * 监听页面滚动事件
  92. */
  93. onPageScroll(e) {
  94. if (e.scrollTop >= 103 && !this.data.isFixed) {
  95. this.setData({
  96. isFixed: true
  97. })
  98. } else if (e.scrollTop < 103 && this.data.isFixed) {
  99. this.setData({
  100. isFixed: false
  101. })
  102. }
  103. },
  104. closeDesktop() {
  105. this.setData({
  106. desktopTips: false
  107. })
  108. wx.setStorage({
  109. key: "preDesktopTime",
  110. data: new Date()
  111. })
  112. },
  113. async getBannerList() {
  114. let bannerList = await getBannerList({
  115. grade: this.data.userInfo.grade,
  116. })
  117. this.setData({
  118. bannerList,
  119. })
  120. },
  121. requestAgain() {
  122. this.resetData()
  123. this.getBannerList()
  124. if (this.data.currentType != 3) {
  125. this.selectComponent('#activityList').getActivities()
  126. }
  127. },
  128. selectType({
  129. target
  130. }) {
  131. if (target.dataset.type) {
  132. if (this.selectComponent('#worksList')) {
  133. this.selectComponent('#worksList').resetAudio()
  134. }
  135. this.setData({
  136. currentType: target.dataset.type,
  137. })
  138. if (target.dataset.type != 3) {
  139. this.selectComponent('#activityList').getActivities()
  140. }
  141. if (target.dataset.type == '3') {
  142. this.resetData()
  143. }
  144. }
  145. },
  146. onUnload() {
  147. this.storeBindings.destroyStoreBindings()
  148. },
  149. })