index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.userInfo.grade) {
  70. return
  71. }
  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. if (target.dataset.type == '3') {
  128. this.selectComponent('#worksList').resetAudio()
  129. }
  130. this.setData({
  131. currentType: target.dataset.type
  132. })
  133. if (target.dataset.type != '3') {
  134. this.resetData()
  135. }
  136. }
  137. },
  138. closeDesktop() {
  139. this.setData({
  140. desktopTips: false
  141. })
  142. }
  143. })