borrow_car.js 6.9 KB

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