index.js 2.0 KB

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