index.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // 没有二级分类
  22. if (options.id) {
  23. this.setData({
  24. childType: options.id
  25. })
  26. this.resetData()
  27. } else if (options.list) {
  28. let categoryList = JSON.parse(decodeURIComponent(options.list))
  29. this.setData({
  30. categoryList
  31. })
  32. this.resetData()
  33. }
  34. wx.setNavigationBarTitle({
  35. title: options.title || '搜索'
  36. })
  37. this.setData({
  38. type: options.type,
  39. })
  40. },
  41. // 获取分类的内容
  42. loadMore() {
  43. if (this.data.type == 'search') {
  44. return
  45. }
  46. let columnId = this.data.childType ? this.data.childType : this.data.categoryList[this.data.currentIndex].id
  47. this.getData(getCategoryWorks, {
  48. columnId
  49. })
  50. },
  51. setClass({
  52. currentTarget
  53. }) {
  54. this.setData({
  55. scrollTop: 0,
  56. currentIndex: currentTarget.dataset.index
  57. })
  58. this.resetData()
  59. },
  60. setSearch({
  61. detail
  62. }) {
  63. this.setData({
  64. text: detail.value
  65. })
  66. },
  67. async search() {
  68. if (!this.data.text) {
  69. return
  70. }
  71. let res = await searchWorks({
  72. title: this.data.text,
  73. grade: 'PRIMARY_FIRST_GRADE'
  74. })
  75. let list = res.map(item => {
  76. return {
  77. readAmount: item.readAmount,
  78. ...item.userRead
  79. }
  80. })
  81. this.setData({
  82. list
  83. })
  84. },
  85. goRead({
  86. currentTarget
  87. }) {
  88. wx.navigateTo({
  89. url: `/pages/reading/index?videoId=${currentTarget.dataset.id}`
  90. })
  91. },
  92. })