index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. const app = getApp()
  2. import {
  3. getHotrecommendList,
  4. getAuthorityList,
  5. getCategoryList
  6. } from "~/api/works"
  7. import reachBottom from '~/mixins/reachBottom'
  8. import share from '~/mixins/share'
  9. import {
  10. createStoreBindings
  11. } from 'mobx-miniprogram-bindings'
  12. import {
  13. store
  14. } from '~/store/index'
  15. Page({
  16. behaviors: [reachBottom, share],
  17. data: {
  18. navBarHeight: app.globalData.navBarHeight,
  19. background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
  20. currentType: '2',
  21. // 控制一级分类是否固定
  22. isFixed: false,
  23. desktopTips: app.globalData.desktopTips,
  24. isIOS: app.globalData.isIOS,
  25. categoryList: []
  26. },
  27. /**
  28. * 生命周期函数--监听页面加载
  29. */
  30. onShow() {
  31. if (typeof this.getTabBar === 'function') {
  32. this.getTabBar().setData({
  33. selected: 0
  34. })
  35. }
  36. this.getLocUserInfo()
  37. if (Object.keys(this.data.userInfo).length > 0) {
  38. this.resetData()
  39. } else {
  40. getApp().callBack = (res) => {
  41. this.getLocUserInfo()
  42. this.resetData()
  43. }
  44. }
  45. let {
  46. desktopTips
  47. } = app.globalData
  48. if (desktopTips) {
  49. setTimeout(() => {
  50. this.setData({
  51. desktopTips: false
  52. })
  53. wx.setStorage({
  54. key: "preDesktopTime",
  55. data: new Date()
  56. })
  57. }, 6000)
  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. loadMore() {
  76. if (!this.data.userInfo.grade) {
  77. return
  78. }
  79. if (this.data.currentType == '2') {
  80. this.getData(getHotrecommendList, {
  81. grade: this.data.userInfo.grade
  82. })
  83. } else if (this.data.currentType == '1') {
  84. this.getData(getAuthorityList, {
  85. grade: this.data.userInfo.grade
  86. })
  87. }
  88. this.getCategoryList()
  89. },
  90. jumpChildClassify({
  91. currentTarget
  92. }) {
  93. let firstInfo = currentTarget.dataset.item
  94. let params = firstInfo.childList.length > 0 ? `list=${encodeURIComponent(JSON.stringify(firstInfo.childList))}` : `id=${firstInfo.id}`
  95. wx.navigateTo({
  96. url: `/pages/childClassify/index?type=class&title=${firstInfo.title}&${params}`,
  97. })
  98. },
  99. jumpSearch() {
  100. wx.navigateTo({
  101. url: '/pages/childClassify/index?type=search',
  102. })
  103. },
  104. /**
  105. * 监听页面滚动事件
  106. */
  107. onPageScroll(e) {
  108. if (e.scrollTop >= 103 && !this.data.isFixed) {
  109. this.setData({
  110. isFixed: true
  111. })
  112. } else if (e.scrollTop < 103 && this.data.isFixed) {
  113. this.setData({
  114. isFixed: false
  115. })
  116. }
  117. },
  118. async getCategoryList() {
  119. let grade = this.data.userInfo.grade
  120. let categoryList = await getCategoryList({
  121. grade
  122. })
  123. this.setData({
  124. categoryList
  125. })
  126. },
  127. selectType({
  128. target
  129. }) {
  130. if (target.dataset.type) {
  131. if (target.dataset.type == '3') {
  132. this.selectComponent('#worksList').resetAudio()
  133. }
  134. this.setData({
  135. currentType: target.dataset.type
  136. })
  137. if (target.dataset.type != '3') {
  138. this.resetData()
  139. }
  140. }
  141. },
  142. closeDesktop() {
  143. this.setData({
  144. desktopTips: false
  145. })
  146. wx.setStorage({
  147. key: "preDesktopTime",
  148. data: new Date()
  149. })
  150. }
  151. })