// pages/checkin/index.js const util = require("../../utils/util"); const tools = require("../../utils/tool"); const App = getApp(); Page({ /** * 页面的初始数据 */ data: { appAssetsUrl: App.appAssetsUrl, appAssetsUrl2: App.appAssetsUrl2, userInfo: {}, currentDate: new Date(), currentYear: new Date().getFullYear(), currentMonth: new Date().getMonth() + 1, checkedDates: [], // 已签到日期,从服务器或本地存储加载 today: new Date().getDate(), // 今天日期 isCheckedToday: false, // 今天是否已签到 weekdays: ['一', '二', '三', '四', '五', '六', '日'], calendarDays: [], tasks: [], totalScore: 0 }, loadList() { let that = this; wx.showLoading({ title: '努力加载中...', }) App._post_form('scoreTask/list', '', { stuId: util.getUserId() }, function (res) { if (res.code == 0 && res.data) { // console.log(res); let list = res.data.dayTask ? [...res.data.dayTask] : []; let list1 = res.data.infiniteTask ? [...res.data.infiniteTask] : []; let list2 = res.data.singleTask ? [...res.data.singleTask] : []; // console.log([...list, ...list1, ...list2]); that.setData({ tasks: [...list, ...list1, ...list2] }) } }) }, onPointsTap() { wx.navigateTo({ url: "/pointExchange/pages/center/center?totalScore=" + this.data.totalScore, }); }, /** * 查询用户数据根据id */ getUserInfo() { let _this = this; let id = util.getUserId(); let openid = wx.getStorageSync("openid"); if (id) { let parm = { id, }; App._post_form( "member/apiSelectMeberInfo", "application/json", JSON.stringify(parm), function (res) { // console.log(res); if (res.code === 0) { wx.setStorageSync("USER", res.member); _this.setData({ userInfo: res.member, }); // _this.loadUser() } else { wx.removeStorageSync("USER"); wx.navigateTo({ url: "/pages/login", }); } } ); } else { wx.navigateTo({ url: '/pages/login', }) } }, loadData() { let that = this; App._post_form('scoreStu/totalScore', '', { stuId: util.getUserId() }, function (res) { if (res.code == 0) { that.setData({ totalScore: res.data }) } }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { wx.setNavigationBarTitle({ title: '每日签到' }); this.loadData(); this.getUserInfo(); this.loadList(); }, /** * 生命周期函数--监听页面显示 */ onShow: async function () { // 页面显示时重新加载签到状态 await this.getCheckinHistoryFromServer(); this.loadData(); this.initCalendar(); }, /** * 下拉刷新 */ onPullDownRefresh: async function () { await this.getCheckinHistoryFromServer(); this.loadData(); this.initCalendar(); this.getUserInfo(); wx.stopPullDownRefresh(); }, /** * 从服务器获取签到历史 */ getCheckinHistoryFromServer() { const userId = util.getUserId(); wx.showLoading({ title: '加载中...', mask: true }); if (!userId) return; App._get( 'user/dayList/' + userId, {}, (res) => { wx.hideLoading(); if (res.code === 0) { // let data = res.data.map(v => { // return parseInt(v.substr(6, 2)); // }) this.setData({ checkedDates: res.data || [] }); this.initCalendar(); } } ); }, /** * 初始化日历 */ initCalendar() { // 使用当前系统时间 const date = new Date(); const year = date.getFullYear(); const month = date.getMonth(); const today = date.getDate(); // 获取当月第一天是星期几 const firstDay = new Date(year, month, 1).getDay(); const adjustedFirstDay = firstDay === 0 ? 7 : firstDay; // 将周日从0调整为7 // 获取当月天数 const daysInMonth = new Date(year, month + 1, 0).getDate(); // 生成日历数组 const calendarDays = []; // 添加空白格子(上个月的日期) for (let i = 1; i < adjustedFirstDay; i++) { calendarDays.push({ day: '', isEmpty: true }); } // 添加当月日期 for (let day = 1; day <= daysInMonth; day++) { let date1 = `${year}${month + 1 > 9 ? month + 1 : '0' + (month + 1)}${day > 9 ? day : '0' + day}` calendarDays.push({ day: day, isEmpty: false, isToday: day === today, isChecked: this.data.checkedDates.includes(date1) }); } let date1 = `${year}${month + 1 > 9 ? month + 1 : '0' + (month + 1)}${today > 9 ? today : '0' + today}` // console.log('date1', date1) // console.log('aaa', calendarDays, this.data.checkedDates.includes(today)) this.setData({ calendarDays, currentYear: year, currentMonth: month + 1, today: today, isCheckedToday: this.data.checkedDates.includes(date1) }); }, /** * 立即签到 */ handleCheckin() { if (this.data.isCheckedToday) { wx.showToast({ title: '今日已签到', icon: 'none' }); return; } wx.showLoading({ title: '签到中...' }); // 执行签到操作 this.performCheckin(); }, /** * 执行签到操作 */ performCheckin() { const userId = util.getUserId(); const currentDate = new Date(); let _this = this; // TODO: 调用签到API App._post_form( 'user/dailySign', 'application/json', { stuId: userId }, async (res) => { if (res.code === 0) { // 积分统计完成 wx.showToast({ title: `签到成功`, icon: 'success' }); await _this.getCheckinHistoryFromServer(); _this.initCalendar(); } else { wx.hideLoading(); wx.showToast({ title: res.message || '签到失败', icon: 'none' }); } } ); }, /** * 处理任务点击 */ handleTaskClick(e) { // console.log('handleTaskClick', e.currentTarget.dataset.item); let item = e.currentTarget.dataset.item || {}; if (item.complete) return; let url = ''; const taskName = item.taskName; let tab = false; // 根据不同任务跳转到不同页面 switch (taskName) { case '邀请好友': url = '/invitationCode/index'; break; case '参与兼职': case '参加兼职/成长会': url = '/pages/practicalExperience/practicalExperience'; tab = true; break; case '每日签到': this.handleCheckin(); break; case '购买会员': case '开通会员': url = '/pages/myMember/myMember'; break; case '学籍认证': case '完成学籍认证': url = '/pages/my/myStudy/myStudy'; break; case '完善资料': case '完善个人资料': url = '/pages/my/myData/myData'; break; case '参与活动': url = '/pages/experience/index/index'; break; case '观看广告': break; } // console.log('url', url) if (url == '') return; if (tab) { wx.switchTab({ url: url, }); } else { wx.navigateTo({ url: url, }); } }, });