index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { getCategoryWorks, searchWorks } from '~/api/works';
  2. import reachBottom from '~/mixins/reachBottom';
  3. import { createStoreBindings } from 'mobx-miniprogram-bindings';
  4. import { store } from '~/store/index';
  5. let storeBindings;
  6. Page({
  7. behaviors: [reachBottom],
  8. data: {
  9. // class为二级,search为搜索
  10. type: 'class',
  11. categoryList: [],
  12. childType: '',
  13. currentIndex: 0,
  14. scrollTop: 0,
  15. text: '',
  16. currentId: '',
  17. navBarTitle: '',
  18. historySearch: []
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad(options) {
  24. // 没有二级分类
  25. if (options.id) {
  26. this.setData({
  27. childType: options.id
  28. });
  29. this.resetData();
  30. } else if (options.list) {
  31. let categoryList = JSON.parse(decodeURIComponent(options.list));
  32. this.setData({
  33. categoryList
  34. });
  35. this.resetData();
  36. }
  37. wx.setNavigationBarTitle({
  38. title: options.title || '搜索'
  39. });
  40. this.setData({
  41. type: options.type,
  42. historySearch: wx.getStorageSync('search'),
  43. navBarTitle: options.title
  44. });
  45. this.storeBindings = createStoreBindings(this, {
  46. store,
  47. fields: {
  48. userInfo: 'userInfo'
  49. }
  50. });
  51. // 立刻更新
  52. this.storeBindings.updateStoreBindings();
  53. },
  54. // 获取分类的内容
  55. loadMore() {
  56. if (this.data.type == 'search') {
  57. return;
  58. }
  59. let columnId = this.data.childType ? this.data.childType : this.data.categoryList[this.data.currentIndex].id;
  60. this.getData(getCategoryWorks, {
  61. columnId
  62. });
  63. },
  64. setClass({
  65. currentTarget
  66. }) {
  67. this.setData({
  68. scrollTop: 0,
  69. navBarTitle: currentTarget.dataset.title,
  70. currentIndex: currentTarget.dataset.index,
  71. currentId: `class${currentTarget.dataset.index}`
  72. });
  73. this.resetData();
  74. },
  75. setSearch({
  76. detail
  77. }) {
  78. if (!detail.value) {
  79. this.setData({
  80. nullList: false,
  81. list: []
  82. });
  83. }
  84. this.setData({
  85. text: detail.value
  86. });
  87. },
  88. async search() {
  89. if (!this.data.text) {
  90. this.setData({
  91. list: []
  92. });
  93. return;
  94. }
  95. let list = await searchWorks({
  96. title: this.data.text,
  97. grade: this.data.userInfo.grade
  98. });
  99. if (!this.data.historySearch.includes(this.data.text)) {
  100. this.setData({
  101. historySearch: [this.data.text, ...this.data.historySearch].slice(0, 20)
  102. });
  103. wx.setStorageSync('search', this.data.historySearch);
  104. }
  105. this.setData({
  106. list,
  107. nullList: list.length == 0
  108. });
  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.slice(0, 20)
  126. });
  127. wx.setStorageSync('search', this.data.historySearch);
  128. },
  129. clearHistory() {
  130. wx.showModal({
  131. title: '温馨提示',
  132. content: '历史记录清除后无法恢复,是否清除全部记录',
  133. success: res => {
  134. if (res.confirm) {
  135. this.setData({
  136. historySearch: []
  137. });
  138. wx.setStorageSync('search', this.data.historySearch);
  139. }
  140. }
  141. });
  142. },
  143. goRead({
  144. currentTarget
  145. }) {
  146. wx.navigateTo({
  147. url: `/pages/reading/index?videoId=${currentTarget.dataset.id}&navBarTitle=${this.data.navBarTitle || ''}`
  148. });
  149. },
  150. onUnload() {
  151. this.storeBindings.destroyStoreBindings();
  152. }
  153. });