123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- // 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)
- })
- },
-
- })
|