index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. categoryList: []
  27. },
  28. /**
  29. * 生命周期函数--监听页面加载
  30. */
  31. onShow() {
  32. if (typeof this.getTabBar === 'function') {
  33. this.getTabBar().setData({
  34. selected: 1
  35. })
  36. }
  37. getApp().callBack = (res) => {
  38. this.getLocUserInfo()
  39. this.resetData()
  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(getAuthorityList, {
  71. grade: this.data.userInfo.grade
  72. })
  73. }
  74. this.getCategoryList()
  75. },
  76. jumpChildClassify({
  77. currentTarget
  78. }) {
  79. let firstInfo = currentTarget.dataset.item
  80. let params = firstInfo.childList.length > 0 ? `list=${encodeURIComponent(JSON.stringify(firstInfo.childList))}` : `id=${firstInfo.id}`
  81. wx.navigateTo({
  82. url: `/pages/childClassify/index?type=class&title=${firstInfo.title}&${params}`,
  83. })
  84. },
  85. jumpSearch() {
  86. wx.navigateTo({
  87. url: '/pages/childClassify/index?type=search',
  88. })
  89. },
  90. /**
  91. * 监听页面滚动事件
  92. */
  93. onPageScroll(e) {
  94. if (e.scrollTop >= 103 && !this.data.isFixed) {
  95. this.setData({
  96. isFixed: true
  97. })
  98. } else if (e.scrollTop < 103 && this.data.isFixed) {
  99. this.setData({
  100. isFixed: false
  101. })
  102. }
  103. },
  104. onReachBottom() {
  105. this.loadMore()
  106. },
  107. async getCategoryList() {
  108. let grade = this.data.userInfo.grade
  109. let categoryList = await getCategoryList({
  110. grade
  111. })
  112. console.log(categoryList);
  113. this.setData({
  114. categoryList
  115. })
  116. },
  117. selectType({
  118. target
  119. }) {
  120. if (target.dataset.type) {
  121. this.setData({
  122. currentType: target.dataset.type
  123. })
  124. this.resetData()
  125. this.selectComponent('#worksList').resetAudio()
  126. }
  127. },
  128. })