index.js 2.2 KB

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