index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. const app = getApp()
  2. import {
  3. getHotrecommendList,
  4. getCategoryList
  5. } from "~/api/works"
  6. import reachBottom from '~/mixins/reachBottom'
  7. import share from '~/mixins/share'
  8. import {
  9. createStoreBindings
  10. } from 'mobx-miniprogram-bindings'
  11. import {
  12. store
  13. } from '~/store/index'
  14. Page({
  15. behaviors: [reachBottom, share],
  16. /**
  17. * 页面的初始数据
  18. */
  19. data: {
  20. navBarHeight: app.globalData.navBarHeight,
  21. background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
  22. currentType: '1',
  23. // 控制一级分类是否固定
  24. isFixed: false,
  25. categoryList: []
  26. },
  27. /**
  28. * 生命周期函数--监听页面加载
  29. */
  30. onShow() {
  31. if (typeof this.getTabBar === 'function') {
  32. this.getTabBar().setData({
  33. selected: 1
  34. })
  35. }
  36. getApp().callBack = (res) => {
  37. this.getLocUserInfo()
  38. this.resetData()
  39. this.getCategoryList()
  40. }
  41. let uid = wx.getStorageSync('uid')
  42. /* if (uid) {
  43. this.getLocUserInfo()
  44. this.resetData()
  45. } else {
  46. getApp().callBack = (res) => {
  47. this.getLocUserInfo()
  48. this.resetData()
  49. }
  50. } */
  51. },
  52. onUnload() {
  53. this.storeBindings.destroyStoreBindings()
  54. },
  55. async getLocUserInfo() {
  56. this.storeBindings = createStoreBindings(this, {
  57. store,
  58. fields: {
  59. userInfo: 'userInfo'
  60. },
  61. })
  62. this.storeBindings.updateStoreBindings()
  63. },
  64. loadMore() {
  65. if (this.data.currentType == '1') {
  66. this.getData(getHotrecommendList, {
  67. grade: this.data.userInfo.grade
  68. })
  69. } else if (this.data.currentType == '2') {
  70. this.getData(getHotrecommendList, {
  71. grade: 'PRESCHOOL'
  72. })
  73. }
  74. },
  75. jumpChildClassify({
  76. currentTarget
  77. }) {
  78. let firstInfo = currentTarget.dataset.item
  79. let params = firstInfo.childList.length > 0 ? `list=${encodeURIComponent(JSON.stringify(firstInfo.childList))}` : `id=${firstInfo.id}`
  80. wx.navigateTo({
  81. url: `/pages/childClassify/index?type=class&title=${firstInfo.title}&${params}`,
  82. })
  83. },
  84. jumpSearch() {
  85. wx.navigateTo({
  86. url: '/pages/childClassify/index?type=search',
  87. })
  88. },
  89. /**
  90. * 监听页面滚动事件
  91. */
  92. onPageScroll(e) {
  93. if (e.scrollTop >= 103 && !this.data.isFixed) {
  94. this.setData({
  95. isFixed: true
  96. })
  97. } else if (e.scrollTop < 103 && this.data.isFixed) {
  98. this.setData({
  99. isFixed: false
  100. })
  101. }
  102. },
  103. onReachBottom() {
  104. this.loadMore()
  105. },
  106. async getCategoryList() {
  107. let grade = this.data.userInfo.grade
  108. let categoryList = await getCategoryList({
  109. grade
  110. })
  111. console.log(categoryList);
  112. this.setData({
  113. categoryList
  114. })
  115. },
  116. selectType({
  117. target
  118. }) {
  119. if (target.dataset.type) {
  120. this.setData({
  121. currentType: target.dataset.type
  122. })
  123. this.resetData()
  124. this.selectComponent('#worksList').resetAudio()
  125. }
  126. },
  127. })