index.js 4.2 KB

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