index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. historySearch: wx.getStorageSync('search')
  26. },
  27. /**
  28. * 生命周期函数--监听页面加载
  29. */
  30. onLoad(options) {
  31. // 没有二级分类
  32. if (options.id) {
  33. this.setData({
  34. childType: options.id
  35. })
  36. this.resetData()
  37. } else if (options.list) {
  38. let categoryList = JSON.parse(decodeURIComponent(options.list))
  39. this.setData({
  40. categoryList
  41. })
  42. this.resetData()
  43. }
  44. wx.setNavigationBarTitle({
  45. title: options.title || '搜索'
  46. })
  47. this.setData({
  48. type: options.type,
  49. navBarTitle: options.title
  50. })
  51. this.storeBindings = createStoreBindings(this, {
  52. store,
  53. fields: {
  54. userInfo: 'userInfo'
  55. },
  56. })
  57. // 立刻更新
  58. this.storeBindings.updateStoreBindings()
  59. },
  60. // 获取分类的内容
  61. loadMore() {
  62. if (this.data.type == 'search') {
  63. return
  64. }
  65. let columnId = this.data.childType ? this.data.childType : this.data.categoryList[this.data.currentIndex].id
  66. this.getData(getCategoryWorks, {
  67. columnId
  68. })
  69. },
  70. setClass({
  71. currentTarget
  72. }) {
  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. console.log(list);
  97. this.setData({
  98. list,
  99. historySearch: [...this.data.historySearch, this.data.text]
  100. })
  101. wx.setStorageSync('search', this.data.historySearch)
  102. },
  103. goRead({
  104. currentTarget
  105. }) {
  106. wx.navigateTo({
  107. url: `/pages/reading/index?videoId=${currentTarget.dataset.id}&navBarTitle=${this.data.navBarTitle||''}`
  108. })
  109. },
  110. onUnload() {
  111. this.storeBindings.destroyStoreBindings()
  112. },
  113. })