index.js 3.9 KB

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