index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. startPageX: 0,
  21. data: {
  22. // class为二级,search为搜索
  23. type: 'class',
  24. placeholderText: '请输入搜索内容',
  25. categoryList: [],
  26. childType: '',
  27. currentIndex: 0,
  28. scrollTop: 0,
  29. text: '',
  30. currentId: '',
  31. navBarTitle: '',
  32. historySearch: [],
  33. },
  34. /**
  35. * 生命周期函数--监听页面加载
  36. */
  37. async onLoad(options) {
  38. console.log(options, 'options');
  39. if (options.id) {
  40. let categoryList = await getCategoryLowerList(options.id)
  41. if (categoryList.length == 0) {
  42. this.setData({
  43. childType: options.id
  44. });
  45. } else {
  46. this.setData({
  47. categoryList
  48. });
  49. }
  50. this.resetData();
  51. }
  52. wx.setNavigationBarTitle({
  53. title: options.title || '搜索'
  54. });
  55. this.setData({
  56. type: options.type,
  57. historySearch: wx.getStorageSync('search'),
  58. navBarTitle: options.title
  59. });
  60. this.storeBindings = createStoreBindings(this, {
  61. store,
  62. fields: {
  63. userInfo: 'userInfo'
  64. }
  65. });
  66. // 立刻更新
  67. this.storeBindings.updateStoreBindings();
  68. setTimeout(() => {
  69. if (options.grade && !this.data.userInfo.grade) {
  70. setUserInfo({
  71. grade: options.grade
  72. }, 'put')
  73. }
  74. }, 1000)
  75. },
  76. // 获取分类的内容
  77. loadMore() {
  78. if (this.data.type == 'search') {
  79. return;
  80. }
  81. let columnId = this.data.childType ? this.data.childType : this.data.categoryList[this.data.currentIndex].id;
  82. this.getData(getCategoryWorks, {
  83. columnId
  84. });
  85. },
  86. setClass({
  87. currentTarget
  88. }) {
  89. this.setData({
  90. scrollTop: 0,
  91. navBarTitle: currentTarget.dataset.title,
  92. currentIndex: currentTarget.dataset.index,
  93. currentId: `class${currentTarget.dataset.index}`
  94. });
  95. this.resetData();
  96. },
  97. setSearch({
  98. detail
  99. }) {
  100. if (!detail.value) {
  101. this.setData({
  102. nullList: false,
  103. list: []
  104. });
  105. }
  106. this.setData({
  107. text: detail.value
  108. });
  109. },
  110. async search() {
  111. if (!this.data.text) {
  112. this.setData({
  113. list: []
  114. });
  115. return;
  116. }
  117. let list = await searchWorks({
  118. title: this.data.text,
  119. grade: this.data.userInfo.grade
  120. });
  121. if (!this.data.historySearch.includes(this.data.text)) {
  122. this.setData({
  123. historySearch: [this.data.text, ...this.data.historySearch].slice(0, 20)
  124. });
  125. wx.setStorageSync('search', this.data.historySearch);
  126. }
  127. this.setData({
  128. list,
  129. nullList: list.length == 0
  130. });
  131. },
  132. historySearch({
  133. currentTarget
  134. }) {
  135. this.setData({
  136. text: currentTarget.dataset.text
  137. });
  138. this.search();
  139. },
  140. deleteHistory({
  141. currentTarget
  142. }) {
  143. let newList = this.data.historySearch.filter(item => {
  144. return item != currentTarget.dataset.text;
  145. });
  146. this.setData({
  147. historySearch: newList.slice(0, 20)
  148. });
  149. wx.setStorageSync('search', this.data.historySearch);
  150. },
  151. clearHistory() {
  152. wx.showModal({
  153. title: '温馨提示',
  154. content: '历史记录清除后无法恢复,是否清除全部记录',
  155. success: res => {
  156. if (res.confirm) {
  157. this.setData({
  158. historySearch: []
  159. });
  160. wx.setStorageSync('search', this.data.historySearch);
  161. }
  162. }
  163. });
  164. },
  165. goRead({
  166. currentTarget
  167. }) {
  168. wx.navigateTo({
  169. url: `/pages/reading/index?videoId=${currentTarget.dataset.id}&navBarTitle=${this.data.navBarTitle || ''}`
  170. });
  171. },
  172. touchStart(e) {
  173. this.startPageX = e.changedTouches[0].pageX;
  174. },
  175. touchEnd(e) {
  176. const moveX = e.changedTouches[0].pageX - this.startPageX;
  177. if (Math.abs(moveX) >= 100) {
  178. if (moveX > 0) {
  179. if (this.data.currentIndex > 0) {
  180. let data = this.data.categoryList[this.data.currentIndex - 1]
  181. let index = data.sort - 1
  182. this.setClass({
  183. currentTarget: {
  184. dataset: {
  185. title: data.title,
  186. index
  187. }
  188. }
  189. })
  190. }
  191. } else {
  192. if (this.data.currentIndex < this.data.categoryList.length - 1) {
  193. let data = this.data.categoryList[this.data.currentIndex + 1]
  194. this.setClass({
  195. currentTarget: {
  196. dataset: {
  197. title: data.title,
  198. index: data.sort - 1
  199. }
  200. }
  201. })
  202. }
  203. }
  204. }
  205. },
  206. cleanPlaceholder() {
  207. this.setData({
  208. placeholderText: ''
  209. })
  210. },
  211. setPlaceholder() {
  212. this.setData({
  213. placeholderText: '请输入搜索内容'
  214. })
  215. },
  216. onUnload() {
  217. this.storeBindings.destroyStoreBindings();
  218. }
  219. });