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. /**
  18. * 页面的初始数据
  19. */
  20. data: {
  21. navBarHeight: app.globalData.navBarHeight,
  22. background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
  23. currentType: '1',
  24. // 控制一级分类是否固定
  25. isFixed: false,
  26. desktopTips: app.globalData.desktopTips,
  27. isIOS: app.globalData.isIOS,
  28. categoryList: []
  29. },
  30. /**
  31. * 生命周期函数--监听页面加载
  32. */
  33. onShow() {
  34. if (typeof this.getTabBar === 'function') {
  35. this.getTabBar().setData({
  36. selected: 1
  37. })
  38. }
  39. this.getLocUserInfo()
  40. if (Object.keys(this.data.userInfo).length > 0) {
  41. this.resetData()
  42. } else {
  43. getApp().callBack = (res) => {
  44. this.getLocUserInfo()
  45. this.resetData()
  46. }
  47. }
  48. let {
  49. desktopTips
  50. } = app.globalData
  51. if (desktopTips) {
  52. setTimeout(() => {
  53. this.setData({
  54. desktopTips: false
  55. })
  56. }, 6000)
  57. }
  58. },
  59. onUnload() {
  60. this.storeBindings.destroyStoreBindings()
  61. },
  62. async getLocUserInfo() {
  63. this.storeBindings = createStoreBindings(this, {
  64. store,
  65. fields: {
  66. userInfo: 'userInfo'
  67. },
  68. })
  69. this.storeBindings.updateStoreBindings()
  70. },
  71. loadMore() {
  72. if (this.data.currentType == '1') {
  73. this.getData(getHotrecommendList, {
  74. grade: this.data.userInfo.grade
  75. })
  76. } else if (this.data.currentType == '2') {
  77. this.getData(getAuthorityList, {
  78. grade: this.data.userInfo.grade
  79. })
  80. }
  81. this.getCategoryList()
  82. },
  83. jumpChildClassify({
  84. currentTarget
  85. }) {
  86. let firstInfo = currentTarget.dataset.item
  87. let params = firstInfo.childList.length > 0 ? `list=${encodeURIComponent(JSON.stringify(firstInfo.childList))}` : `id=${firstInfo.id}`
  88. wx.navigateTo({
  89. url: `/pages/childClassify/index?type=class&title=${firstInfo.title}&${params}`,
  90. })
  91. },
  92. jumpSearch() {
  93. wx.navigateTo({
  94. url: '/pages/childClassify/index?type=search',
  95. })
  96. },
  97. /**
  98. * 监听页面滚动事件
  99. */
  100. onPageScroll(e) {
  101. if (e.scrollTop >= 103 && !this.data.isFixed) {
  102. this.setData({
  103. isFixed: true
  104. })
  105. } else if (e.scrollTop < 103 && this.data.isFixed) {
  106. this.setData({
  107. isFixed: false
  108. })
  109. }
  110. },
  111. onReachBottom() {
  112. this.loadMore()
  113. },
  114. async getCategoryList() {
  115. let grade = this.data.userInfo.grade
  116. let categoryList = await getCategoryList({
  117. grade
  118. })
  119. this.setData({
  120. categoryList
  121. })
  122. },
  123. selectType({
  124. target
  125. }) {
  126. if (target.dataset.type) {
  127. this.setData({
  128. currentType: target.dataset.type
  129. })
  130. if (target.dataset.type != '3') {
  131. this.resetData()
  132. this.selectComponent('#worksList').resetAudio()
  133. }
  134. }
  135. },
  136. closeDesktop() {
  137. this.setData({
  138. desktopTips: false
  139. })
  140. }
  141. })