borrow.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // parents/pages/borrow/borrow.js
  2. import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
  3. import { GetBorrowBook, BorrowBook, GetBorrowBookNum, ScanningBooks } 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. navIndex: 0,
  15. isSuccess: 0,
  16. navList: [
  17. {
  18. text: '本次借阅',
  19. list: [],
  20. nodataArray: {
  21. text: '暂没有本次借阅书单',
  22. image: '../../../assets/nodata_1.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.userId = getGlobalVal('userId')
  44. options.vipId = getGlobalVal('vipId', `vipId_${options.userId}`) || ''
  45. options.libId = getGlobalVal('library').id || ''
  46. this.setData({ options })
  47. const list = getGlobalVal(`borrowBook_${options.userId}_${options.libId}_${options.vipId}`) || []
  48. const { summary, author, detailsId, img } = options
  49. if (detailsId) {
  50. list.unshift({ summary, author, detailsId, img })
  51. this.resetBorrowBook(list)
  52. } else {
  53. this.resetBorrowBook(list)
  54. }
  55. this.getData(res => {
  56. this.setData({ isLoaded: 1 })
  57. })
  58. },
  59. /**
  60. * 生命周期函数--监听页面初次渲染完成
  61. */
  62. onReady: function () {
  63. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  64. for (const i in obj) {
  65. this[i] = obj[i]
  66. }
  67. },
  68. /**
  69. * 生命周期函数--监听页面显示
  70. */
  71. onShow: function () {
  72. },
  73. /**
  74. * 生命周期函数--监听页面隐藏
  75. */
  76. onHide: function () {
  77. },
  78. /**
  79. * 生命周期函数--监听页面卸载
  80. */
  81. onUnload: function () {
  82. },
  83. /**
  84. * 页面相关事件处理函数--监听用户下拉动作
  85. */
  86. onPullDownRefresh: function () {
  87. const { navIndex } = this.data
  88. if (navIndex == 1) {
  89. this.getData(res => {
  90. wx.stopPullDownRefresh()
  91. })
  92. }
  93. },
  94. /**
  95. * 页面上拉触底事件的处理函数
  96. */
  97. onReachBottom: function () {
  98. },
  99. /**
  100. * 用户点击右上角分享
  101. */
  102. onShareAppMessage: function () {
  103. if (isFn(sharePage)) return sharePage()
  104. },
  105. /**
  106. * 获取已借阅的书籍
  107. */
  108. getData: function (cb) {
  109. const continuousFn = { fn: this.getData, param: { ...arguments } }
  110. const { navIndex, navList, options } = this.data
  111. const { vipId, libId } = options
  112. if (!hasVipService()) {
  113. cb && cb()
  114. return
  115. }
  116. if (!hasLibraryService()) {
  117. cb && cb()
  118. return
  119. }
  120. GetBorrowBook({ data: { vipId, libId }, continuousFn }).then(res => {
  121. this.setData({ 'navList[1].list': res.data.list })
  122. cb && cb(res)
  123. }).catch(res => {
  124. cb && cb(res)
  125. })
  126. },
  127. /**
  128. * 选择导航栏
  129. */
  130. selectNavCtl: function (e) {
  131. const { index } = e.currentTarget.dataset
  132. const { navIndex, navList } = this.data
  133. if (navIndex == index) {
  134. return
  135. }
  136. this.setData({ navIndex: index })
  137. if (index == 1) {
  138. this.getData(res => {
  139. this.setData({ isLoaded: 1 })
  140. })
  141. }
  142. },
  143. /**
  144. * 没有数据模板按钮触发事件
  145. */
  146. nodataBtnCtl: function () {
  147. this.scanCtl()
  148. },
  149. /**
  150. * 调用微信扫一扫之前判断可借书多少本
  151. */
  152. scanCtl: function () {
  153. const continuousFn = { fn: this.scanCtl, param: { ...arguments } }
  154. const { vipId, library } = this.data.options
  155. const libId = library.id || ''
  156. if (!hasVipService()) {
  157. return
  158. }
  159. if (!hasLibraryService()) {
  160. return
  161. }
  162. GetBorrowBookNum({ data: { vipId, libId }, continuousFn }).then(res => {
  163. const { totalBook, lentBook } = res.data
  164. wx.showToast({
  165. title: `您还可以借${totalBook}本书`,
  166. icon: 'none'
  167. })
  168. if (totalBook > 0) {
  169. this.scanFn()
  170. }
  171. this.setData({ ...res.data })
  172. })
  173. },
  174. /**
  175. * 调用微信扫一扫
  176. */
  177. scanFn: function () {
  178. wx.scanCode({
  179. success: res => {
  180. const url = res.result
  181. const detailsId = url.split('_')[1].split('.')[0]
  182. if (!detailsId) {
  183. wx.showToast({
  184. title: qrcodeInvalidToastText,
  185. icon: 'none'
  186. })
  187. return
  188. }
  189. this.scanningBooksFn(detailsId)
  190. }
  191. })
  192. },
  193. /**
  194. * 扫一扫借书
  195. */
  196. scanningBooksFn: function (detailsId) {
  197. const continuousFn = { fn: this.scanningBooksFn, param: { ...arguments } }
  198. const { vipId } = this.data.options
  199. ScanningBooks({ data: { detailsId, vipId }, continuousFn }).then(res => {
  200. const { detailsId } = res.data
  201. if (detailsId) {
  202. list.unshift({ ...res.data })
  203. this.resetBorrowBook(list)
  204. }
  205. }).catch(res => {
  206. wx.showToast({
  207. title: res.msg,
  208. icon: 'none'
  209. })
  210. })
  211. },
  212. /**
  213. * 删除图书
  214. */
  215. delBookCtl: function (e) {
  216. const { navIndex, navList } = this.data
  217. const { index } = e.currentTarget.dataset
  218. const { list } = navList[navIndex]
  219. if (navIndex != 0) {
  220. return
  221. }
  222. list.splice(index, 1)
  223. this.resetBorrowBook(list)
  224. },
  225. /**
  226. * 重置全局被借阅的书籍
  227. */
  228. resetBorrowBook: function (e) {
  229. const { userId, libId, vipId } = this.data.options
  230. console.log({ userId, libId, vipId })
  231. wx.setStorageSync(`borrowBook_${userId}_${libId}_${vipId}`, e)
  232. this.setData({ 'navList[0].list': e, isLoaded: 1 })
  233. },
  234. /**
  235. * 确定借阅以上书籍
  236. */
  237. submitCtl: function () {
  238. const continuousFn = { fn: this.submitCtl, param: { ...arguments } }
  239. if (!hasVipService()) {
  240. return
  241. }
  242. const { navList, navIndex, options } = this.data
  243. const { vipId } = options
  244. const { list } = navList[navIndex]
  245. if (list.length == 0) {
  246. wx.showToast({
  247. title: '请添加书籍后再借',
  248. icon: 'none'
  249. })
  250. return
  251. }
  252. const bookDetailsId = (res => {
  253. const arr = []
  254. for (let v of res) {
  255. arr.push(v['detailsId'])
  256. }
  257. return arr
  258. })(list)
  259. BorrowBook({ data: { bookDetailsId, vipId }, continuousFn }).then(res => {
  260. const { borrowList = [] } = res.data
  261. wx.showToast({
  262. title: '借阅成功',
  263. icon: 'none'
  264. })
  265. setTimeout(() => {
  266. navigateTo({ url: 'parents/pages/borrow_success/borrow_success', paras: { list: JSON.stringify(borrowList), isSuccess: 1 }, zindex: 3 })
  267. }, 800)
  268. })
  269. }
  270. })