reservations.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // parents/pages/reservations/reservations.js
  2. import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
  3. import { ToReservation, AlreadyReservation, AddWishBooks } from '../../../utils/api.js'
  4. const { navigateTo, redirectTo, navigateBack } = routers()
  5. const { globalData, hasLibraryService, hasVipService } = getApp()
  6. const { baseImgUrl, qrcodeInvalidToastText } = globalData
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. baseImgUrl,
  13. isLoaded: 0,
  14. content: '',
  15. navIndex: 0,
  16. navList: [
  17. {
  18. text: '我要预约',
  19. list: [],
  20. nodataArray: {
  21. text: '暂没有预约书单',
  22. image: '../../../assets/nodata_3.png',
  23. hasNodataBtn: 1,
  24. btnText: '去书库看看'
  25. }
  26. },
  27. {
  28. text: '已预约',
  29. list: [],
  30. nodataArray: {
  31. text: '暂没有已预约书单',
  32. image: '../../../assets/nodata_2.png',
  33. hasNodataBtn: 1,
  34. btnText: '去书库看看'
  35. }
  36. }
  37. ]
  38. },
  39. /**
  40. * 生命周期函数--监听页面加载
  41. */
  42. onLoad: function (options) {
  43. options.vipId = getGlobalVal('vipId', `vipId_${getGlobalVal('userId')}`) || ''
  44. options.libId = getGlobalVal('library').id || ''
  45. this.setData({ options })
  46. this.getData(res => {
  47. this.setData({ isLoaded: 1 })
  48. })
  49. this.getDataTwo()
  50. },
  51. /**
  52. * 生命周期函数--监听页面初次渲染完成
  53. */
  54. onReady: function () {
  55. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  56. for (const i in obj) {
  57. this[i] = obj[i]
  58. }
  59. },
  60. /**
  61. * 生命周期函数--监听页面显示
  62. */
  63. onShow: function () {
  64. },
  65. /**
  66. * 生命周期函数--监听页面隐藏
  67. */
  68. onHide: function () {
  69. },
  70. /**
  71. * 生命周期函数--监听页面卸载
  72. */
  73. onUnload: function () {
  74. },
  75. /**
  76. * 页面相关事件处理函数--监听用户下拉动作
  77. */
  78. onPullDownRefresh: function () {
  79. this.getData(res => {
  80. wx.stopPullDownRefresh()
  81. })
  82. },
  83. /**
  84. * 页面上拉触底事件的处理函数
  85. */
  86. onReachBottom: function () {
  87. },
  88. /**
  89. * 用户点击右上角分享
  90. */
  91. onShareAppMessage: function () {
  92. if (isFn(sharePage)) return sharePage()
  93. },
  94. /**
  95. * 获取数据
  96. */
  97. getData: function (cb) {
  98. const continuousFn = { fn: this.getData, param: { ...arguments } }
  99. if (!hasVipService()) {
  100. cb && cb()
  101. return
  102. }
  103. if (!hasLibraryService()) {
  104. cb && cb()
  105. return
  106. }
  107. const { navIndex, navList, options, content } = this.data
  108. const { vipId, libId } = options
  109. const Fn = [ToReservation, AlreadyReservation]
  110. const data = [{ content, libId }, { vipId, libId }]
  111. Fn[navIndex]({ data: data[navIndex], continuousFn }).then(res => {
  112. navList[navIndex]['list'] = res.data.list
  113. if (navIndex == 1) {
  114. navList[navIndex]['num'] = res.data.list.length
  115. }
  116. this.setData({ navList })
  117. cb && cb()
  118. }).catch(res => {
  119. cb && cb()
  120. })
  121. },
  122. /**
  123. * 获取数据
  124. */
  125. getDataTwo: function (cb) {
  126. const continuousFn = { fn: this.getDataTwo, param: { ...arguments } }
  127. if (!hasVipService() || !hasLibraryService()) {
  128. cb && cb()
  129. return
  130. }
  131. const { navList, options } = this.data
  132. const { vipId, libId } = options
  133. AlreadyReservation({ data: { vipId, libId }, continuousFn }).then(res => {
  134. navList[1]['list'] = res.data.list
  135. this.setData({ navList })
  136. cb && cb()
  137. }).catch(res => {
  138. cb && cb()
  139. })
  140. },
  141. /**
  142. * 选择导航栏
  143. */
  144. selectNavCtl: function (e) {
  145. const { index } = e.currentTarget.dataset
  146. const { navIndex, navList } = this.data
  147. if (navIndex == index) return
  148. this.setData({ navIndex: index })
  149. if (index == 0) {
  150. if (navList[index]['list'].length == 0) {
  151. this.getData()
  152. }
  153. } else if (index == 1) {
  154. this.getData()
  155. }
  156. },
  157. /**
  158. * 输入搜索值
  159. */
  160. inputSearchCtl: function (e) {
  161. const { value } = e.detail
  162. const { content } = this.data
  163. if (content == value) return
  164. this.setData({ content: value })
  165. },
  166. /**
  167. * 清除搜索值
  168. */
  169. clearSearchCtl: function () {
  170. this.setData({ content: '' })
  171. },
  172. /**
  173. * 提交搜索值
  174. */
  175. submitSearchCtl: function () {
  176. this.getData()
  177. },
  178. /**
  179. * 预约书籍
  180. */
  181. reservationsCtl: function (e) {
  182. const continuousFn = { fn: this.reservationsCtl, param: { ...arguments } }
  183. if (!hasVipService() || !hasLibraryService()) return
  184. const { index } = e.currentTarget.dataset
  185. const { navIndex, navList, options } = this.data
  186. const { vipId, libId } = options
  187. const { isbn13 } = navList[navIndex]['list'][index]
  188. AddWishBooks({ data: { isbn13, libId, type: 3, vipId }, continuousFn }).then(res => {
  189. wx.showToast({
  190. title: '预约成功'
  191. })
  192. this.getData()
  193. })
  194. },
  195. /**
  196. * 没有数据模板按钮触发事件
  197. */
  198. nodataBtnCtl: function () {
  199. redirectTo({ url: 'parents/pages/home/home', zindex: 3 })
  200. }
  201. })