index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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: false,
  27. isIOS: false,
  28. categoryList: []
  29. },
  30. /**
  31. * 生命周期函数--监听页面加载
  32. */
  33. onShow() {
  34. if (typeof this.getTabBar === 'function') {
  35. this.getTabBar().setData({
  36. selected: 1
  37. })
  38. }
  39. getApp().callBack = (res) => {
  40. this.getLocUserInfo()
  41. this.resetData()
  42. }
  43. // 1023 安卓系统桌面图标,1104微信聊天主界面下拉,「我的小程序」栏(基础库2.2.4-2.29.0版本废弃,2.29.1版本起生效)
  44. let {
  45. isIOS,
  46. scene
  47. } = app.globalData
  48. let desktopTips = isIOS ? scene != 1104 : scene != 1023
  49. if (desktopTips) {
  50. this.setData({
  51. isIOS,
  52. desktopTips
  53. })
  54. setTimeout(() => {
  55. this.setData({
  56. desktopTips: false
  57. })
  58. }, 8000)
  59. }
  60. let uid = wx.getStorageSync('uid')
  61. /* if (uid) {
  62. this.getLocUserInfo()
  63. this.resetData()
  64. } else {
  65. getApp().callBack = (res) => {
  66. this.getLocUserInfo()
  67. this.resetData()
  68. }
  69. } */
  70. },
  71. onUnload() {
  72. this.storeBindings.destroyStoreBindings()
  73. },
  74. async getLocUserInfo() {
  75. this.storeBindings = createStoreBindings(this, {
  76. store,
  77. fields: {
  78. userInfo: 'userInfo'
  79. },
  80. })
  81. this.storeBindings.updateStoreBindings()
  82. },
  83. loadMore() {
  84. if (this.data.currentType == '1') {
  85. this.getData(getHotrecommendList, {
  86. grade: this.data.userInfo.grade
  87. })
  88. } else if (this.data.currentType == '2') {
  89. this.getData(getAuthorityList, {
  90. grade: this.data.userInfo.grade
  91. })
  92. }
  93. this.getCategoryList()
  94. },
  95. jumpChildClassify({
  96. currentTarget
  97. }) {
  98. let firstInfo = currentTarget.dataset.item
  99. let params = firstInfo.childList.length > 0 ? `list=${encodeURIComponent(JSON.stringify(firstInfo.childList))}` : `id=${firstInfo.id}`
  100. wx.navigateTo({
  101. url: `/pages/childClassify/index?type=class&title=${firstInfo.title}&${params}`,
  102. })
  103. },
  104. jumpSearch() {
  105. wx.navigateTo({
  106. url: '/pages/childClassify/index?type=search',
  107. })
  108. },
  109. /**
  110. * 监听页面滚动事件
  111. */
  112. onPageScroll(e) {
  113. if (e.scrollTop >= 103 && !this.data.isFixed) {
  114. this.setData({
  115. isFixed: true
  116. })
  117. } else if (e.scrollTop < 103 && this.data.isFixed) {
  118. this.setData({
  119. isFixed: false
  120. })
  121. }
  122. },
  123. onReachBottom() {
  124. this.loadMore()
  125. },
  126. async getCategoryList() {
  127. let grade = this.data.userInfo.grade
  128. let categoryList = await getCategoryList({
  129. grade
  130. })
  131. this.setData({
  132. categoryList
  133. })
  134. },
  135. selectType({
  136. target
  137. }) {
  138. if (target.dataset.type) {
  139. this.setData({
  140. currentType: target.dataset.type
  141. })
  142. if (target.dataset.type != '3') {
  143. this.resetData()
  144. this.selectComponent('#worksList').resetAudio()
  145. }
  146. }
  147. },
  148. closeDesktop() {
  149. this.setData({
  150. desktopTips: false
  151. })
  152. }
  153. })