index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import {
  2. getCategoryWorks,
  3. searchWorks
  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. let storeBindings
  13. Page({
  14. behaviors: [reachBottom],
  15. data: {
  16. // class为二级,search为搜索
  17. type: 'class',
  18. categoryList: [],
  19. childType: '',
  20. currentIndex: 0,
  21. scrollTop: 0,
  22. text: '',
  23. currentId: '',
  24. navBarTitle: ''
  25. },
  26. /**
  27. * 生命周期函数--监听页面加载
  28. */
  29. onLoad(options) {
  30. // 没有二级分类
  31. if (options.id) {
  32. this.setData({
  33. childType: options.id
  34. })
  35. this.resetData()
  36. } else if (options.list) {
  37. let categoryList = JSON.parse(decodeURIComponent(options.list))
  38. this.setData({
  39. categoryList
  40. })
  41. this.resetData()
  42. }
  43. wx.setNavigationBarTitle({
  44. title: options.title || '搜索'
  45. })
  46. this.setData({
  47. type: options.type,
  48. navBarTitle: options.title
  49. })
  50. this.storeBindings = createStoreBindings(this, {
  51. store,
  52. fields: {
  53. userInfo: 'userInfo'
  54. },
  55. })
  56. // 立刻更新
  57. this.storeBindings.updateStoreBindings()
  58. },
  59. // 获取分类的内容
  60. loadMore() {
  61. if (this.data.type == 'search') {
  62. return
  63. }
  64. let columnId = this.data.childType ? this.data.childType : this.data.categoryList[this.data.currentIndex].id
  65. this.getData(getCategoryWorks, {
  66. columnId
  67. })
  68. },
  69. setClass({
  70. currentTarget
  71. }) {
  72. console.log(currentTarget);
  73. this.setData({
  74. scrollTop: 0,
  75. navBarTitle: currentTarget.dataset.title,
  76. currentIndex: currentTarget.dataset.index,
  77. currentId: `class${currentTarget.dataset.index}`
  78. })
  79. this.resetData()
  80. },
  81. setSearch({
  82. detail
  83. }) {
  84. this.setData({
  85. text: detail.value
  86. })
  87. },
  88. async search() {
  89. if (!this.data.text) {
  90. return
  91. }
  92. let list = await searchWorks({
  93. title: this.data.text,
  94. grade: this.data.userInfo.grade
  95. })
  96. this.setData({
  97. list
  98. })
  99. },
  100. goRead({
  101. currentTarget
  102. }) {
  103. wx.navigateTo({
  104. url: `/pages/reading/index?videoId=${currentTarget.dataset.id}&navBarTitle=${this.data.navBarTitle||''}`
  105. })
  106. },
  107. onUnload() {
  108. this.storeBindings.destroyStoreBindings()
  109. },
  110. })