book_detail.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // parents/pages/book_detail/book_detail.js
  2. import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
  3. import { BookDetailView, AddWishBooks, GetBorrowBookNum, ScanningBooks } from '../../../utils/api.js'
  4. const { navigateTo, redirectTo, navigateBack } = routers()
  5. const { globalData, hasLibraryService, hasVipService } = getApp()
  6. const { baseImgUrl, borrowToastText, reservationsToastText, wishToastText, noBorrowToastText, qrcodeInvalidToastText } = globalData
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. baseImgUrl,
  13. borrowToastText,
  14. reservationsToastText,
  15. wishToastText,
  16. isAll: 0,
  17. isLoaded: 0,
  18. isShowModal: true
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad: function (options) {
  24. options.vipId = getGlobalVal('vipId', `vipId_${getGlobalVal('userId')}`) || ''
  25. options.libId = getGlobalVal('library').id || ''
  26. this.setData({ options })
  27. this.getData(res => {
  28. this.setData({ isLoaded: 1 })
  29. })
  30. },
  31. /**
  32. * 生命周期函数--监听页面初次渲染完成
  33. */
  34. onReady: function () {
  35. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  36. for (const i in obj) {
  37. this[i] = obj[i]
  38. }
  39. },
  40. /**
  41. * 生命周期函数--监听页面显示
  42. */
  43. onShow: function () {
  44. },
  45. /**
  46. * 生命周期函数--监听页面隐藏
  47. */
  48. onHide: function () {
  49. },
  50. /**
  51. * 生命周期函数--监听页面卸载
  52. */
  53. onUnload: function () {
  54. },
  55. /**
  56. * 页面相关事件处理函数--监听用户下拉动作
  57. */
  58. onPullDownRefresh: function () {
  59. },
  60. /**
  61. * 页面上拉触底事件的处理函数
  62. */
  63. onReachBottom: function () {
  64. },
  65. /**
  66. * 用户点击右上角分享
  67. */
  68. onShareAppMessage: function () {
  69. if (isFn(sharePage)) return sharePage()
  70. },
  71. /**
  72. * 获取书籍详情
  73. */
  74. getData: function (cb) {
  75. const continuousFn = { fn: this.getData, param: { ...arguments } }
  76. const { bookInfoId } = this.data.options
  77. BookDetailView({ data: { bookInfoId }, continuousFn }).then(res => {
  78. this.setData({ ...res.data })
  79. cb && cb(res)
  80. }).catch(res => {
  81. cb && cb(res)
  82. })
  83. },
  84. /**
  85. * 收起展开全部
  86. */
  87. toggleAllConentCtl: function () {
  88. this.setData({ isAll: !this.data.isAll, isShowModal: 0 })
  89. },
  90. /**
  91. * 收起展开模态框
  92. */
  93. toggleModalCtl: function () {
  94. this.setData({ isShowModal: !this.data.isShowModal })
  95. },
  96. /**
  97. * 预约或者加入心愿单
  98. */
  99. submitCtl: function (e) {
  100. const continuousFn = { fn: this.submitCtl, param: { ...arguments } }
  101. const { type } = e.currentTarget.dataset
  102. const { canOper, options, isClick } = this.data
  103. const { isbn13, libId, vipId } = options
  104. if (canOper != 2) {
  105. wx.showToast({
  106. title: noBorrowToastText,
  107. icon: 'none'
  108. })
  109. return
  110. }
  111. if (!hasVipService()) {
  112. return
  113. }
  114. if (!hasLibraryService()) {
  115. return
  116. }
  117. if (type == 1) {
  118. this.setData({ isShowModal: true })
  119. }
  120. if (isClick) {
  121. return
  122. } else {
  123. this.setData({ isClick: true })
  124. }
  125. AddWishBooks({ data: { isbn13, libId, type, vipId }, continuousFn }).then(res => {
  126. wx.showToast({
  127. title: `${type == 3 ? '预约' : type == 2 ? '加入心愿单' : type == 1 ? '借阅' : ''}成功`
  128. })
  129. this.setData({ isShowModal: false, isClick: false })
  130. setTimeout(() => {
  131. navigateBack()
  132. }, 2000)
  133. }).catch(res => {
  134. this.setData({ isClick: false })
  135. })
  136. },
  137. /**
  138. * 调用微信扫一扫之前判断可借书多少本
  139. */
  140. scanCtl: function () {
  141. const continuousFn = { fn: this.scanCtl, param: { ...arguments } }
  142. const { vipId, library } = this.data.options
  143. const libId = library.id || ''
  144. if (!hasVipService() || !hasLibraryService()) return
  145. GetBorrowBookNum({ data: { vipId, libId }, continuousFn }).then(res => {
  146. const { totalBook, lentBook } = res.data
  147. wx.showToast({
  148. title: `您还可以借${totalBook}本书`,
  149. icon: 'none'
  150. })
  151. if (totalBook > 0) {
  152. this.scanFn()
  153. }
  154. this.setData({ ...res.data })
  155. })
  156. },
  157. /**
  158. * 调用微信扫一扫
  159. */
  160. scanFn: function () {
  161. wx.scanCode({
  162. success: res => {
  163. const url = res.result
  164. const detailsId = url.split('_')[1].split('.')[0]
  165. if (!detailsId) {
  166. wx.showToast({
  167. title: qrcodeInvalidToastText,
  168. icon: 'none'
  169. })
  170. return
  171. }
  172. this.scanningBooksFn(detailsId)
  173. }
  174. })
  175. },
  176. /**
  177. * 扫一扫借书
  178. */
  179. scanningBooksFn: function (detailsId) {
  180. const continuousFn = { fn: this.scanningBooksFn, param: { ...arguments } }
  181. const { vipId } = this.data.options
  182. ScanningBooks({ data: { detailsId, vipId }, continuousFn }).then(res => {
  183. navigateTo({ url: 'borrow', paras: { ...res.data } })
  184. }).catch(res => {
  185. wx.showToast({
  186. title: res.msg,
  187. icon: 'none'
  188. })
  189. })
  190. }
  191. })