index.js 3.3 KB

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