book_classification.js 4.9 KB

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