book_classification.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // parents/pages/growth/growth.js
  2. import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
  3. import { BookSort, SortSearch} from '../../../utils/api.js'
  4. const { navigateTo, redirectTo, navigateBack } = routers()
  5. const { globalData, hasLibraryService, hasVipService } = getApp()
  6. const { baseImgUrl } = globalData
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. baseImgUrl,
  13. isLoaded: 1,
  14. pageNo: 1,
  15. pageSize: 10,
  16. isAll: 0,
  17. list: [],
  18. themeIdx:0,
  19. isClass:true,
  20. themeList: [
  21. // {
  22. // "code": "child1",
  23. // "name": "历史文化"
  24. // },
  25. // {
  26. // "code": "child2",
  27. // "name": "人文艺术"
  28. // },
  29. // {
  30. // "code": "child3",
  31. // "name": "学科素养"
  32. // },
  33. // {
  34. // "code": "child4",
  35. // "name": "想象幻想"
  36. // },
  37. // {
  38. // "code": "child4",
  39. // "name": "想象幻想"
  40. // },
  41. // {
  42. // "code": "child4",
  43. // "name": "想象幻想"
  44. // }
  45. ],
  46. nodataArray: {
  47. text: '暂没有成长记录',
  48. image: '../../../assets/nodata_2.png'
  49. }
  50. },
  51. /**
  52. * 生命周期函数--监听页面加载
  53. */
  54. onLoad: function (options) {
  55. options.userId = getGlobalVal('userId')
  56. // options.vipId = getGlobalVal('vipId', `vipId_${options.userId}`) || ''
  57. options.vipId ='ea4e93ee-8b22-41bd-bb09-ec36e88eab78'
  58. options.libId = getGlobalVal('library').id || ''
  59. this.setData({ options })
  60. // this.getData(res => {
  61. // this.setData({ isLoaded: 1 })
  62. // })
  63. },
  64. /**
  65. * 生命周期函数--监听页面初次渲染完成
  66. */
  67. onReady: function () {
  68. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  69. for (const i in obj) {
  70. this[i] = obj[i]
  71. }
  72. },
  73. /**
  74. * 生命周期函数--监听页面显示
  75. */
  76. onShow: function () {
  77. this.getBookSort()
  78. },
  79. /**
  80. * 生命周期函数--监听页面隐藏
  81. */
  82. onHide: function () {
  83. },
  84. /**
  85. * 生命周期函数--监听页面卸载
  86. */
  87. onUnload: function () {
  88. },
  89. /**
  90. * 页面相关事件处理函数--监听用户下拉动作
  91. */
  92. onPullDownRefresh: function () {
  93. this.setData({ pageNo: 1 })
  94. this.getData(res => {
  95. wx.stopPullDownRefresh()
  96. })
  97. },
  98. /**
  99. * 页面上拉触底事件的处理函数
  100. */
  101. onReachBottom: function () {
  102. const { isAll } = this.data
  103. if (isAll) {
  104. return
  105. }
  106. this.getData()
  107. },
  108. /**
  109. * 用户点击右上角分享
  110. */
  111. onShareAppMessage: function () {
  112. if (isFn(sharePage)) return sharePage()
  113. },
  114. /**
  115. * 获取主题年龄段
  116. */
  117. getBookSort: function (cb) {
  118. const continuousFn = { fn: this.getBookSort, param: { ...arguments } }
  119. wx.showLoading({
  120. title: '加载中',
  121. })
  122. BookSort({ data: {}, continuousFn }).then(res => {
  123. wx.hideLoading()
  124. const { themeList, gradeList} = res.data
  125. themeList.unshift({ code: '', name:'全部'})
  126. gradeList.unshift('全部')
  127. this.setData({ themeList, gradeList})
  128. cb && cb()
  129. }).catch(res => {
  130. this.setData({ isLoaded: true })
  131. cb && cb()
  132. })
  133. },
  134. /**
  135. * 选择主题
  136. */
  137. selectTheme:function(e){
  138. const { code, theme, type } = e.currentTarget.dataset;
  139. this.setData({ code, isClass: false, isTheme: type, grade: type == 0 ? theme : "",theme,isLoaded:false})
  140. wx.setNavigationBarTitle({
  141. title: `${theme}`,
  142. })
  143. this.getData()
  144. },
  145. /**
  146. * 选择二级主题
  147. */
  148. selectOne: function (e) {
  149. const { code, index} = e.currentTarget.dataset;
  150. this.setData({ code, themeIdx: index, list: [], isLoaded: false, pageNo:1 })
  151. this.getData()
  152. },
  153. /**
  154. * 选择班级
  155. */
  156. selectTwo: function (e) {
  157. const { theme, index } = e.currentTarget.dataset;
  158. this.setData({ grade: theme == '全部' ? '' : theme, themeIdx: index, list: [], isLoaded: false, pageNo: 1 })
  159. this.getData()
  160. },
  161. /**
  162. * 获取数据
  163. */
  164. getData: function (cb) {
  165. const continuousFn = { fn: this.getData, param: { ...arguments } }
  166. const { pageNo, pageSize, options, list, code, grade } = this.data
  167. const { libId} = options
  168. SortSearch({ data: { pageNo, pageSize, libId, code, grade }, continuousFn }).then(res => {
  169. const newList = res.data.list
  170. const listTemp = (res => {
  171. for (let i in res) {
  172. if (res[i].summary.length > 30) {
  173. res[i].summary = res[i].summary.slice(0, 30) + '...'
  174. } else {
  175. res[i].summary = res[i].summary
  176. }
  177. }
  178. return res
  179. })(newList)
  180. let arr = []
  181. if (pageNo == 1) {
  182. arr = [...listTemp]
  183. } else {
  184. arr = [...list, ...listTemp]
  185. }
  186. this.setData({ list: arr, isAll: newList.length < pageSize, pageNo: newList.length == pageSize ? pageNo + 1 : pageNo, isLoaded: true })
  187. cb && cb()
  188. }).catch(res => {
  189. this.setData({ isLoaded: true })
  190. cb && cb()
  191. })
  192. }
  193. })