// parents//pages/borrow_car/borrow_car.js import { routers, viewImage, sharePage, isFn, getGlobalVal, formatDateTime } from '../../../utils/util.js' import { GetBorrowBook, ScanningBooksTwo, InitBorrowCar, ConfirmBorrow } from '../../../utils/api.js' const { navigateTo, redirectTo, navigateBack } = routers() const { globalData, hasLibraryService, hasVipService } = getApp() const { baseImgUrl, qrcodeInvalidToastText } = globalData Page({ /** * 页面的初始数据 */ data: { baseImgUrl, isLoaded: 0, isLoaded:1, hiddenModel: false, deleteModel:false, isDetail:false, pageNo: 1, pageSize: 5, detailList:[], borrowTime: formatDateTime(new Date()) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { options.userId = getGlobalVal('userId') options.libId = getGlobalVal('list').length > 0 ? getGlobalVal('list')[0].libId : '' options.library = getGlobalVal('library') options.list = getGlobalVal('list') options.vipId = wx.getStorageSync("studentId") this.setData({ options }) const list = getGlobalVal(`presidentBorrowBook_${options.userId}_${options.libId}_${options.vipId}`) || [] wx.setNavigationBarTitle({ title: `借阅车`, }) const { summary, title, detailsId, img } = options if (detailsId) { var summaryText='' if (summary.length > 30) { summaryText = summary.slice(0, 30) + '...' } else { summaryText ==summary } list.unshift({ summary: summaryText, title, detailsId, img }) this.resetBorrowBook(list) } else { this.resetBorrowBook(list) } // this.getData(res => { // this.setData({ isLoaded: 1 }) // }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { const obj = { navigateTo, redirectTo, navigateBack, viewImage } for (const i in obj) { this[i] = obj[i] } }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, /** * 提示框隐藏 */ allShowModel: function () { this.setData({ hiddenModel: false, deleteModel:false}) }, /** * 提示框打开 */ showModel:function(){ this.setData({ hiddenModel: true }) }, /** * 删除打开 */ sureDelete:function(e){ const { index } = e.currentTarget.dataset this.setData({ deleteModel: true,deleteIndex:index}) }, /** * 调用微信扫一扫之前判断可借书多少本 */ scanCtl: function () { const continuousFn = { fn: this.scanCtl, param: { ...arguments } } const { vipId, libId } = this.data.options const { list } = this.data InitBorrowCar({ data: { vipId, libId }, continuousFn }).then(res => { const { canBorrowNum, borrowNumTotal } = res.data if (canBorrowNum-list.length<1){ wx.showToast({ title: `已达上限`, icon: 'none' }) return } if (canBorrowNum - list.length > 0) { this.scanFn() } this.setData({ ...res.data }) }) }, /** * 调用微信扫一扫 */ scanFn: function () { wx.scanCode({ success: res => { const url = res.result const detailsId = url.split('_')[1].split('.')[0] if (!detailsId) { wx.showToast({ title: qrcodeInvalidToastText, icon: 'none' }) return } this.scanningBooksFn(detailsId) } }) }, /** * 扫一扫借书 */ scanningBooksFn: function (detailsId) { const continuousFn = { fn: this.scanningBooksFn, param: { ...arguments } } const { vipId} = this.data.options const { list } = this.data ScanningBooksTwo({ data: { detailsId, vipId }, continuousFn }).then(res => { const { detailsId, summary, title, img } = res.data for (var i in list) { if (detailsId == list[i].detailsId) { wx.showToast({ title: '此二维码已经扫过', icon: 'none' }) return } } if (summary) { var summaryText = '' if (summary.length > 30) { summaryText = summary.slice(0, 30) + '...' } else { summaryText == summary } } console.log(list) list.unshift({ summary: summaryText, title, detailsId, img }) this.resetBorrowBook(list) }).catch(res => { wx.showToast({ title: res.msg, icon: 'none' }) }) }, /** * 删除图书 */ delBookCtl: function () { const { list, deleteIndex } = this.data list.splice(deleteIndex, 1) this.resetBorrowBook(list) this.setData({ deleteModel: false }) }, /** * 重置全局被借阅的书籍 */ resetBorrowBook: function (e) { const { userId, libId, vipId, canBorrowNum } = this.data.options const { borromNum=0}=this.data // console.log(e, canBorrowNum) wx.setStorageSync(`presidentBorrowBook_${userId}_${libId}_${vipId}`, e) this.setData({ list: e, isLoaded: 1, borromNum: canBorrowNum-e.length }) }, /** * 确定借阅以上书籍 */ submitCtl: function () { const continuousFn = { fn: this.submitCtl, param: { ...arguments } } var that=this; const { list, options } = this.data const { vipId, libId, userId} = options if (list.length == 0) { wx.showToast({ title: '请添加书籍后再借', icon: 'none' }) return } const bookDetailsId = (res => { const arr = [] for (let v of res) { arr.push(v['detailsId']) } return arr })(list) ConfirmBorrow({ data: { bookDetailsId, vipId }, continuousFn }).then(res => { const { borrowList = [] } = res.data const data = (res => { for (let i in res) { if (res[i].summary.length > 30) { res[i].summary = res[i].summary.slice(0, 30) + '...' } else { res[i].summary = res[i].summary } } return res })(borrowList) wx.showToast({ title: '借阅成功', icon: 'none' }) wx.removeStorageSync(`presidentBorrowBook_${userId}_${libId}_${vipId}`) that.setData({ isDetail: true, borrowList:data, borrowTime: formatDateTime(new Date()) }) // setTimeout(() => { // navigateTo({ url: 'parents/pages/borrow_success/borrow_success', paras: { list: JSON.stringify(borrowList), isSuccess: 1 }, zindex: 3 }) // }, 800) }) }, })