index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. let uid = wx.getStorageSync('uid')
  40. if (uid) {
  41. getApp().callBack = (res) => {
  42. this.getLocUserInfo()
  43. this.resetData()
  44. }
  45. } else {
  46. getApp().callBack = (res) => {
  47. this.getLocUserInfo()
  48. this.resetData()
  49. }
  50. }
  51. /* getApp().callBack = (res) => {
  52. this.getLocUserInfo()
  53. this.resetData()
  54. } */
  55. let {
  56. desktopTips
  57. } = app.globalData
  58. if (desktopTips) {
  59. setTimeout(() => {
  60. this.setData({
  61. desktopTips: false
  62. })
  63. }, 6000)
  64. }
  65. },
  66. onUnload() {
  67. this.storeBindings.destroyStoreBindings()
  68. },
  69. async getLocUserInfo() {
  70. this.storeBindings = createStoreBindings(this, {
  71. store,
  72. fields: {
  73. userInfo: 'userInfo'
  74. },
  75. })
  76. this.storeBindings.updateStoreBindings()
  77. },
  78. loadMore() {
  79. if (this.data.currentType == '1') {
  80. this.getData(getHotrecommendList, {
  81. grade: this.data.userInfo.grade
  82. })
  83. } else if (this.data.currentType == '2') {
  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. onReachBottom() {
  119. this.loadMore()
  120. },
  121. async getCategoryList() {
  122. let grade = this.data.userInfo.grade
  123. let categoryList = await getCategoryList({
  124. grade
  125. })
  126. this.setData({
  127. categoryList
  128. })
  129. },
  130. selectType({
  131. target
  132. }) {
  133. if (target.dataset.type) {
  134. this.setData({
  135. currentType: target.dataset.type
  136. })
  137. if (target.dataset.type != '3') {
  138. this.resetData()
  139. this.selectComponent('#worksList').resetAudio()
  140. }
  141. }
  142. },
  143. closeDesktop() {
  144. this.setData({
  145. desktopTips: false
  146. })
  147. }
  148. })