index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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: []
  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. historySearch: wx.getStorageSync('search'),
  50. navBarTitle: options.title
  51. })
  52. this.storeBindings = createStoreBindings(this, {
  53. store,
  54. fields: {
  55. userInfo: 'userInfo'
  56. },
  57. })
  58. // 立刻更新
  59. this.storeBindings.updateStoreBindings()
  60. },
  61. // 获取分类的内容
  62. loadMore() {
  63. if (this.data.type == 'search') {
  64. return
  65. }
  66. let columnId = this.data.childType ? this.data.childType : this.data.categoryList[this.data.currentIndex].id
  67. this.getData(getCategoryWorks, {
  68. columnId
  69. })
  70. },
  71. setClass({
  72. currentTarget
  73. }) {
  74. this.setData({
  75. scrollTop: 0,
  76. navBarTitle: currentTarget.dataset.title,
  77. currentIndex: currentTarget.dataset.index,
  78. currentId: `class${currentTarget.dataset.index}`
  79. })
  80. this.resetData()
  81. },
  82. setSearch({
  83. detail
  84. }) {
  85. this.setData({
  86. text: detail.value
  87. })
  88. },
  89. async search() {
  90. if (!this.data.text) {
  91. this.setData({
  92. list: []
  93. })
  94. return
  95. }
  96. let list = await searchWorks({
  97. title: this.data.text,
  98. grade: this.data.userInfo.grade
  99. })
  100. if (!this.data.historySearch.includes(this.data.text)) {
  101. this.setData({
  102. historySearch: [...this.data.historySearch, this.data.text]
  103. })
  104. }
  105. this.setData({
  106. list,
  107. })
  108. wx.setStorageSync('search', this.data.historySearch)
  109. },
  110. historySearch({
  111. currentTarget
  112. }) {
  113. this.setData({
  114. text: currentTarget.dataset.text
  115. })
  116. this.search()
  117. },
  118. deleteHistory({
  119. currentTarget
  120. }) {
  121. let newList = this.data.historySearch.filter(item => {
  122. return item != currentTarget.dataset.text
  123. })
  124. this.setData({
  125. historySearch: newList
  126. })
  127. wx.setStorageSync('search', newList)
  128. },
  129. goRead({
  130. currentTarget
  131. }) {
  132. wx.navigateTo({
  133. url: `/pages/reading/index?videoId=${currentTarget.dataset.id}&navBarTitle=${this.data.navBarTitle||''}`
  134. })
  135. },
  136. onUnload() {
  137. this.storeBindings.destroyStoreBindings()
  138. },
  139. })