123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- // 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: {
- avatar: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLt5DDw9rFVYB2D8bYlMOVqVTdJ2y7WRzVBJy5s3ib5K8nIHQ8iaJrGvEJmjwP3QrhEEHgPnIiccRJvA/132',
- nickname: '刘微雪',
- points: 2000
- },
- currentDate: new Date(),
- currentYear: new Date().getFullYear(),
- currentMonth: new Date().getMonth() + 1,
- checkedDates: [], // 已签到日期,从服务器或本地存储加载
- today: new Date().getDate(), // 今天日期
- isCheckedToday: false, // 今天是否已签到
- weekdays: ['一', '二', '三', '四', '五', '六', '日'],
- calendarDays: [],
- tasks: [
- {
- id: 1,
- icon: '/assets/images/user-icon/user1.png',
- title: '邀请好友',
- desc: '邀请成功可获得积分奖励',
- status: '去完成'
- },
- {
- id: 2,
- icon: '/assets/images/user-icon/user2.png',
- title: '观看广告',
- desc: '观看广告可获得积分奖励',
- count: '(0/3)',
- status: '去完成'
- },
- {
- id: 3,
- icon: '/assets/images/user-icon/user3.png',
- title: '参与课程',
- desc: '邀请成功可获得积分奖励',
- status: '去完成'
- },
- {
- id: 4,
- icon: '/assets/images/user-icon/user4.png',
- title: '邀请好友',
- desc: '邀请成功可获得积分奖励',
- status: '去完成'
- }
- ]
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- wx.setNavigationBarTitle({
- title: '每日签到'
- });
- this.loadCheckinHistory();
- this.initCalendar();
- this.getUserInfo();
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- // 页面显示时重新加载签到状态
- this.loadCheckinHistory();
- this.initCalendar();
- },
- /**
- * 下拉刷新
- */
- onPullDownRefresh: function () {
- this.loadCheckinHistory();
- this.initCalendar();
- this.getUserInfo();
- wx.stopPullDownRefresh();
- },
- /**
- * 加载签到历史记录
- */
- loadCheckinHistory() {
- const currentDate = new Date();
- const year = currentDate.getFullYear();
- const month = currentDate.getMonth() + 1;
- const storageKey = `checkin_${year}_${month}`;
-
- // 从本地存储加载当月签到记录
- let checkedDates = wx.getStorageSync(storageKey) || [];
-
- // 确保数据是数组格式
- if (!Array.isArray(checkedDates)) {
- checkedDates = [];
- }
-
- // 过滤掉无效的日期(大于当月最大天数)
- const daysInMonth = new Date(year, month, 0).getDate();
- checkedDates = checkedDates.filter(day => day >= 1 && day <= daysInMonth);
-
- this.setData({
- checkedDates
- });
-
- // TODO: 这里可以调用API从服务器获取签到记录
- // this.getCheckinHistoryFromServer();
- },
- /**
- * 从服务器获取签到历史(备用)
- */
- getCheckinHistoryFromServer() {
- const userId = util.getUserId();
- if (!userId) return;
-
- const currentDate = new Date();
- App._post_form(
- 'checkin/getMonthlyHistory',
- 'application/json',
- {
- userId: userId,
- year: currentDate.getFullYear(),
- month: currentDate.getMonth() + 1
- },
- (res) => {
- if (res.code === 0) {
- 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++) {
- calendarDays.push({
- day: day,
- isEmpty: false,
- isToday: day === today,
- isChecked: this.data.checkedDates.includes(day)
- });
- }
-
- this.setData({
- calendarDays,
- currentYear: year,
- currentMonth: month + 1,
- today: today,
- isCheckedToday: this.data.checkedDates.includes(today)
- });
- },
- /**
- * 获取用户信息
- */
- getUserInfo() {
- // 这里可以调用API获取用户信息
- const userInfo = wx.getStorageSync('userInfo') || this.data.userInfo;
- this.setData({
- userInfo
- });
- },
- /**
- * 立即签到
- */
- handleCheckin() {
- if (this.data.isCheckedToday) {
- wx.showToast({
- title: '今日已签到',
- icon: 'none'
- });
- return;
- }
- wx.showLoading({
- title: '签到中...'
- });
- // 执行签到操作
- this.performCheckin();
- },
- /**
- * 执行签到操作
- */
- performCheckin() {
- const userId = util.getUserId();
- const today = this.data.today;
- const currentDate = new Date();
- const year = currentDate.getFullYear();
- const month = currentDate.getMonth() + 1;
-
- // TODO: 调用签到API
- // App._post_form(
- // 'checkin/dailyCheckin',
- // 'application/json',
- // {
- // userId: userId,
- // date: `${year}-${month.toString().padStart(2, '0')}-${today.toString().padStart(2, '0')}`
- // },
- // (res) => {
- // if (res.code === 0) {
- // this.updateCheckinSuccess(res.data.points || 10);
- // } else {
- // wx.hideLoading();
- // wx.showToast({
- // title: res.message || '签到失败',
- // icon: 'none'
- // });
- // }
- // }
- // );
-
- // 模拟成功签到
- setTimeout(() => {
- this.updateCheckinSuccess(10);
- }, 1000);
- },
- /**
- * 更新签到成功状态
- */
- updateCheckinSuccess(points = 10) {
- wx.hideLoading();
-
- const today = this.data.today;
- const checkedDates = [...this.data.checkedDates, today];
- const calendarDays = this.data.calendarDays.map(item => {
- if (item.day === today) {
- return { ...item, isChecked: true };
- }
- return item;
- });
- // 保存到本地存储
- const currentDate = new Date();
- const year = currentDate.getFullYear();
- const month = currentDate.getMonth() + 1;
- const storageKey = `checkin_${year}_${month}`;
- wx.setStorageSync(storageKey, checkedDates);
- this.setData({
- checkedDates,
- calendarDays,
- isCheckedToday: true,
- 'userInfo.points': this.data.userInfo.points + points
- });
- wx.showToast({
- title: `签到成功,获得${points}积分`,
- icon: 'success'
- });
-
- // 统计积分(每日登录)
- this.addScore();
- },
- /**
- * 统计积分(每日登录)
- */
- addScore: function() {
- if (!util.getUserId()) {
- return;
- }
- App._post_form(
- 'scoreStu/dailyLogin',
- '', {
- stuId: util.getUserId()
- },
- function(res) {
- // 积分统计完成
- }
- );
- },
- /**
- * 处理任务点击
- */
- handleTaskClick(e) {
- const taskId = e.currentTarget.dataset.id;
- const task = this.data.tasks.find(t => t.id == taskId);
-
- wx.showToast({
- title: `点击了${task.title}`,
- icon: 'none'
- });
-
- // 根据不同任务跳转到不同页面
- switch(taskId) {
- case 1:
- case 4:
- // 邀请好友
- this.inviteFriend();
- break;
- case 2:
- // 观看广告
- this.watchAd();
- break;
- case 3:
- // 参与课程
- this.joinCourse();
- break;
- }
- },
- /**
- * 邀请好友
- */
- inviteFriend() {
- wx.showModal({
- title: '邀请好友',
- content: '邀请好友注册可获得积分奖励',
- confirmText: '立即邀请',
- success: (res) => {
- if (res.confirm) {
- // 实现邀请逻辑
- wx.showToast({
- title: '邀请功能开发中',
- icon: 'none'
- });
- }
- }
- });
- },
- /**
- * 观看广告
- */
- watchAd() {
- wx.showToast({
- title: '广告功能开发中',
- icon: 'none'
- });
- },
- /**
- * 参与课程
- */
- joinCourse() {
- wx.navigateTo({
- url: '/pages/home/index/courseDetail/courseDetail'
- });
- }
- });
|