borrow_books.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // dispatcher/pages/sweeped_books/sweeped_books.js
  2. import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
  3. import { AgencyVipList, InitBorrowCar, ScanningBooksTwo} from '../../../utils/api.js'
  4. const { navigateTo, redirectTo, navigateBack } = routers()
  5. const { globalData } = getApp()
  6. const { baseImgUrl, thumbnail, qrcodeInvalidToastText } = globalData
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. baseImgUrl,
  13. thumbnail,
  14. isLoaded: false,
  15. isAllCheck: 0,
  16. isManage: 0,
  17. totalNum: 0,
  18. childName:'',
  19. ids: [],
  20. pageNo: 1,
  21. studentId:'',
  22. isDown: false,
  23. pageSize: 10,
  24. isAll: 0,
  25. totalNumbers: 0,
  26. list: [
  27. ],
  28. nodataArray: {
  29. text: "暂无图书",
  30. image: '../../../assets/nodata_1.png',
  31. margin: '400rpx auto'
  32. }
  33. },
  34. /**
  35. * 生命周期函数--监听页面加载
  36. */
  37. onLoad: function (options) {
  38. options.userId = getGlobalVal('userId')
  39. options.libId = getGlobalVal('list').length > 0 ? getGlobalVal('list')[0].libId : ''
  40. options.library = getGlobalVal('library')
  41. this.setData({ options })
  42. wx.setNavigationBarTitle({
  43. title: `代办借阅`,
  44. })
  45. this.getData(res => {
  46. this.setData({ isLoaded: 1 })
  47. })
  48. },
  49. /**
  50. * 生命周期函数--监听页面初次渲染完成
  51. */
  52. onReady: function () {
  53. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  54. for (const i in obj) {
  55. this[i] = obj[i]
  56. }
  57. },
  58. /**
  59. * 生命周期函数--监听页面显示
  60. */
  61. onShow: function () {
  62. },
  63. /**
  64. * 生命周期函数--监听页面初次渲染完成
  65. */
  66. onReady: function () {
  67. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  68. for (const i in obj) {
  69. this[i] = obj[i]
  70. }
  71. },
  72. /**
  73. * 生命周期函数--监听页面隐藏
  74. */
  75. onHide: function () {
  76. },
  77. /**
  78. * 生命周期函数--监听页面卸载
  79. */
  80. onUnload: function () {
  81. },
  82. /**
  83. * 页面相关事件处理函数--监听用户下拉动作
  84. */
  85. onPullDownRefresh: function () {
  86. this.setData({ pageNo: 1 })
  87. this.getData(res => {
  88. wx.stopPullDownRefresh()
  89. })
  90. },
  91. /**
  92. * 页面上拉触底事件的处理函数
  93. */
  94. onReachBottom: function () {
  95. const { isAll, isDown } = this.data
  96. if (isAll) {
  97. this.setData({ isDown: true })
  98. return
  99. } else {
  100. this.setData({ isLoad: true, isDown: false })
  101. this.getData(() => {
  102. this.setData({ isLoad: false })
  103. })
  104. }
  105. },
  106. /**
  107. * 用户点击右上角分享
  108. */
  109. onShareAppMessage: function () {
  110. if (isFn(sharePage)) return sharePage()
  111. },
  112. /**
  113. * 选择跳转进入页
  114. */
  115. selectNavCtl: function (e) {
  116. const { id } = e.currentTarget.dataset
  117. const { options } = this.data
  118. this.setData({ studentId: id })
  119. wx.setStorageSync('studentId', id);
  120. const bookList = getGlobalVal(`presidentBorrowBook_${options.userId}_${options.libId}_${id}`) || []
  121. console.log(`presidentBorrowBook_${options.userId}_${options.libId}_${id}`)
  122. console.log(bookList)
  123. this.setData({bookList })
  124. this.scanCtl()
  125. },
  126. /**
  127. * 调用微信扫一扫之前判断可借书多少本
  128. */
  129. scanCtl: function () {
  130. const continuousFn = { fn: this.scanCtl, param: { ...arguments } }
  131. const { libId, vipId } = this.data.options
  132. const { studentId, options } = this.data
  133. const bookList = getGlobalVal(`presidentBorrowBook_${options.userId}_${options.libId}_${studentId}`) || []
  134. console.log(`presidentBorrowBook_${options.userId}_${options.libId}_${studentId}`)
  135. console.log(bookList)
  136. InitBorrowCar({ data: { vipId: studentId, libId }, continuousFn }).then(res => {
  137. const { canBorrowNum, borrowNumTotal } = res.data
  138. wx.showToast({
  139. title: `您还可以借${canBorrowNum}本书`,
  140. icon: 'none'
  141. })
  142. // if (canBorrowNum ==0) return
  143. // // if (this.data.bookList.length > 0) {
  144. // // navigateTo({ url: 'president/pages/borrow_car/borrow_car', paras: { canBorrowNum } })
  145. // // return
  146. // // }
  147. // if (canBorrowNum > 0) {
  148. // this.scanFn()
  149. // }
  150. console.log(bookList)
  151. if (bookList.length > 0) {
  152. navigateTo({ url: 'president/pages/borrow_car/borrow_car', paras: { canBorrowNum } })
  153. return
  154. }
  155. if (canBorrowNum -bookList.length > 0) {
  156. this.scanFn()
  157. }
  158. this.setData({ ...res.data })
  159. })
  160. },
  161. /**
  162. * 调用微信扫一扫
  163. */
  164. scanFn: function () {
  165. wx.scanCode({
  166. success: res => {
  167. const url = res.result
  168. const detailsId = url.split('_')[1].split('.')[0]
  169. if (!detailsId) {
  170. wx.showToast({
  171. title: qrcodeInvalidToastText,
  172. icon: 'none'
  173. })
  174. return
  175. }
  176. this.scanningBooksFn(detailsId)
  177. }
  178. })
  179. },
  180. /**
  181. * 获取书籍数据
  182. */
  183. getData: function (cb) {
  184. const continuousFn = { fn: this.getData, param: { ...arguments } }
  185. const { pageNo, pageSize, list, childName } = this.data
  186. const { libId } = this.data.options
  187. AgencyVipList({ data: { libId, pageNo, pageSize, childName }, continuousFn }).then(res => {
  188. const { list, pages } = res.data
  189. let listTemp = []
  190. if (pageNo == 1) {
  191. listTemp = [].concat(list)
  192. } else {
  193. listTemp = [].concat(this.data.list, list)
  194. }
  195. const correctListTemp = (res => {
  196. console.log(res)
  197. var d = []
  198. for (let i in res) {
  199. res[i].number = 0
  200. }
  201. return res
  202. })(listTemp)
  203. this.setData({ list: correctListTemp, totalNumbers:pages, pageNo: list.length == pageSize ? pageNo + 1 : pageNo, isAll: list.length < pageSize })
  204. cb && cb(res)
  205. }).catch(res => {
  206. cb && cb(res)
  207. })
  208. },
  209. /**
  210. * 扫一扫借书
  211. */
  212. scanningBooksFn: function (detailsId) {
  213. const continuousFn = { fn: this.scanningBooksFn, param: { ...arguments } }
  214. const { vipId } = this.data.options
  215. const { canBorrowNum, bookList, studentId } = this.data
  216. ScanningBooksTwo({ data: { detailsId, vipId: studentId }, continuousFn }).then(res => {
  217. const { detailsId} = res.data;
  218. for (var i in bookList) {
  219. if (detailsId == bookList[i].detailsId) {
  220. wx.showToast({
  221. title: '此二维码已经扫过',
  222. icon: 'none'
  223. })
  224. }
  225. }
  226. if (detailsId) {
  227. navigateTo({ url: 'president/pages/borrow_car/borrow_car', paras: { ...res.data, canBorrowNum } })
  228. }
  229. }).catch(res => {
  230. wx.showToast({
  231. title: res.msg,
  232. icon: 'none'
  233. })
  234. })
  235. }
  236. })