index.js 4.6 KB

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