// 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 { StudentList, MyInfo, ClassTeacherList, ChangeClass, ConfirmClassTeacher, DetachTeacher} 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: false, isChild:false, searchKey: '', checkNum:0, isCheck:false, pageNo: 1, pageSize: 50, 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 - 190; var scrollHeight_ = windowHeight +"px"; this.setData({scrollHeight_}) this.setData(options) // console.log(options) if (options.className){ wx.setNavigationBarTitle({ title: `${options.className}`, }) } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { const obj = { navigateTo, redirectTo, navigateBack, viewImage } for (const i in obj) { this[i] = obj[i] } }, /** * 生命周期函数--监听页面显示 */ onShow: function () { const { navIdx } = this.data; this.setData({ pageNo: 1 }) if (navIdx == 0) { this.getData(); } else { this.teacherGetData() } }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { const { navIdx, isAdd, isAddTeacher} = this.data if (navIdx==0&&!isAdd){ this.setData({ pageNo: 1 }) this.getData(res => { wx.stopPullDownRefresh() }) } // else if (isAddTeacher && navIdx == 1){ // this.setData({ pageNo: 1 }) // this.getAddTeacher(res => { // wx.stopPullDownRefresh() // }) // } }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { const { isAll, pageNo, navIdx, isAddTeacher } = this.data if (isAll) { return } wx.showLoading({ title: '加载中', }) if (navIdx==0){ this.getData() } // else if (isAddTeacher && navIdx==1){ // this.getAddTeacher() // } }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { if (app.sharePageDefaultCtl) { return app.sharePageDefaultCtl() } }, /** * 跳转 */ navigateCtl:function(e){ // const { parentVipList } = this.data var parentVipList = wx.getStorageSync('parentVipList') if (parentVipList.length>0) { app.navigateCtl(e); } else { wx.showToast({ title: '请勾选孩子后再试', icon: 'none' }) } }, /** * 底部导航跳转 */ redirectCtl: app.redirectCtl, /** * 添加老师 */ addTeacher:function(){ wx.setNavigationBarTitle({ title: `添加老师`, }) this.setData({ pageNo: 1, isAddTeacher: true,isAdd:true,list:[]}) this.getAddTeacher() }, /** * 提示框打开 */ addTeacherShow:function(e){ const {id}=e.currentTarget.dataset this.setData({ hiddenModel: true, teacherId:id }) }, /** * 提示框隐藏 */ allShowModel: function () { this.setData({ hiddenModel: false}) }, /** * 选择老师 */ selectTeacher:function(e){ const { idx } = e.currentTarget.dataset const { navIdx } = this.data this.setData({ pageNo: 1, pageSize: 10, navIdx: idx }) wx.showLoading({ title: '加载中', }) if(idx==0){ this.getData() }else{ this.teacherGetData() } }, /** * 获取老师列表 */ teacherGetData: function (cb) { const continuousFn = { fn: this.teacherGetData, param: { ...arguments } } const { classId} = this.data; var that=this ClassTeacherList({ data: { classId }, continuousFn }).then(res => { wx.hideLoading() const {list} = res.data; const tempList = (res => { for (let i in res) { res[i]['subject'] = res[i].subject.split(',') res[i]['index'] = pinyin.go(res[i].name).slice(0, 1).toUpperCase() } return res })(list || []) // console.log(list) this.setData({ teacherList: that.normalizeSinger(list), isLoaded: true }) if (isFn(cb)) cb() }).catch(res => { this.setData({ isLoaded: true }) if (isFn(cb)) cb() }) }, /** * 班级里移除老师 */ deleteTeacher: function (cb) { const continuousFn = { fn: this.deleteTeacher, param: { ...arguments } } const { classId, teacherId} = this.data; DetachTeacher({ data: { classId, teacherId}, continuousFn }).then(res => { wx.showToast({ title: '移除教师成功', icon: 'none', duration:1000 }) this.teacherGetData() this.setData({ hiddenModel:false }) if (isFn(cb)) cb() }).catch(res => { this.setData({ isLoaded: true }) if (isFn(cb)) cb() }) }, /** * 添加学生更换班级确认 */ addStudentShow:function(){ wx.setNavigationBarTitle({ title: `添加学生`, }) this.setData({ isAdd: true, isAllType: 2, isLoaded: false, pageNo: 1, list: [], searchKey:''}) this.getData(); }, /** * 添加学生更换班级确认 */ addStudent: function (cb) { const continuousFn = { fn: this.addStudent, param: { ...arguments } } const { classId, vipIds=[], className}=this.data if (vipIds.length == 0) { wx.showToast({ title: '请添加学生', icon: 'none' }) return } ChangeClass({ data: { vipIds, classId}, continuousFn }).then(res => { wx.showToast({ title: '添加学生成功', icon: 'none' }) wx.setNavigationBarTitle({ title: `${className}`, }) this.setData({ isAdd: false, isAllType: 1, searchKey: '', list: [], isLoaded: false, pageNo: 1, checkNum:0}) this.getData(); if (isFn(cb)) cb() }).catch(res => { if (isFn(cb)) cb() }) }, /** * 班级添加老师 */ addTeacherList:function(){ const continuousFn = { fn: this.addTeacherList, param: { ...arguments } } const { classId, teacherIdList=[], className } = this.data if (teacherIdList.length==0){ wx.showToast({ title: '请添加教师', icon: 'none' }) return } ConfirmClassTeacher({ data: { teacherIdList, classId }, continuousFn }).then(res => { wx.showToast({ title: '添加教师成功', icon: 'none' }) wx.setNavigationBarTitle({ title: `${className}`, }) this.setData({ isAdd: false, teacherList: [], isLoaded: false, isAddTeacher: false, pageNo: 1, checkNum: 0 }) this.teacherGetData(); if (isFn(cb)) cb() }).catch(res => { if (isFn(cb)) cb() }) }, /** * 选择续费学生 */ selectPay:function(){ this.setData({ isChild: true, isAllType: 1, pageNo: 1, searchKey:''}); this.getData(); }, /** * 续费搜索学生列表 */ payBlurCtl: function () { const { searchKey } = this.data if (!searchKey) { wx.showToast({ title: '搜索内容不能为空', icon: 'none' }) return } this.setData({ pageNo: 1, isAllType:1 }); this.getData() }, /** * 重新搜索学生列表 */ cancel:function(){ this.setData({ isAllType: 0, isChild: false, pageNo: 1 }) this.getData(); }, /** * 保存学生姓名 */ inputCtl: function (e) { const { value } = e.detail const { searchKey } = this.data if (searchKey == value) { return } this.setData({ searchKey: value }) }, /** * 重新搜索学生列表 */ blurCtl: function () { const { searchKey } = this.data this.setData({ pageNo: 1 }); this.getData() }, /** * 选择学生 */ selectLiCtl: function (e) { const { index,key} = e.currentTarget.dataset const { list, isCheck,navIdx,teacherList } = this.data console.log(navIdx,teacherList) if(navIdx==0){ list[index]['isCheck'] = list[index].isCheck == '' ? 'active' : ''; this.setData({ list}) this.studentCaculate(list) }else{ teacherList[index].items[key]['isCheck'] = teacherList[index].items[key].isCheck == '' ? 'active' : ''; this.setData({ teacherList}) this.caculate(teacherList) } }, /** * 计算学生总数和vip列表 */ studentCaculate: function (list) { let checkNum = 0 const vipIds = []; for (let i in list) { if (list[i]['isCheck'] =='active') { checkNum++ vipIds.push(list[i]['vipId']) } } this.setData({ checkNum, vipIds}) }, /** * 计算教师总数和vip列表 */ caculate: function (list) { let checkNum = 0 const vipIds = []; const teacherIdList = []; for (let i in list) { for (let j in list[i].items){ if (list[i].items[j]['isCheck'] =='active') { checkNum++ vipIds.push(list[i].items[j]['vipId']) teacherIdList.push(list[i].items[j]['teacherId']) } } } this.setData({ checkNum, vipIds, teacherIdList}) }, /** * 获取学生数据 */ getData: function (cb) { const continuousFn = { fn: this.getData, param: { ...arguments } } const { searchKey, isAllType, pageNo, pageSize, list=[], isAdd, classId}=this.data; var that=this; StudentList({ data: { searchKey, type: isAllType,classId,pageNo,pageSize}, continuousFn }).then(res => { wx.hideLoading(); // const {isCheck } = this.data; const {uploadHomeworkSum,studentSum,correctSum } = res.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, 5)+ '...' }else{ res[i].childName = res[i].childName } } return res })(temp || []) let arr = [] if (pageNo == 1) { arr = [...tempList] } else { arr = [...list, ...tempList] } this.setData({ uploadHomeworkSum,studentSum,correctSum, list: arr, studentList: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() }) }, /** * 获取添加老师列表 */ getAddTeacher: function (cb) { const continuousFn = { fn: this.getData, param: { ...arguments } } const { pageNo, pageSize, list = [],classId } = this.data; var that=this; ClassTeacherList({ data: {classId,type:1}, continuousFn }).then(res => { const { isCheck } = this.data; const temp = res.data.list; const tempList = (res => { for (let i in res) { // res[i]['isCheck'] = '' res[i]['subject'] = res[i].subject.split(',') res[i]['index'] = pinyin.go(res[i].name).slice(0, 1).toUpperCase() } return res })(temp || []) let arr = [] if (pageNo == 1) { arr = [...tempList] } else { arr = [...list, ...tempList] } this.setData({ teacherList: 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() }) }, /** * 字母导航 */ normalizeSinger(list) { // console.log(list) const { navIdx}=this.data; let map = { hot: { title: this.data.HOT_NAME, items: [] } } if (navIdx==0){ list.forEach((item, index) => { // console.log(item) var parent = /^[A-Za-z]+$/; if (!parent.test(item.index)) { map.hot.items.push({ childName: item.childName, classId: item.classId, className: item.className, grade: item.grade, imgUrl: item.imgUrl, serviceType: item.serviceType, vipId: item.vipId, phone:item.phone, homeworkStatus:item.homeworkStatus }) } const key = item.index if (!map[key]) { map[key] = { title: key, items: [] } } map[key].items.push({ childName: item.childName, classId: item.classId, className: item.className, grade: item.grade, imgUrl: item.imgUrl, serviceType: item.serviceType, vipId: item.vipId, phone: item.phone, homeworkStatus:item.homeworkStatus }) }) }else if(navIdx == 1){ list.forEach((item, index) => { var parent = /^[A-Za-z]+$/; if (!parent.test(item.index)) { map.hot.items.push({ name: item.name, phone: item.phone, subject: item.subject, imgUrl: item.imgUrl, teacherTypeList: item.teacherTypeList, teacherId: item.teacherId, isCheck: '' }) } const key = item.index if (!map[key]) { map[key] = { title: key, items: [] } } map[key].items.push({ name: item.name, phone: item.phone, subject: item.subject, imgUrl: item.imgUrl, teacherTypeList: item.teacherTypeList, teacherId: item.teacherId, 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) } })