// pages/child_list/child_list.js import { routers, viewImage, sharePage, isFn, getGlobalVal, formatDateTime, formateNumber } from '../../utils/util.js' import { pinyin } from '../../utils/hz2py_full.js' import { RenewStudentList, ClassList,MyInfo2} from '../../utils/api.js' const { navigateTo, redirectTo, navigateBack } = routers() const { globalData, saveUserInfo, getWxloginCode, checkStatus } = getApp() const { baseImgUrl, thumbnail } = globalData const app = getApp() Page({ /** * 页面的初始数据 */ data: { baseImgUrl, thumbnail, statusBar: app.globalData.statusBar, isLoaded: true, isChild:false, searchKey: '', checkNum:0, classCheckNum:0, isCheck:0, classIsCheck:0, pageNo: 1, pageSize: 20, isAll: false, isAllType:1, isType:1, isAdd:false, isAddTeacher:false, hiddenModel:false, navList: ['学生续费','班级续费'], navIdx:0, HOT_NAME: '#', toView: 'A', scrollTop: 10, nodata: [ { text: '暂无学生续费' }, { text: '暂无班级续费' }, ], list: [] }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { var windowHeight = wx.getSystemInfoSync().windowHeight - 120; var scrollHeight_ = windowHeight +"px"; this.setData({scrollHeight_}) this.setData(options) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { const obj = { navigateTo, redirectTo, navigateBack, viewImage } for (const i in obj) { this[i] = obj[i] } }, /** * 生命周期函数--监听页面显示 */ onShow: function () { this.getDataNumber() const { navIdx } = this.data; this.setData({checkNum:0,classCheckNum:0,selectStudentList:[],classIdList:[]}) if (navIdx == 0) { this.getData(); } else { this.getClassList() } }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { const { navIdx} = this.data if (navIdx==0){ this.setData({ pageNo: 1 }) this.getData(res => { wx.stopPullDownRefresh() }) } }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { const { isAll, pageNo, navIdx, isAddTeacher } = this.data if (isAll) { return } wx.showLoading({ title: '加载中', }) if (navIdx==0){ this.getData() } }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { if (app.sharePageDefaultCtl) { return app.sharePageDefaultCtl() } }, /** * 跳转 */ navigateCtl:function(e){ const { navIdx,selectStudentList=[],classIdList=[]} = this.data if(navIdx==0){ if (selectStudentList.length>0) { app.navigateCtl(e); } else { wx.showToast({ title: '请勾选学生后再试', icon: 'none' }) } }else{ if (classIdList.length>0) { app.navigateCtl(e); } else { wx.showToast({ title: '请勾选班级后再试', icon: 'none' }) } } }, /** * 底部导航跳转 */ redirectCtl: app.redirectCtl, /** * 选择老师 */ selectTeacher:function(e){ const { idx } = e.currentTarget.dataset const { navIdx } = this.data this.setData({ pageNo: 1, pageSize: 10, navIdx: idx,checkNum:0,classCheckNum:0}) wx.showLoading({ title: '加载中', }) if(idx==0){ this.getData() }else{ this.getClassList() } }, /** * 获取币数 */ getDataNumber: function (cb) { const continuousFn = { fn: this.getDataNumber, param: { ...arguments } } var that=this MyInfo2({ data: {}, continuousFn }).then(res => { const { libService, platformService, libId} = res.data; that.setData({ libService, platformService, libId,isLoaded: true}) if (isFn(cb)) cb() }).catch(res => { that.setData({ isLoaded: true }) if (isFn(cb)) cb() }) }, /** * 选择学生 */ selectLiCtl: function (e) { const { index,key} = e.currentTarget.dataset const { list, isCheck,navIdx,classList } = this.data // console.log(index,classList) if(navIdx==0){ list[index].items[key]['isCheck'] = list[index].items[key].isCheck == '' ? 'active' : ''; this.setData({ list}) this.caculate(list) }else{ classList[index]['isCheck'] = classList[index].isCheck == '' ? 'active' : ''; this.setData({ classList}) this.classCaculate(classList) } }, /** * 选择全部学生和班级续费 */ checkAllCtl:function () { const { list, isCheck,navIdx,classList,classIsCheck} = this.data const temp = {} var that=this if(navIdx==0){ for (let i in list) { for (let j in list[i].items){ list[i].items[j].isCheck= isCheck != 1 ? 'active' : '' } } that.setData({list}) this.caculate(list) }else{ for (let i in classList) { temp[`classList[${i}].isCheck`] = classIsCheck != 1 ? 'active' :'' } that.setData(temp) this.classCaculate(classList) } }, /** * 计算学生总数和vip列表 */ caculate: function (list) { const { studentList=[]}=this.data let checkNum = 0 const selectStudentList = []; const vipIdList = []; for (let i in list) { for (let j in list[i].items){ if (list[i].items[j]['isCheck'] =='active') { checkNum++ vipIdList.push(list[i].items[j]['vipId']) selectStudentList.push(list[i].items[j]) } } } this.setData({ checkNum, vipIdList,selectStudentList,isCheck:studentList.length==selectStudentList.length}) }, /** * 计算班级总数和vip列表 */ classCaculate: function (list) { const { classList}=this.data let classCheckNum = 0 const selectClassList = []; const classIdList = []; for (let i in list) { if (list[i]['isCheck'] =='active') { classCheckNum++ classIdList.push(classList[i]['classId']) selectClassList.push(classList[i]) } } this.setData({ classCheckNum, selectClassList, classIdList,classIsCheck:classList.length==selectClassList.length}) }, /** * 获取学生续费数据 */ getData: function (cb) { const continuousFn = { fn: this.getData, param: { ...arguments } } const { pageNo, pageSize, list=[],studentList=[], libId}=this.data; var that=this; RenewStudentList({ data: { libId, pageNo, pageSize}, continuousFn }).then(res => { wx.hideLoading(); const {isCheck } = this.data; const temp = res.data.list; const tempList = (res => { for (let i in res) { res[i]['isCheck'] = '' res[i]['index'] = pinyin.go(res[i].childName).slice(0, 1).toUpperCase() if (res[i].childName.length>5){ res[i].childName = res[i].childName.slice(0, 3)+ '...' }else{ res[i].childName = res[i].childName } } return res })(temp || []) let arr = [] if (pageNo == 1) { arr = [...tempList] } else { arr = [...studentList, ...tempList] } this.setData({ studentList:arr, list: that.normalizeSinger(arr), pageNo: tempList.length == pageSize ? Number(pageNo) + 1 : pageNo, isAll: tempList.length < pageSize, isLoaded: true }) if (isFn(cb)) cb() }).catch(res => { this.setData({ isLoaded: true }) if (isFn(cb)) cb() }) }, /** * 获取班级续费 */ getClassList: function (cb) { const continuousFn = { fn: this.getClassList, param: { ...arguments } } const { list = [],classId } = this.data; var that=this; ClassList({ data: {}, continuousFn }).then(res => { wx.hideLoading(); const { isCheck } = this.data; const temp = res.data.list; const tempList = (res => { for (let i in res) { res[i]['isCheck'] = '' } return res })(temp || []) this.setData({ classList:tempList, isLoaded: true }) if (isFn(cb)) cb() }).catch(res => { this.setData({ isLoaded: true }) if (isFn(cb)) cb() }) }, /** * 字母导航 */ normalizeSinger(list) { let map = { hot: { title: this.data.HOT_NAME, items: [] } } list.forEach((item, index) => { var parent = /^[A-Za-z]+$/; if (!parent.test(item.index)) { map.hot.items.push({ childName: item.childName, className: item.className, endDate:item.endDate, grade: item.grade, imgUrl: item.imgUrl, serviceType: item.serviceType, vipId: item.vipId, isCheck:'' }) } const key = item.index if (!map[key]) { map[key] = { title: key, items: [] } } map[key].items.push({ childName: item.childName, className: item.className, endDate:item.endDate, grade: item.grade, imgUrl: item.imgUrl, serviceType: item.serviceType, vipId: item.vipId, isCheck: '' }) }) // console.log(map) // 为了得到有序列表,我们需要处理 map let ret = [] let hot = [] for (let key in map) { let val = map[key] if (val.title.match(/[a-zA-Z]/)) { ret.push(val) } else if (val.title === this.data.HOT_NAME) { hot.push(val) } } ret.sort((a, b) => { return a.title.charCodeAt(0) - b.title.charCodeAt(0) }) // console.log(hot, ret) if (hot[0].items.length == 0) { return ret } else { return ret.concat(hot) } }, // 点击首字母 scrollToview(e) { const { id, idx } = e.currentTarget.dataset console.log(id, idx) this.setData({ currentIndex: idx, toView: id }) wx.showToast({ title: `${id}`, icon: 'none', duration: 500 }) }, // 滚动时触发事件 viewScroll: function (e) { var newY = e.detail.scrollTop; // this.scrollY(newY); }, scrollY(newY) { const listHeight = this.data.listHeight // console.log(listHeight) // 当滚动到顶部,newY>0 if (newY == 0 || newY < 0) { this.setData({ currentIndex: 0, }) return } // 在中间部分滚动 for (let i = 0; i < listHeight.length - 1; i++) { // debugger let height1 = listHeight[i] let height2 = listHeight[i + 1] // console.log(listHeight, i) if (newY >= height1 && newY < height2) { // console.log(i) this.setData({ currentIndex: i, }) return } } // 当滚动到底部,且-newY大于最后一个元素的上限 this.setData({ currentIndex: listHeight.length - 2, }) }, /** * 返回孩子信息 */ blockInfo:function(){ const { classId} = this.data this.redirectCtl({ url: 'main', method: { classId} }, true) }, calculateHeight() { var lHeight = [], that = this; let height = 0; lHeight.push(height); var query = wx.createSelectorQuery(); query.selectAll('.list-group').boundingClientRect(function (rects) { var rect = rects, len = rect.length; for (let i = 0; i < len; i++) { height += rect[i].height; lHeight.push(height) } }).exec(); var calHeight = setInterval(function () { if (lHeight != [0]) { that.setData({ listHeight: lHeight }); clearInterval(calHeight); } }, 1000) } })