// parents/pages/home/home.js import { routers, viewImage, sharePage, isFn, getGlobalVal, getWxLocationSetting } from '../../../utils/util.js' import { GetLibrary, GetParentHome, ScanningBooksTwo, GoodIndex, IndexBarList, InitBorrowCar, GetMineInfo} from '../../../utils/api.js' const { navigateTo, redirectTo, navigateBack } = routers() const { globalData, hasLibraryService, hasVipService } = getApp() const { baseImgUrl, qrcodeInvalidToastText, isIphoneX, noVipListToastText } = globalData Page({ /** * 页面的初始数据 */ data: { baseImgUrl, isIphoneX, isFirst: 0, latitude: '', longitude: '', isLoaded: 0, pageNo: 1, pageSize: 10, carouselList: ['../../../assets/main_banner1.png', '../../../assets/banner1.png'], routers: [ { icon: '../../../assets/scond_banner1.png', text: '我要借书', url: 'parents/pages/borrow_car/borrow_car', zindex: 3, }, { icon: '../../../assets/scond_banner2.png', text: '借阅记录', url: 'parents/pages/borrow_record/borrow_record', zindex: 3, }, { icon: '../../../assets/scond_banner3.png', text: '图书分类', url: 'parents/pages/book_classification/book_classification', zindex: 3, }, { icon: '../../../assets/scond_banner4.png', text: '申请新书', url: 'parents/pages/apply_book/apply_book', zindex: 3, } ], topList: [], list: [], nodataArray: { text: "暂无数据" }, footerData: { isIphoneX, actIndex: 0, list: [ { text: '找好书', icon: '../../../assets/book_gray.png', actIcon: '../../../assets/book_blue.png', url: 'parents/pages/main/main', zindex: 3, }, { text: '成长轨迹', icon: '../../../assets/wish_gray.png', actIcon: '../../../assets/wish_blue.png', url: 'parents/pages/growth_track/growth_track', zindex: 3, }, { text: '我的', icon: '../../../assets/me_gray.png', actIcon: '../../../assets/me_blue.png', url: 'parents/pages/me/me', zindex: 3, } ] } }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { const userId = getGlobalVal('userId') options.vipId = getGlobalVal('vipId', `vipId_${userId}`) || '' // options.vipId ='306330bd-5c8c-41aa-8a00-0f0c296ebb29', options.libId = getGlobalVal('library').id || '' options.library = getGlobalVal('library') this.setData({ options, isFirst: 1}) if (options.isDirector==1){ this.getVipList() } if (options.library) { this.getIndexBarList() this.getList(res => { this.setData({ isLoaded: 1 }) }) } else { this.getLocation(res => { this.setData({ isLoaded: 1 }) }) } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { const obj = { navigateTo, redirectTo, navigateBack, viewImage } for (const i in obj) { this[i] = obj[i] } }, /** * 生命周期函数--监听页面显示 */ onShow: function () { const { options, isFirst } = this.data options.library = getGlobalVal('library') const userId = getGlobalVal('userId') options.vipId = getGlobalVal('vipId', `vipId_${userId}`) || '' options.libId = getGlobalVal('library').id || '' const bookList = getGlobalVal(`borrowBook_${userId}_${options.libId}_${options.vipId}`) || [] // console.log(bookList) this.setData({ options, bookList }) if (!options.library && !isFirst) { this.getLocation() } }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { // this.getList(res => { // wx.stopPullDownRefresh() // }) }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { if (isFn(sharePage)) return sharePage() }, /** * 获取会员卡列表 */ getVipList: function () { const continuousFn = { fn: this.getVipList, param: { ...arguments } } const userId = getGlobalVal('userId') const { options } = this.data const { vipId}=this.data.options const fn = (i, v) => { globalData['vip'] = v wx.setStorageSync(`vip_${userId}`, v) globalData['vipIndex'] = i wx.setStorageSync(`vipIndex_${userId}`, i) globalData['vipId'] = v['vip_id'] options.vipId  =  v['vip_id']  ||  '' this.setData({  options }) wx.setStorageSync(`vipId_${userId}`, v['vip_id']) } GetMineInfo({ continuousFn }).then(res => { const { vipList = null } = res.data if (vipList.length > 0) { let isSame = false for (let i in vipList) { if (vipList[i]['vip_id'] == vipId) { isSame = true fn(i, vipList[i]) break } } if (!isSame) { fn(0, vipList[0]) } globalData['vipList'] = vipList wx.setStorageSync(`vipList_${userId}`, vipList) } else { wx.showToast({ title: noVipListToastText, icon: 'none' }) } }) }, /** * 获取首页数据 */ getList: function (cb) { const continuousFn = { fn: this.getList, param: { ...arguments } } const libId = this.data.options.library.id || '' if (!libId) { wx.showToast({ title: '请先定位图书馆', icon: 'none' }) return } GoodIndex({ data: { libId }, continuousFn }).then(res => { const { list } = res.data const listTemp = (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 })(list) // console.log(listTemp,list) this.setData({ list: listTemp }) cb && cb(res) }).catch(res => { cb && cb(res) }) }, /** * 获取定位 */ getLocation: function (cb) { getWxLocationSetting(res => { this.setData({ isShowPosition: false }) wx.showLoading({ title: '定位中' }) wx.getLocation({ type: 'wgs84', success: res => { const { latitude, longitude } = res this.setData({ latitude, longitude }) this.getLibrary(cb) }, fail: res => { wx.showToast({ title: '定位失败', icon: 'none' }) }, complete: res => { wx.hideLoading() } }) }) }, /** * 获取图书馆 */ getLibrary: function (cb) { const continuousFn = { fn: this.getLibrary, param: { ...arguments } } const { options, latitude, longitude, pageNo, pageSize } = this.data const { vipId } = options GetLibrary({ data: { vipId, pageNo, pageSize, leagueLat: latitude, leagueLng: longitude }, continuousFn }).then(res => { const { list } = res.data if (list[0]['id']) { getApp().globalData.library = list[0] wx.setStorageSync('library', list[0]) this.setData({ 'options.library': list[0] }) this.getList(cb) } }) }, /** * 获取首页图片 */ getIndexBarList: function (cb) { const continuousFn = { fn: this.getIndexBarList, param: { ...arguments } } const { options, latitude, longitude, pageNo, pageSize } = this.data const { vipId } = options IndexBarList({ data: {}, continuousFn }).then(res => { const { imgList } = res.data this.setData({ imgList}) }) }, /** * 选择跳转进入页 */ selectRouteCtl: function (e) { const { index } = e.currentTarget.dataset const { routers } = this.data const { url, zindex } = routers[index] if(index==0){ this.scanCtl() } else if (index == 2){ navigateTo({ url, zindex }) }else{ if (!hasVipService()) return navigateTo({ url, zindex }) } }, /** * 调用微信扫一扫之前判断可借书多少本 */ scanCtl: function () { const continuousFn = { fn: this.scanCtl, param: { ...arguments } } const { library, vipId, userId } = this.data.options const libId = library.id || '' if (!hasVipService()) return InitBorrowCar({ data: { vipId, libId }, continuousFn }).then(res => { const { canBorrowNum, borrowNumTotal } = res.data wx.showToast({ title: `您还可以借${canBorrowNum}本书`, icon: 'none' }) if (this.data.bookList.length>0){ navigateTo({ url: 'parents/pages/borrow_car/borrow_car', paras: { canBorrowNum } }) return } if (canBorrowNum - this.data.bookList.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 { canBorrowNum, bookList } = this.data ScanningBooksTwo({ data: { detailsId, vipId }, continuousFn }).then(res => { const { detailsId}=res.data; for(var i in bookList){ if (detailsId == bookList[i].detailsId){ wx.showToast({ title: '此二维码已经扫过', icon: 'none' }) } } if (detailsId){ navigateTo({ url: 'parents/pages/borrow_car/borrow_car', paras: { ...res.data, canBorrowNum } }) } }).catch(res => { wx.showToast({ title: res.msg, icon: 'none' }) }) } })