// 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, appAssetsUrl3: App.appAssetsUrl3, userInfo: {}, 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]); let dataList = [...list, ...list1, ...list2].map(v => { return v }) that.setData({ tasks: [...dataList] }) } }) }, 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.clearStorage(); 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) { if (!util.getUserId()) { wx.redirectTo({ url: '/pages/login', }) return } wx.setNavigationBarTitle({ title: '每日签到' }); this.loadData(); this.getUserInfo(); this.loadList(); }, /** * 生命周期函数--监听页面显示 */ onShow: async function () { // 页面显示时重新加载签到状态 await this.getCheckinHistoryFromServer(); this.loadData(); }, /** * 下拉刷新 */ onPullDownRefresh: async function () { await this.getCheckinHistoryFromServer(); this.loadData(); this.getUserInfo(); wx.stopPullDownRefresh(); }, /** * 从服务器获取签到历史 */ async getCheckinHistoryFromServer() { const userId = util.getUserId(); wx.showLoading({ title: '加载中...', mask: true }); if (!userId) return; await App._get( 'user/dayList/' + userId, {}, (res) => { wx.hideLoading(); if (res.code === 0 && res.data) { // let data = res.data.map(v => { // return parseInt(v.substr(6, 2)); // }) this.setData({ checkedDates: res.data.history || [] }); this.initCalendar(res.data.current); } } ); }, /** * 初始化日历 */ initCalendar(dates = null) { // 使用当前系统时间 const date = dates ? new Date(dates) : 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(); 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' }); _this.loadData(); await _this.getCheckinHistoryFromServer(); _this.loadList(); } 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; case '分享小程序': break; case '观看广告': this.showRewardedVideoAd(); break; } if (taskName.indexOf('观看广告') != -1) { // console.log('showInterstitialAd') // this.showInterstitialAd(); return } // console.log('url', url) if (url == '') return; if (tab) { wx.switchTab({ url: url, }); } else { wx.navigateTo({ url: url, }); } }, //统计积分(每日小程序分享) addScore: function() { if(!util.getUserId()){ return ; } wx.showLoading({ title: '努力加载中...', }) App._post_form('scoreStu/share', "", { stuId: util.getUserId() }, function(res) { if (res.code === 0) {} }) }, /** * 用户点击右上角分享 */ onShareAppMessage() { this.addScore(); // console.log('分享=============='); return { title: '每日签到', path: '/pages/home/index/index', desc: '', } }, // 广告观看完毕,获取积分 addWatchAds() { const userId = util.getUserId(); let _this = this; // TODO: 调用签到API App._post_form( 'scoreStu/watchAds', 'application/json', { stuId: userId }, async (res) => { if (res.code === 0) { // 积分统计完成 wx.showToast({ title: `获取成功`, icon: 'success' }); setTimeout(() => { _this.loadList(); _this.loadData(); }, 1000); } } ); }, /** * 查看广告 * */ showRewardedVideoAd: function (e) { // console.log('showRewardedVideoAd') this.selectComponent('.uni-rewarded-video-ad').show(); }, onadload: function (e) { // console.log('广告加载成功:', e) }, onadclose: function (e) { const detail = e.detail; // console.log("onClose-播放结束:" + detail) // 用户点击了【关闭广告】按钮 if (detail && detail.isEnded) { // 正常播放结束 // console.log("onClose-正常播放结束:" + detail.isEnded); this.addWatchAds(); } else { // 播放中途退出 console.log("onClose-播放中途退出:" + detail.isEnded); } }, onaderror: function (e) { // 广告加载失败 console.log('广告加载失败:', e.detail) } });