index.js 5.8 KB

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