borrow_car.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // parents//pages/borrow_car/borrow_car.js
  2. import { routers, viewImage, sharePage, isFn, getGlobalVal, formatDateTime } from '../../../utils/util.js'
  3. import { GetBorrowBook, ScanningBooksTwo, InitBorrowCar, ConfirmBorrow } 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. isLoaded:1,
  15. hiddenModel: false,
  16. deleteModel:false,
  17. isDetail:false,
  18. pageNo: 1,
  19. pageSize: 5,
  20. detailList:[],
  21. borrowTime: formatDateTime(new Date())
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad: function (options) {
  27. options.userId = getGlobalVal('userId')
  28. options.vipId = getGlobalVal('vipId', `vipId_${options.userId}`) || ''
  29. // options.vipId = '306330bd-5c8c-41aa-8a00-0f0c296ebb29',
  30. options.libId = getGlobalVal('library').id || ''
  31. options.library = getGlobalVal('library')
  32. this.setData({ options })
  33. const list = getGlobalVal(`borrowBook_${options.userId}_${options.libId}_${options.vipId}`) || []
  34. console.log(options)
  35. const { summary, title, detailsId, img } = options
  36. if (detailsId) {
  37. var summaryText=''
  38. if (summary.length > 30) {
  39. summaryText = summary.slice(0, 30) + '...'
  40. } else {
  41. summaryText ==summary
  42. }
  43. list.unshift({ summary: summaryText, title, detailsId, img })
  44. this.resetBorrowBook(list)
  45. } else {
  46. this.resetBorrowBook(list)
  47. }
  48. // this.getData(res => {
  49. // this.setData({ isLoaded: 1 })
  50. // })
  51. },
  52. /**
  53. * 生命周期函数--监听页面初次渲染完成
  54. */
  55. onReady: function () {
  56. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  57. for (const i in obj) {
  58. this[i] = obj[i]
  59. }
  60. },
  61. /**
  62. * 生命周期函数--监听页面显示
  63. */
  64. onShow: function () {
  65. },
  66. /**
  67. * 生命周期函数--监听页面隐藏
  68. */
  69. onHide: function () {
  70. },
  71. /**
  72. * 生命周期函数--监听页面卸载
  73. */
  74. onUnload: function () {
  75. },
  76. /**
  77. * 页面相关事件处理函数--监听用户下拉动作
  78. */
  79. onPullDownRefresh: function () {
  80. },
  81. /**
  82. * 页面上拉触底事件的处理函数
  83. */
  84. onReachBottom: function () {
  85. },
  86. /**
  87. * 用户点击右上角分享
  88. */
  89. onShareAppMessage: function () {
  90. },
  91. /**
  92. * 提示框隐藏
  93. */
  94. allShowModel: function () {
  95. this.setData({ hiddenModel: false, deleteModel:false})
  96. },
  97. /**
  98. * 提示框打开
  99. */
  100. showModel:function(){
  101. this.setData({ hiddenModel: true })
  102. },
  103. /**
  104. * 删除打开
  105. */
  106. sureDelete:function(e){
  107. const { index } = e.currentTarget.dataset
  108. this.setData({ deleteModel: true,deleteIndex:index})
  109. },
  110. /**
  111. * 调用微信扫一扫之前判断可借书多少本
  112. */
  113. scanCtl: function () {
  114. const continuousFn = { fn: this.scanCtl, param: { ...arguments } }
  115. const { vipId, library } = this.data.options
  116. const { list } = this.data
  117. const libId = library.id || ''
  118. if (!hasVipService()) {
  119. return
  120. }
  121. InitBorrowCar({ data: { vipId, libId }, continuousFn }).then(res => {
  122. const { canBorrowNum, borrowNumTotal } = res.data
  123. // wx.showToast({
  124. // title: `您还可以借${totalBook}本书`,
  125. // icon: 'none'
  126. // })
  127. // console.log(totalBook - list.length)
  128. if (canBorrowNum-list.length<1){
  129. wx.showToast({
  130. title: `已达上限`,
  131. icon: 'none'
  132. })
  133. return
  134. }
  135. if (canBorrowNum - list.length > 0) {
  136. this.scanFn()
  137. }
  138. this.setData({ ...res.data })
  139. })
  140. },
  141. /**
  142. * 调用微信扫一扫
  143. */
  144. scanFn: function () {
  145. wx.scanCode({
  146. success: res => {
  147. const url = res.result
  148. const detailsId = url.split('_')[1].split('.')[0]
  149. if (!detailsId) {
  150. wx.showToast({
  151. title: qrcodeInvalidToastText,
  152. icon: 'none'
  153. })
  154. return
  155. }
  156. this.scanningBooksFn(detailsId)
  157. }
  158. })
  159. },
  160. /**
  161. * 扫一扫借书
  162. */
  163. scanningBooksFn: function (detailsId) {
  164. const continuousFn = { fn: this.scanningBooksFn, param: { ...arguments } }
  165. const { vipId} = this.data.options
  166. const { list } = this.data
  167. ScanningBooksTwo({ data: { detailsId, vipId }, continuousFn }).then(res => {
  168. const { detailsId, summary, title, img } = res.data
  169. for (var i in list) {
  170. if (detailsId == list[i].detailsId) {
  171. wx.showToast({
  172. title: '此二维码已经扫过',
  173. icon: 'none'
  174. })
  175. return
  176. }
  177. }
  178. if (summary) {
  179. var summaryText = ''
  180. if (summary.length > 30) {
  181. summaryText = summary.slice(0, 30) + '...'
  182. } else {
  183. summaryText == summary
  184. }
  185. }
  186. console.log(list)
  187. list.unshift({ summary: summaryText, title, detailsId, img })
  188. this.resetBorrowBook(list)
  189. }).catch(res => {
  190. wx.showToast({
  191. title: res.msg,
  192. icon: 'none'
  193. })
  194. })
  195. },
  196. /**
  197. * 删除图书
  198. */
  199. delBookCtl: function () {
  200. const { list, deleteIndex } = this.data
  201. list.splice(deleteIndex, 1)
  202. this.resetBorrowBook(list)
  203. this.setData({ deleteModel: false })
  204. },
  205. /**
  206. * 重置全局被借阅的书籍
  207. */
  208. resetBorrowBook: function (e) {
  209. const { userId, libId, vipId, canBorrowNum } = this.data.options
  210. const { borromNum=0}=this.data
  211. // console.log(e, canBorrowNum)
  212. wx.setStorageSync(`borrowBook_${userId}_${libId}_${vipId}`, e)
  213. this.setData({ list: e, isLoaded: 1, borromNum: canBorrowNum-e.length })
  214. },
  215. /**
  216. * 确定借阅以上书籍
  217. */
  218. submitCtl: function () {
  219. const continuousFn = { fn: this.submitCtl, param: { ...arguments } }
  220. var that=this;
  221. if (!hasVipService()) {
  222. return
  223. }
  224. const { list, options } = this.data
  225. const { vipId, libId, userId} = options
  226. if (list.length == 0) {
  227. wx.showToast({
  228. title: '请添加书籍后再借',
  229. icon: 'none'
  230. })
  231. return
  232. }
  233. const bookDetailsId = (res => {
  234. const arr = []
  235. for (let v of res) {
  236. arr.push(v['detailsId'])
  237. }
  238. return arr
  239. })(list)
  240. ConfirmBorrow({ data: { bookDetailsId, vipId }, continuousFn }).then(res => {
  241. const { borrowList = [] } = res.data
  242. const data = (res => {
  243. for (let i in res) {
  244. if (res[i].summary.length > 30) {
  245. res[i].summary = res[i].summary.slice(0, 30) + '...'
  246. } else {
  247. res[i].summary = res[i].summary
  248. }
  249. }
  250. return res
  251. })(borrowList)
  252. wx.showToast({
  253. title: '借阅成功',
  254. icon: 'none'
  255. })
  256. wx.removeStorageSync(`borrowBook_${userId}_${libId}_${vipId}`)
  257. that.setData({
  258. isDetail: true,
  259. borrowList:data
  260. })
  261. // setTimeout(() => {
  262. // navigateTo({ url: 'parents/pages/borrow_success/borrow_success', paras: { list: JSON.stringify(borrowList), isSuccess: 1 }, zindex: 3 })
  263. // }, 800)
  264. })
  265. },
  266. })