index.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 {
  10. store
  11. } from '~/store/index'
  12. let storeBindings
  13. Page({
  14. behaviors: [reachBottom],
  15. data: {
  16. // class为二级,search为搜索
  17. type: 'class',
  18. categoryList: [],
  19. childType: '',
  20. currentIndex: 0,
  21. scrollTop: 0,
  22. text: '',
  23. currentId: ''
  24. },
  25. /**
  26. * 生命周期函数--监听页面加载
  27. */
  28. onLoad(options) {
  29. // 没有二级分类
  30. if (options.id) {
  31. this.setData({
  32. childType: options.id
  33. })
  34. this.resetData()
  35. } else if (options.list) {
  36. let categoryList = JSON.parse(decodeURIComponent(options.list))
  37. this.setData({
  38. categoryList
  39. })
  40. this.resetData()
  41. }
  42. wx.setNavigationBarTitle({
  43. title: options.title || '搜索'
  44. })
  45. this.setData({
  46. type: options.type,
  47. })
  48. this.storeBindings = createStoreBindings(this, {
  49. store,
  50. fields: {
  51. userInfo: 'userInfo'
  52. },
  53. })
  54. // 立刻更新
  55. this.storeBindings.updateStoreBindings()
  56. },
  57. // 获取分类的内容
  58. loadMore() {
  59. if (this.data.type == 'search') {
  60. return
  61. }
  62. let columnId = this.data.childType ? this.data.childType : this.data.categoryList[this.data.currentIndex].id
  63. this.getData(getCategoryWorks, {
  64. columnId
  65. })
  66. },
  67. setClass({
  68. currentTarget
  69. }) {
  70. this.setData({
  71. scrollTop: 0,
  72. currentIndex: currentTarget.dataset.index,
  73. currentId: `class${currentTarget.dataset.index}`
  74. })
  75. this.resetData()
  76. },
  77. setSearch({
  78. detail
  79. }) {
  80. this.setData({
  81. text: detail.value
  82. })
  83. },
  84. async search() {
  85. if (!this.data.text) {
  86. return
  87. }
  88. let list = await searchWorks({
  89. title: this.data.text,
  90. grade: this.data.userInfo.grade
  91. })
  92. this.setData({
  93. list
  94. })
  95. },
  96. goRead({
  97. currentTarget
  98. }) {
  99. wx.navigateTo({
  100. url: `/pages/reading/index?videoId=${currentTarget.dataset.id}`
  101. })
  102. },
  103. onUnload() {
  104. this.storeBindings.destroyStoreBindings()
  105. },
  106. })