index.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const app = getApp()
  2. import {
  3. getHotrecommendList
  4. } from "~/api/works"
  5. import reachBottom from '~/mixins/reachBottom'
  6. import {
  7. createStoreBindings
  8. } from 'mobx-miniprogram-bindings'
  9. import {
  10. store
  11. } from '~/store/index'
  12. Page({
  13. behaviors: [reachBottom],
  14. /**
  15. * 页面的初始数据
  16. */
  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. },
  24. /**
  25. * 生命周期函数--监听页面加载
  26. */
  27. onShow() {
  28. if (typeof this.getTabBar === 'function') {
  29. this.getTabBar().setData({
  30. selected: 1
  31. })
  32. }
  33. this.storeBindings = createStoreBindings(this, {
  34. store,
  35. fields: {
  36. userInfo: 'userInfo'
  37. },
  38. })
  39. this.storeBindings.updateStoreBindings()
  40. if (wx.getStorageSync('uid')) {
  41. this.loadMore()
  42. } else {
  43. getApp().callBack = (res) => {
  44. this.loadMore()
  45. }
  46. }
  47. },
  48. onUnload() {
  49. this.storeBindings.destroyStoreBindings()
  50. },
  51. loadMore() {
  52. if (this.data.currentType == '1') {
  53. this.getData(getHotrecommendList, {
  54. grade: this.data.userInfo.grade
  55. })
  56. } else if (this.data.currentType == '2') {
  57. this.getData(getHotrecommendList, {
  58. grade: 'PRESCHOOL'
  59. })
  60. }
  61. },
  62. /**
  63. * 监听页面滚动事件
  64. */
  65. onPageScroll(e) {
  66. if (e.scrollTop >= 103 && !this.data.isFixed) {
  67. this.setData({
  68. isFixed: true
  69. })
  70. } else if (e.scrollTop < 103 && this.data.isFixed) {
  71. this.setData({
  72. isFixed: false
  73. })
  74. }
  75. },
  76. onReachBottom() {
  77. this.loadMore()
  78. },
  79. selectType({
  80. target
  81. }) {
  82. if (target.dataset.type) {
  83. this.setData({
  84. currentType: target.dataset.type
  85. })
  86. this.resetData()
  87. }
  88. },
  89. })