123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- // parents/pages/book_detail/book_detail.js
- import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
- import { BookDetailView, AddWishBooks, GetBorrowBookNum, ScanningBooks } from '../../../utils/api.js'
- const { navigateTo, redirectTo, navigateBack } = routers()
- const { globalData, hasLibraryService, hasVipService } = getApp()
- const { baseImgUrl, borrowToastText, reservationsToastText, wishToastText, noBorrowToastText, qrcodeInvalidToastText } = globalData
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- baseImgUrl,
- borrowToastText,
- reservationsToastText,
- wishToastText,
- isAll: 0,
- isLoaded: 0,
- isShowModal: true
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- options.vipId = getGlobalVal('vipId', `vipId_${getGlobalVal('userId')}`) || ''
- options.libId = getGlobalVal('library').id || ''
- this.setData({ options })
- 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 () {
- if (isFn(sharePage)) return sharePage()
- },
- /**
- * 获取书籍详情
- */
- getData: function (cb) {
- const continuousFn = { fn: this.getData, param: { ...arguments } }
- const { bookInfoId } = this.data.options
- BookDetailView({ data: { bookInfoId }, continuousFn }).then(res => {
- this.setData({ ...res.data })
- cb && cb(res)
- }).catch(res => {
- cb && cb(res)
- })
- },
- /**
- * 收起展开全部
- */
- toggleAllConentCtl: function () {
- this.setData({ isAll: !this.data.isAll, isShowModal: 0 })
- },
- /**
- * 收起展开模态框
- */
- toggleModalCtl: function () {
- this.setData({ isShowModal: !this.data.isShowModal })
- },
- /**
- * 预约或者加入心愿单
- */
- submitCtl: function (e) {
- const continuousFn = { fn: this.submitCtl, param: { ...arguments } }
- const { type } = e.currentTarget.dataset
- const { canOper, options, isClick } = this.data
- const { isbn13, libId, vipId } = options
- if (canOper != 2) {
- wx.showToast({
- title: noBorrowToastText,
- icon: 'none'
- })
- return
- }
- if (!hasVipService()) {
- return
- }
- if (!hasLibraryService()) {
- return
- }
- if (type == 1) {
- this.setData({ isShowModal: true })
- }
- if (isClick) {
- return
- } else {
- this.setData({ isClick: true })
- }
- AddWishBooks({ data: { isbn13, libId, type, vipId }, continuousFn }).then(res => {
- wx.showToast({
- title: `${type == 3 ? '预约' : type == 2 ? '加入心愿单' : type == 1 ? '借阅' : ''}成功`
- })
- this.setData({ isShowModal: false, isClick: false })
- setTimeout(() => {
- navigateBack()
- }, 2000)
- }).catch(res => {
- this.setData({ isClick: false })
- })
- },
- /**
- * 调用微信扫一扫之前判断可借书多少本
- */
- scanCtl: function () {
- const continuousFn = { fn: this.scanCtl, param: { ...arguments } }
- const { vipId, library } = this.data.options
- const libId = library.id || ''
- if (!hasVipService() || !hasLibraryService()) return
- GetBorrowBookNum({ data: { vipId, libId }, continuousFn }).then(res => {
- const { totalBook, lentBook } = res.data
- wx.showToast({
- title: `您还可以借${totalBook}本书`,
- icon: 'none'
- })
- if (totalBook > 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
- ScanningBooks({ data: { detailsId, vipId }, continuousFn }).then(res => {
- navigateTo({ url: 'borrow', paras: { ...res.data } })
- }).catch(res => {
- wx.showToast({
- title: res.msg,
- icon: 'none'
- })
- })
- }
- })
|