123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- // 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,
- });
- }
- },
- });
|