// pages/piano/piano.js const app = getApp() const util = require("../../utils/util") const urlDef = require("../../utils/urls") Page({ /** * 页面的初始数据 */ data: { nickName: '***', height: 400, firstLoad: wx.getStorageSync('firstLoad'), listHeight: 0, btnTop: 0, toTop: 0, animationBtn: {}, animationChange: {}, childTitle: '预约练琴', beginDate: null, endDate: null, list: [], loading: '加载中...', flag: 0, pageNum: 0, hasNextPage: true, type: '', ruleShow: false, bgShow: false, curStuId: '', orgIndex: 0, beginDate: '2020-01-01', endDate: '2021-02-02', initDate: '2020-01-01', lastDate: '', yFlag: false, animationBox: {}, animationBg: {}, typeArr: ['全部', '已预约', '排队中'], typeIndex: 0, str: {}, selectHeight: 1000, }, getCurStu: function (e) { let curStuId = e.currentTarget.dataset.id this.setData({ curStuId }) }, ruleShow: function () { if (this.data.ruleShow) { this.setData({ ruleShow: false, }) } else { this.setData({ ruleShow: true, }) } }, getPiano: function () { wx.navigateTo({ url: '/pages/makePiano/makePiano', }) }, autoLogin: function () { var openId = wx.getStorageSync('openId') util.doPost( 'autoLogin', { openId, } ).then(rs => { if (rs.success > 0) { wx.setStorageSync('sso-token', rs.data.token); this.loadIndexData() // this.queryList(); } else { wx.showToast({ title: rs.errMsg, icon: 'none', duration: 3000 }) } }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { var query = wx.createSelectorQuery(); query.select('#change-view').boundingClientRect() query.exec(res => { this.setData({ selectHeight: res[0].height, }) }) // 显示近7天的预约记录 let now = new Date(); let bd = util.formatTime(now).substring(0, 10).replace(/\//g, '-') let edd = new Date(now.setDate(now.getDate() + 7)); let ed = util.formatTime(edd).substring(0, 10).replace(/\//g, '-') this.setData({ beginDate: bd, endDate: ed, list: [] }); // end this.initDate() }, initDate: function () { var date = new Date() const year = date.getFullYear() const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1 const day = date.getDate() + 1 < 10 ? '0' + date.getDate() : date.getDate() let now = new Date(); let edd = new Date(now.setDate(now.getDate() + 7)); let ed = util.formatTime(edd).substring(0, 10).replace(/\//g, '-') this.setData({ endDate: year + '-' + month + '-' + day, lastDate: year + '-' + month + '-' + day, initChangeDate: ed }) const token = wx.getStorageSync('sso-token') if (!token) { this.autoLogin() } else { this.loadIndexData() } if (this.data.firstLoad) { this.showChange() this.setData({ bgShow: true }) } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () {}, /** * 生命周期函数--监听页面显示 */ onShow: function () { this.setData({ firstLoad: wx.getStorageSync('firstLoad') }) if (!this.data.firstLoad) { this.setData({ list: [] }) this.queryList() } }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { wx.setStorageSync('firstLoad', true) }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, // onPageScroll:function(e){ // const that = this // that.hideBtn() // that.setData({ // scrollTop:e.scrollTop, // scrollIng:true // }) // let timer= setTimeout(()=>{ // if(that.data.scrollTop===e.scrollTop){ // that.setData({ // scrollTop:e.scrollTop, // scrollIng:false // }) // console.log('滚动结束') // that.showBtn() // clearTimeout(timer) // } // },1000) // }, getChange: function () { if (!this.data.bgShow) { this.setData({ yFlag: true }) this.showSelect() this.showChange() } else { this.hideChange() } this.setData({ bgShow: !this.data.bgShow }) }, showChange: function () { var animationChange = wx.createAnimation({ timingFunction: 'linear', duration: 600 }) this.animationChange = animationChange let right = this.data.height animationChange.translateY(right).step() this.setData({ animationChange: animationChange.export(), }) }, hideChange: function () { var animationChange = wx.createAnimation({ timingFunction: 'linear', duration: 800 }) this.animationChange = animationChange let right = -this.data.height animationChange.translateY(right).step() this.setData({ animationChange: animationChange.export(), }) }, changeStuBtn: function () { this.setData({ bgShow: false }) this.hideChange() }, hideBtn: function () { //淡化切换学生按钮 var animationBtn = wx.createAnimation({ timingFunction: 'linear', duration: 10000 }) this.animationBtn = animationBtn let opacity = 0.3 animationBtn.opacity(opacity).step() this.setData({ animationBtn: animationBtn.export() }) }, showBtn: function () { //正常显示学生切换按钮 var animationBtn = wx.createAnimation({ timingFunction: 'linear', duration: 800 }) this.animationBtn = animationBtn let opacity = 1 animationBtn.opacity(opacity).step() this.setData({ animationBtn: animationBtn.export() }) }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { if (this.data.hasNextPage) { let pn = this.data.pageNum this.setData({ loading: '加载中', 'pageNum': ++pn }) this.queryList() } }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, getDates: function (e) { this.setData({ list: [], pageNum: 0, beginDate: e[0], endDate: e[1], type: this.data.typeIndex == 0 ? '' : this.data.typeIndex, loading: '加载中' }) this.queryList() }, queryList: function () { const urls = urlDef.urls; let stu = wx.getStorageSync('student'); if (stu) { let params = { 'q.beginDate': this.data.beginDate, 'q.endDate': this.data.endDate, 'q.studentId': stu.studentId, 'q.type': this.data.type } params.pageNum = this.data.pageNum util.apiPost(urls.query_appointment_list, params).then(rs => { let list = rs.list list.map(o => { o.visitDate = o.visitDate.substring(0, 10) o.cancelFlag = this.canCancel(o) // 是否可以取消 }) this.setData({ 'hasNextPage': rs.hasNextPage, 'loading': rs.hasNextPage ? '上拉加载' : '没有更多数据', 'list': this.data.list.concat(list) }) if (this.data.firstLoad) { this.getCount() } }) } }, getCount: function () { const urls = urlDef.urls; let stu = wx.getStorageSync('student'); util.apiPost(urls.query_appointment_count, { 'q.studentId': stu.studentId }).then(rs => { if (rs[0] <= 0) { this.setData({ ruleShow: true }) } }) }, loadIndexData: function () { this.setData({ 'stuShowList': [], 'teaShowList': [], }) const urls = urlDef.urls; //获取登录用户数据, 并加载首页数据 util.apiPost(urls.get_cur_user, {}).then(rs => { // console.log(JSON.stringify(rs)); let userItems = rs.attr.others let orgItems = [] let student = {} userItems.forEach(u => { let exists = false; orgItems.some(o => { if (o.orgId === u.orgId) { exists = true; return true; } }) if (!exists) { orgItems.push({ orgId: u.orgId, orgName: u.orgName }) } if (u.studentId === rs.attr.personId) { // 当前用户 const headImg = u.imageUrl ? urls.oss_file + 'image/' + u.imageUrl : '/images/head.png'; student = { studentId: u.studentId, orgId: u.orgId, orgName: u.orgName, name: u.studentName, sex: u.sex, headImg: headImg } this.setData({ 'curStu': student, }) // 放入 strong中 wx.setStorageSync('student', student); } }) for (var i in userItems) { userItems[i].headName = userItems[i].studentName.substring(userItems[i].studentName.length - 1, userItems[i].studentName.length) userItems[i].nickName = userItems[i].studentName.substring(0, 1) + "*" + userItems[i].studentName.substring(userItems[i].studentName.length - 1, userItems[i].studentName.length) } for (var j in orgItems) { if (this.data.curStu.orgId == orgItems[j].orgId) { const tempName = this.data.curStu.name.substring(0, 1) + "*" + this.data.curStu.name.substring( this.data.curStu.name.length - 1, this.data.curStu.name.length) this.setData({ orgIndex: j, curStuId: this.data.curStu.studentId, nickName: tempName }) break } } this.setData({ 'userItems': userItems, 'orgItems': orgItems }) this.queryList() }); }, //切换校区 bindPickerChangeSchool: function (e) { let i = e.detail.value; if (i == this.data.orgIndex) { console.log('无须切换...'); return; } // 取该校区 第1位学生 作为默认登录用户 let targetId = this.data.orgItems[i].orgId; this.data.userItems.some(u => { if (u.orgId === targetId) { this.doChangeUser(u.studentId); this.setData({ orgIndex: i, sid: u.studentId, list: [], loading: '加载中...', firstLoad: true }); return true; } }); }, //切换身份 changeStu: function (e) { let sid = e.currentTarget.dataset.id; this.doChangeUser(sid); }, doChangeUser: function (sid) { if (this.data.curStu.studentId == sid) { console.log('无须切换') return; } wx.showLoading({ title: '正在切换用户', mask: true }) const urls = urlDef.urls; util.apiPost(urls.change_user, { targetId: sid }).then(rs => { wx.hideLoading(); if (rs === 1) { console.log('切换成功'); this.setData({ curStuId: sid, list: [], loading: '加载中...', firstLoad: true }) this.loadIndexData(); } else { wx.showToast({ title: '切换失败,请稍后重试', icon: 'none' }) } }); }, canCancel(item) { // 判断是否能取消, 未到预约时间前都能取消,至少提前1个小时 let t = item.visitDate + ' ' + item.beginTime + ':00'; let vdt = new Date(t.replace(/-/g, '/')); let tdt = new Date().getTime(); if (vdt < tdt) { return false; } // const dayTime = 86400000; const dayTime = 3600000; return item.attend == 0 && (vdt - tdt) >= dayTime }, doCancelQueue: function (e) { let id = e.currentTarget.dataset.id let that = this wx.showModal({ title: '确认信息', content: '确定要取消本次排队?', showCancel: true, cancelText: "否", confirmText: "是", success: function (res) { if (res.cancel) {} else { wx.showLoading({ title: '正在取消', mask: true }) const urls = urlDef.urls; util.apiPost(urls.delete_queue, { 'ids[]': [id] }).then(rs => { if (rs.message) { wx.showToast({ title: rs.message, icon: 'none' }) return } wx.showToast({ title: '取消成功', icon: 'success' }) that.setData({ list: [], pageNum: 0, loading: '加载中' }) that.queryList() }) } } }) }, doCancel: function (e) { let id = e.currentTarget.dataset.id let that = this wx.showModal({ title: '确认信息', content: '确定要取消本次预约?', showCancel: true, cancelText: "否", confirmText: "是", success: function (res) { if (res.cancel) {} else { wx.showLoading({ title: '正在取消', mask: true }) that.doRealCancel(id) } } }) }, doRealCancel: function (id) { const urls = urlDef.urls; util.apiPost(urls.cancel_appointment + '?id=' + id).then(rs => { if (rs.message) { wx.showToast({ title: rs.message, icon: 'none' }) return } wx.showToast({ title: '取消成功', icon: 'success' }) this.setData({ list: [], pageNum: 0, loading: '加载中' }) this.queryList() }) }, bindBeginDateChange: function (e) { if (e.detail.value > this.data.endDate) { this.setData({ beginDate: this.data.endDate }) } else { this.setData({ beginDate: e.detail.value }) } }, bindEndDateChange: function (e) { if (e.detail.value > this.data.initChangeDate) { this.setData({ endDate: this.data.initChangeDate }) } else { this.setData({ endDate: e.detail.value }) } }, resetDate: function () { this.setData({ beginDate: this.data.initDate, endDate: this.data.initChangeDate, typeIndex: 0 }) const date = new Date() const dateArr = [date.getFullYear() + "-01" + "-01", this.data.initChangeDate]; const typeIndex = this.data.typeIndex this.getDates(dateArr) // this.triggerEvent("getDates", {dateArr,typeIndex}); this.showSelect() }, submitDate: function () { const dateArr = [this.data.beginDate, this.data.endDate]; // const typeIndex = this.data.typeIndex // this.triggerEvent("getDates", {dateArr,typeIndex}); this.getDates(dateArr) this.showSelect() }, showSelect: function () { var animation = wx.createAnimation({ timingFunction: 'linear', duration: 600 }) this.animation = animation var num = 0 var opacity = 0 if (this.data.yFlag == false) { num = Math.abs(this.data.toTop) / 2 + this.data.selectHeight - 20 opacity = 0.4 this.setData({ yFlag: true, bgShow: false }) this.hideChange() } else { num = this.data.toTop opacity = 0 this.setData({ yFlag: false, }) } animation.translateY(num).step() this.setData({ animationBox: animation.export() }) var animation2 = wx.createAnimation({ timingFunction: 'linear', duration: 200 }) this.animation2 = animation2 animation2.opacity(opacity).step() this.setData({ animationBg: animation2.export() }) }, bindPickerChange: function (e) { const typeIndex = e.detail.value this.setData({ typeIndex }) // this.triggerEvent("changeType",str) }, }) // 历史课程 // end