index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. console.log(this.data.userInfo);
  67. if (options.grade && !this.data.userInfo.grade) {
  68. setUserInfo({
  69. grade: options.grade
  70. }, 'put')
  71. }
  72. },
  73. // 获取分类的内容
  74. loadMore() {
  75. if (this.data.type == 'search') {
  76. return;
  77. }
  78. let columnId = this.data.childType ? this.data.childType : this.data.categoryList[this.data.currentIndex].id;
  79. this.getData(getCategoryWorks, {
  80. columnId
  81. });
  82. },
  83. setClass({
  84. currentTarget
  85. }) {
  86. this.setData({
  87. scrollTop: 0,
  88. navBarTitle: currentTarget.dataset.title,
  89. currentIndex: currentTarget.dataset.index,
  90. currentId: `class${currentTarget.dataset.index}`
  91. });
  92. this.resetData();
  93. },
  94. setSearch({
  95. detail
  96. }) {
  97. if (!detail.value) {
  98. this.setData({
  99. nullList: false,
  100. list: []
  101. });
  102. }
  103. this.setData({
  104. text: detail.value
  105. });
  106. },
  107. async search() {
  108. if (!this.data.text) {
  109. this.setData({
  110. list: []
  111. });
  112. return;
  113. }
  114. let list = await searchWorks({
  115. title: this.data.text,
  116. grade: this.data.userInfo.grade
  117. });
  118. if (!this.data.historySearch.includes(this.data.text)) {
  119. this.setData({
  120. historySearch: [this.data.text, ...this.data.historySearch].slice(0, 20)
  121. });
  122. wx.setStorageSync('search', this.data.historySearch);
  123. }
  124. this.setData({
  125. list,
  126. nullList: list.length == 0
  127. });
  128. },
  129. historySearch({
  130. currentTarget
  131. }) {
  132. this.setData({
  133. text: currentTarget.dataset.text
  134. });
  135. this.search();
  136. },
  137. deleteHistory({
  138. currentTarget
  139. }) {
  140. let newList = this.data.historySearch.filter(item => {
  141. return item != currentTarget.dataset.text;
  142. });
  143. this.setData({
  144. historySearch: newList.slice(0, 20)
  145. });
  146. wx.setStorageSync('search', this.data.historySearch);
  147. },
  148. clearHistory() {
  149. wx.showModal({
  150. title: '温馨提示',
  151. content: '历史记录清除后无法恢复,是否清除全部记录',
  152. success: res => {
  153. if (res.confirm) {
  154. this.setData({
  155. historySearch: []
  156. });
  157. wx.setStorageSync('search', this.data.historySearch);
  158. }
  159. }
  160. });
  161. },
  162. goRead({
  163. currentTarget
  164. }) {
  165. wx.navigateTo({
  166. url: `/pages/reading/index?videoId=${currentTarget.dataset.id}&navBarTitle=${this.data.navBarTitle || ''}`
  167. });
  168. },
  169. onUnload() {
  170. this.storeBindings.destroyStoreBindings();
  171. }
  172. });