book_classification.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // parents/pages/growth/growth.js
  2. import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
  3. import { BookInLibInfo , 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_1.png'
  49. }
  50. },
  51. /**
  52. * 生命周期函数--监听页面加载
  53. */
  54. onLoad: function (options) {
  55. options.libId = getGlobalVal('list').length > 0 ? getGlobalVal('list')[0].libId : ''
  56. this.setData(options)
  57. // this.getData(res => {
  58. // this.setData({ isLoaded: 1 })
  59. // })
  60. },
  61. /**
  62. * 生命周期函数--监听页面初次渲染完成
  63. */
  64. onReady: function () {
  65. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  66. for (const i in obj) {
  67. this[i] = obj[i]
  68. }
  69. },
  70. /**
  71. * 生命周期函数--监听页面显示
  72. */
  73. onShow: function () {
  74. this.getBookSort()
  75. },
  76. /**
  77. * 生命周期函数--监听页面隐藏
  78. */
  79. onHide: function () {
  80. },
  81. /**
  82. * 生命周期函数--监听页面卸载
  83. */
  84. onUnload: function () {
  85. },
  86. /**
  87. * 页面相关事件处理函数--监听用户下拉动作
  88. */
  89. onPullDownRefresh: function () {
  90. this.setData({ pageNo: 1 })
  91. this.getData(res => {
  92. wx.stopPullDownRefresh()
  93. })
  94. },
  95. /**
  96. * 页面上拉触底事件的处理函数
  97. */
  98. onReachBottom: function () {
  99. const { isAll } = this.data
  100. if (isAll) {
  101. return
  102. }
  103. this.getData()
  104. },
  105. /**
  106. * 用户点击右上角分享
  107. */
  108. onShareAppMessage: function () {
  109. if (isFn(sharePage)) return sharePage()
  110. },
  111. /**
  112. * 获取主题年龄段
  113. */
  114. getBookSort: function (cb) {
  115. const { libId } = this.data
  116. const continuousFn = { fn: this.getBookSort, param: { ...arguments } }
  117. wx.showLoading({
  118. title: '加载中',
  119. })
  120. BookInLibInfo({ data: { libId}, continuousFn }).then(res => {
  121. wx.hideLoading()
  122. const { themeList, booksSize, gradeList} = res.data
  123. themeList.unshift({ code: '', name:'全部'})
  124. gradeList.unshift({ gradeCode: '', grade: '全部' })
  125. this.setData({ themeList, gradeList, booksSize: booksSize})
  126. cb && cb()
  127. }).catch(res => {
  128. this.setData({ isLoaded: true })
  129. cb && cb()
  130. })
  131. },
  132. /**
  133. * 选择主题
  134. */
  135. selectTheme:function(e){
  136. const { code, theme, type } = e.currentTarget.dataset;
  137. this.setData({ code, isClass: false, isTheme: type, grade: type == 0 ? theme : "",theme,isLoaded:false})
  138. wx.setNavigationBarTitle({
  139. title: `${theme}`,
  140. })
  141. this.getData()
  142. },
  143. /**
  144. * 选择二级主题
  145. */
  146. selectOne: function (e) {
  147. const { code, index} = e.currentTarget.dataset;
  148. this.setData({ code, themeIdx: index, list: [], isLoaded: false, pageNo:1 })
  149. this.getData()
  150. },
  151. /**
  152. * 选择班级
  153. */
  154. selectTwo: function (e) {
  155. const { theme, index } = e.currentTarget.dataset;
  156. this.setData({ grade: theme == '全部' ? '' : theme, themeIdx: index, list: [], isLoaded: false, pageNo: 1 })
  157. this.getData()
  158. },
  159. /**
  160. * 获取数据
  161. */
  162. getData: function (cb) {
  163. const continuousFn = { fn: this.getData, param: { ...arguments } }
  164. const { pageNo, pageSize, options, list, code, grade, libId } = this.data
  165. SortSearch({ data: { pageNo, pageSize, libId, code, grade }, continuousFn }).then(res => {
  166. const newList = res.data.list
  167. const listTemp = (res => {
  168. for (let i in res) {
  169. if (res[i].summary.length > 30) {
  170. res[i].summary = res[i].summary.slice(0, 30) + '...'
  171. } else {
  172. res[i].summary = res[i].summary
  173. }
  174. }
  175. return res
  176. })(newList)
  177. let arr = []
  178. if (pageNo == 1) {
  179. arr = [...listTemp]
  180. } else {
  181. arr = [...list, ...listTemp]
  182. }
  183. this.setData({ list: arr, isAll: newList.length < pageSize, pageNo: newList.length == pageSize ? pageNo + 1 : pageNo, isLoaded: true })
  184. cb && cb()
  185. }).catch(res => {
  186. this.setData({ isLoaded: true })
  187. cb && cb()
  188. })
  189. }
  190. })