const { getGoal } = require("../../../utils/serve/train") const { setGoal, getClock } = require("../../../utils/serve/user") // subpackage/train/goal/goal.js Page({ /** * 页面的初始数据 */ data: { goalNum:0, nowGoal:{}, year: new Date().getFullYear(), month: (new Date().getMonth() + 1).toString().padStart(2, '0'), days: [], startX: 0, // 触摸起始位置 startY: 0, // 触摸起始位置 minSwipeDistance: 50 , isChooseDate:' ' }, putNum(e){ console.log(e) this.setData({ goalNum:e.detail.value, }) }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { this.getMyGoal() this.loadCalendar(); }, async loadCalendar() { wx.showLoading() const { year, month } = this.data; // 获取当月的第一天是星期几 const firstDay = new Date(year, month - 1, 1).getDay(); // 获取当月的天数 const totalDays = new Date(year, month, 0).getDate(); // 获取今天的日期(用于高亮显示) const today = new Date(); const todayDate = `${today.getFullYear()}-${(today.getMonth() + 1).toString().padStart(2, '0')}-${today.getDate().toString().padStart(2, '0')}`; const {data} = await getClock({ StartTime:`${year}-${month}-01`, EndTime:`${year}-${month}-${totalDays}`, }) // 创建日期数组 const days = []; // 填充空白日期 for (let i = 0; i < firstDay; i++) { days.push('nn'); } // 填充实际日期 for (let i = 1; i <= totalDays; i++) { const day = `${year}-${month.toString().padStart(2, '0')}-${i.toString().padStart(2, '0')}`; const isToday = day === todayDate; // 如果是今天的日期,则标记为今天 const goal = data.filter(item => item.day == i) let haveGoal = false let finishGoal = false let itemClass = '' if(goal.length>0){ haveGoal = true itemClass = 'haveGoal' if(goal[0].completedCount > goal[0].goalAmount){ itemClass = 'finishGoal' } } days.push({ day: i.toString().padStart(2,'0'),isToday ,...goal[0],haveGoal,finishGoal,itemClass}); } console.log(days) this.setData({ days, selectedDate:this.data.selectedDate?this.data.selectedDate:todayDate }); wx.hideLoading() }, changeMonth(event) { const direction = event.currentTarget.dataset.direction; let { year, month } = this.data; if (direction === 'prev') { month--; if (month < 1) { month = 12; year--; } } else if (direction === 'next') { month++; if (month > 12) { month = 1; year++; } } this.setData({ year, month:month.toString().padStart(2,'0'), isChooseDate:' ' }, () => { this.loadCalendar(); }); }, // 显示事件详情 showEventDetail(event) { return const day = event.currentTarget.dataset.day.day; const selectedDate = this.data.year + '-'+ `${this.data.month}`.padStart(2, '0')+'-' +`${day.toString().padStart(2, '0')}`; this.setData({ isChooseDate:day, selectedDate:selectedDate }) }, touchStart(e) { this.setData({ startX: e.touches[0].clientX, startY: e.touches[0].clientY }); }, // 触摸结束事件 touchEnd(e) { const endX = e.changedTouches[0].clientX; const endY = e.changedTouches[0].clientY; const dx = endX - this.data.startX; // 水平滑动距离 const dy = endY - this.data.startY; // 垂直滑动距离 // 判断是否为左右滑动(忽略上下滑动) if (Math.abs(dx) > Math.abs(dy)) { if (Math.abs(dx) >= this.data.minSwipeDistance) { // 只有当滑动距离超过阈值时才切换,防止误触 if (dx > 0) { // 向右滑动,切换到上个月 this.changeMonthBySwipe('prev'); } else if (dx < 0) { // 向左滑动,切换到下个月 this.changeMonthBySwipe('next'); } } } }, // 根据滑动方向切换月份 changeMonthBySwipe(direction) { let { year, month } = this.data; if (direction === 'prev') { month--; if (month < 1) { month = 12; year--; } } else if (direction === 'next') { month++; if (month > 12) { month = 1; year++; } } this.setData({ year, month, isChooseDate:' ' }, () => { this.loadCalendar(); }); }, //设置个人目标 async setMyGoal(){ const that = this wx.showLoading({ title: '', }) const now = new Date(); const year = now.getFullYear(); const month = String(now.getMonth() + 1).padStart(2, '0'); const day = String(now.getDate()).padStart(2, '0'); const res = await setGoal({ "goalDuration": 0, "goalAmount": this.data.goalNum, "goalDate": year + '-' + month + '-' + day }) await that.getMyGoal() wx.showToast({ title: res.success ? '设置成功':'设置失败,请稍后重试!', }) let app = getApp() app.globalData.teamRefresh = 1 }, async getMyGoal(){ const res = await getGoal() this.setData({ nowGoal : res.data }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })