index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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: '1',
  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: 1
  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. }, 6000)
  54. }
  55. },
  56. onUnload() {
  57. this.storeBindings.destroyStoreBindings()
  58. },
  59. async getLocUserInfo() {
  60. this.storeBindings = createStoreBindings(this, {
  61. store,
  62. fields: {
  63. userInfo: 'userInfo'
  64. },
  65. })
  66. this.storeBindings.updateStoreBindings()
  67. },
  68. loadMore() {
  69. if (this.data.currentType == '1') {
  70. this.getData(getHotrecommendList, {
  71. grade: this.data.userInfo.grade
  72. })
  73. } else if (this.data.currentType == '2') {
  74. this.getData(getAuthorityList, {
  75. grade: this.data.userInfo.grade
  76. })
  77. }
  78. this.getCategoryList()
  79. },
  80. jumpChildClassify({
  81. currentTarget
  82. }) {
  83. let firstInfo = currentTarget.dataset.item
  84. let params = firstInfo.childList.length > 0 ? `list=${encodeURIComponent(JSON.stringify(firstInfo.childList))}` : `id=${firstInfo.id}`
  85. wx.navigateTo({
  86. url: `/pages/childClassify/index?type=class&title=${firstInfo.title}&${params}`,
  87. })
  88. },
  89. jumpSearch() {
  90. wx.navigateTo({
  91. url: '/pages/childClassify/index?type=search',
  92. })
  93. },
  94. /**
  95. * 监听页面滚动事件
  96. */
  97. onPageScroll(e) {
  98. if (e.scrollTop >= 103 && !this.data.isFixed) {
  99. this.setData({
  100. isFixed: true
  101. })
  102. } else if (e.scrollTop < 103 && this.data.isFixed) {
  103. this.setData({
  104. isFixed: false
  105. })
  106. }
  107. },
  108. onReachBottom() {
  109. this.loadMore()
  110. },
  111. async getCategoryList() {
  112. let grade = this.data.userInfo.grade
  113. let categoryList = await getCategoryList({
  114. grade
  115. })
  116. this.setData({
  117. categoryList
  118. })
  119. },
  120. selectType({
  121. target
  122. }) {
  123. if (target.dataset.type) {
  124. if (target.dataset.type == '3') {
  125. this.selectComponent('#worksList').resetAudio()
  126. }
  127. this.setData({
  128. currentType: target.dataset.type
  129. })
  130. if (target.dataset.type != '3') {
  131. this.resetData()
  132. }
  133. }
  134. },
  135. closeDesktop() {
  136. this.setData({
  137. desktopTips: false
  138. })
  139. }
  140. })