index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. if (!detail.value) {
  86. this.setData({
  87. nullList: false
  88. })
  89. }
  90. this.setData({
  91. text: detail.value
  92. })
  93. },
  94. async search() {
  95. if (!this.data.text) {
  96. this.setData({
  97. list: []
  98. })
  99. return
  100. }
  101. let list = await searchWorks({
  102. title: this.data.text,
  103. grade: this.data.userInfo.grade
  104. })
  105. if (!this.data.historySearch.includes(this.data.text)) {
  106. this.setData({
  107. historySearch: [this.data.text, ...this.data.historySearch].slice(0, 10)
  108. })
  109. wx.setStorageSync('search', this.data.historySearch)
  110. }
  111. this.setData({
  112. list,
  113. nullList: list.length == 0
  114. })
  115. },
  116. historySearch({
  117. currentTarget
  118. }) {
  119. this.setData({
  120. text: currentTarget.dataset.text,
  121. })
  122. this.search()
  123. },
  124. deleteHistory({
  125. currentTarget
  126. }) {
  127. let newList = this.data.historySearch.filter(item => {
  128. return item != currentTarget.dataset.text
  129. })
  130. this.setData({
  131. historySearch: newList.slice(0, 10)
  132. })
  133. wx.setStorageSync('search', this.data.historySearch)
  134. },
  135. goRead({
  136. currentTarget
  137. }) {
  138. wx.navigateTo({
  139. url: `/pages/reading/index?videoId=${currentTarget.dataset.id}&navBarTitle=${this.data.navBarTitle||''}`
  140. })
  141. },
  142. onUnload() {
  143. this.storeBindings.destroyStoreBindings()
  144. },
  145. })