index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. /**
  32. * 生命周期函数--监听页面加载
  33. */
  34. onShow() {
  35. if (typeof this.getTabBar === 'function') {
  36. this.getTabBar().setData({
  37. selected: 0
  38. })
  39. }
  40. this.getLocUserInfo()
  41. if (Object.keys(this.data.userInfo).length > 0) {
  42. this.requestAgain()
  43. } else {
  44. getApp().callBack = (res) => {
  45. this.getLocUserInfo()
  46. this.requestAgain()
  47. }
  48. }
  49. let {
  50. desktopTips
  51. } = app.globalData
  52. if (desktopTips) {
  53. setTimeout(() => {
  54. this.setData({
  55. desktopTips: false
  56. })
  57. wx.setStorage({
  58. key: "preDesktopTime",
  59. data: new Date()
  60. })
  61. }, 6000)
  62. }
  63. },
  64. onUnload() {
  65. this.storeBindings.destroyStoreBindings()
  66. },
  67. onReachBottom() {
  68. this.loadMore()
  69. },
  70. async getLocUserInfo() {
  71. this.storeBindings = createStoreBindings(this, {
  72. store,
  73. fields: {
  74. userInfo: 'userInfo'
  75. },
  76. })
  77. this.storeBindings.updateStoreBindings()
  78. },
  79. async loadMore() {
  80. if (!this.data.userInfo.grade) {
  81. return
  82. }
  83. if (this.data.currentType == '2') {
  84. this.localGetData(getHotrecommendList, {
  85. grade: this.data.userInfo.grade
  86. })
  87. } else if (this.data.currentType == '1') {
  88. let list = await getAuthorityList({
  89. grade: this.data.userInfo.grade
  90. })
  91. this.setData({
  92. list
  93. })
  94. }
  95. },
  96. jumpChildClassify({
  97. currentTarget
  98. }) {
  99. let firstInfo = currentTarget.dataset.item
  100. let params = firstInfo.childList.length > 0 ? `list=${encodeURIComponent(JSON.stringify(firstInfo.childList))}` : `id=${firstInfo.id}`
  101. wx.navigateTo({
  102. url: `/pages/childClassify/index?type=class&title=${firstInfo.title}&${params}`,
  103. })
  104. },
  105. jumpSearch() {
  106. wx.navigateTo({
  107. url: '/pages/childClassify/index?type=search',
  108. })
  109. },
  110. /**
  111. * 监听页面滚动事件
  112. */
  113. onPageScroll(e) {
  114. if (e.scrollTop >= 103 && !this.data.isFixed) {
  115. this.setData({
  116. isFixed: true
  117. })
  118. } else if (e.scrollTop < 103 && this.data.isFixed) {
  119. this.setData({
  120. isFixed: false
  121. })
  122. }
  123. },
  124. async getCategoryList() {
  125. let grade = this.data.userInfo.grade
  126. let categoryList = await getCategoryList({
  127. grade
  128. })
  129. this.setData({
  130. categoryList
  131. })
  132. },
  133. closeDesktop() {
  134. this.setData({
  135. desktopTips: false
  136. })
  137. wx.setStorage({
  138. key: "preDesktopTime",
  139. data: new Date()
  140. })
  141. },
  142. async getBannerList() {
  143. let bannerList = await getBannerList({
  144. grade: this.data.userInfo.grade,
  145. })
  146. this.setData({
  147. bannerList,
  148. })
  149. },
  150. requestAgain() {
  151. this.resetData()
  152. this.getCategoryList()
  153. this.getBannerList()
  154. if (this.data.currentType == 7) {
  155. this.selectComponent('#activityList').getActivities()
  156. }
  157. },
  158. selectType({
  159. target
  160. }) {
  161. if (target.dataset.type) {
  162. if (this.selectComponent('#worksList')) {
  163. this.selectComponent('#worksList').resetAudio()
  164. }
  165. this.setData({
  166. currentType: target.dataset.type,
  167. list: [],
  168. })
  169. if (target.dataset.type != '7') {
  170. this.resetData()
  171. }
  172. }
  173. },
  174. onUnload() {
  175. this.storeBindings.destroyStoreBindings()
  176. },
  177. })