index.js 3.7 KB

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