index.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // pages/checkin/index.js
  2. const util = require("../../utils/util");
  3. const tools = require("../../utils/tool");
  4. const App = getApp();
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. appAssetsUrl: App.appAssetsUrl,
  11. appAssetsUrl2: App.appAssetsUrl2,
  12. userInfo: {
  13. avatar: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLt5DDw9rFVYB2D8bYlMOVqVTdJ2y7WRzVBJy5s3ib5K8nIHQ8iaJrGvEJmjwP3QrhEEHgPnIiccRJvA/132',
  14. nickname: '刘微雪',
  15. points: 2000
  16. },
  17. currentDate: new Date(),
  18. currentYear: new Date().getFullYear(),
  19. currentMonth: new Date().getMonth() + 1,
  20. checkedDates: [], // 已签到日期,从服务器或本地存储加载
  21. today: new Date().getDate(), // 今天日期
  22. isCheckedToday: false, // 今天是否已签到
  23. weekdays: ['一', '二', '三', '四', '五', '六', '日'],
  24. calendarDays: [],
  25. tasks: [
  26. {
  27. id: 1,
  28. icon: '/assets/images/user-icon/user1.png',
  29. title: '邀请好友',
  30. desc: '邀请成功可获得积分奖励',
  31. status: '去完成'
  32. },
  33. {
  34. id: 2,
  35. icon: '/assets/images/user-icon/user2.png',
  36. title: '观看广告',
  37. desc: '观看广告可获得积分奖励',
  38. count: '(0/3)',
  39. status: '去完成'
  40. },
  41. {
  42. id: 3,
  43. icon: '/assets/images/user-icon/user3.png',
  44. title: '参与课程',
  45. desc: '邀请成功可获得积分奖励',
  46. status: '去完成'
  47. },
  48. {
  49. id: 4,
  50. icon: '/assets/images/user-icon/user4.png',
  51. title: '邀请好友',
  52. desc: '邀请成功可获得积分奖励',
  53. status: '去完成'
  54. }
  55. ]
  56. },
  57. /**
  58. * 生命周期函数--监听页面加载
  59. */
  60. onLoad: function (options) {
  61. wx.setNavigationBarTitle({
  62. title: '每日签到'
  63. });
  64. this.loadCheckinHistory();
  65. this.initCalendar();
  66. this.getUserInfo();
  67. },
  68. /**
  69. * 生命周期函数--监听页面显示
  70. */
  71. onShow: function () {
  72. // 页面显示时重新加载签到状态
  73. this.loadCheckinHistory();
  74. this.initCalendar();
  75. },
  76. /**
  77. * 下拉刷新
  78. */
  79. onPullDownRefresh: function () {
  80. this.loadCheckinHistory();
  81. this.initCalendar();
  82. this.getUserInfo();
  83. wx.stopPullDownRefresh();
  84. },
  85. /**
  86. * 加载签到历史记录
  87. */
  88. loadCheckinHistory() {
  89. const currentDate = new Date();
  90. const year = currentDate.getFullYear();
  91. const month = currentDate.getMonth() + 1;
  92. const storageKey = `checkin_${year}_${month}`;
  93. // 从本地存储加载当月签到记录
  94. let checkedDates = wx.getStorageSync(storageKey) || [];
  95. // 确保数据是数组格式
  96. if (!Array.isArray(checkedDates)) {
  97. checkedDates = [];
  98. }
  99. // 过滤掉无效的日期(大于当月最大天数)
  100. const daysInMonth = new Date(year, month, 0).getDate();
  101. checkedDates = checkedDates.filter(day => day >= 1 && day <= daysInMonth);
  102. this.setData({
  103. checkedDates
  104. });
  105. // TODO: 这里可以调用API从服务器获取签到记录
  106. // this.getCheckinHistoryFromServer();
  107. },
  108. /**
  109. * 从服务器获取签到历史(备用)
  110. */
  111. getCheckinHistoryFromServer() {
  112. const userId = util.getUserId();
  113. if (!userId) return;
  114. const currentDate = new Date();
  115. App._post_form(
  116. 'checkin/getMonthlyHistory',
  117. 'application/json',
  118. {
  119. userId: userId,
  120. year: currentDate.getFullYear(),
  121. month: currentDate.getMonth() + 1
  122. },
  123. (res) => {
  124. if (res.code === 0) {
  125. this.setData({
  126. checkedDates: res.data || []
  127. });
  128. this.initCalendar();
  129. }
  130. }
  131. );
  132. },
  133. /**
  134. * 初始化日历
  135. */
  136. initCalendar() {
  137. // 使用当前系统时间
  138. const date = new Date();
  139. const year = date.getFullYear();
  140. const month = date.getMonth();
  141. const today = date.getDate();
  142. // 获取当月第一天是星期几
  143. const firstDay = new Date(year, month, 1).getDay();
  144. const adjustedFirstDay = firstDay === 0 ? 7 : firstDay; // 将周日从0调整为7
  145. // 获取当月天数
  146. const daysInMonth = new Date(year, month + 1, 0).getDate();
  147. // 生成日历数组
  148. const calendarDays = [];
  149. // 添加空白格子(上个月的日期)
  150. for (let i = 1; i < adjustedFirstDay; i++) {
  151. calendarDays.push({ day: '', isEmpty: true });
  152. }
  153. // 添加当月日期
  154. for (let day = 1; day <= daysInMonth; day++) {
  155. calendarDays.push({
  156. day: day,
  157. isEmpty: false,
  158. isToday: day === today,
  159. isChecked: this.data.checkedDates.includes(day)
  160. });
  161. }
  162. this.setData({
  163. calendarDays,
  164. currentYear: year,
  165. currentMonth: month + 1,
  166. today: today,
  167. isCheckedToday: this.data.checkedDates.includes(today)
  168. });
  169. },
  170. /**
  171. * 获取用户信息
  172. */
  173. getUserInfo() {
  174. // 这里可以调用API获取用户信息
  175. const userInfo = wx.getStorageSync('userInfo') || this.data.userInfo;
  176. this.setData({
  177. userInfo
  178. });
  179. },
  180. /**
  181. * 立即签到
  182. */
  183. handleCheckin() {
  184. if (this.data.isCheckedToday) {
  185. wx.showToast({
  186. title: '今日已签到',
  187. icon: 'none'
  188. });
  189. return;
  190. }
  191. wx.showLoading({
  192. title: '签到中...'
  193. });
  194. // 执行签到操作
  195. this.performCheckin();
  196. },
  197. /**
  198. * 执行签到操作
  199. */
  200. performCheckin() {
  201. const userId = util.getUserId();
  202. const today = this.data.today;
  203. const currentDate = new Date();
  204. const year = currentDate.getFullYear();
  205. const month = currentDate.getMonth() + 1;
  206. // TODO: 调用签到API
  207. // App._post_form(
  208. // 'checkin/dailyCheckin',
  209. // 'application/json',
  210. // {
  211. // userId: userId,
  212. // date: `${year}-${month.toString().padStart(2, '0')}-${today.toString().padStart(2, '0')}`
  213. // },
  214. // (res) => {
  215. // if (res.code === 0) {
  216. // this.updateCheckinSuccess(res.data.points || 10);
  217. // } else {
  218. // wx.hideLoading();
  219. // wx.showToast({
  220. // title: res.message || '签到失败',
  221. // icon: 'none'
  222. // });
  223. // }
  224. // }
  225. // );
  226. // 模拟成功签到
  227. setTimeout(() => {
  228. this.updateCheckinSuccess(10);
  229. }, 1000);
  230. },
  231. /**
  232. * 更新签到成功状态
  233. */
  234. updateCheckinSuccess(points = 10) {
  235. wx.hideLoading();
  236. const today = this.data.today;
  237. const checkedDates = [...this.data.checkedDates, today];
  238. const calendarDays = this.data.calendarDays.map(item => {
  239. if (item.day === today) {
  240. return { ...item, isChecked: true };
  241. }
  242. return item;
  243. });
  244. // 保存到本地存储
  245. const currentDate = new Date();
  246. const year = currentDate.getFullYear();
  247. const month = currentDate.getMonth() + 1;
  248. const storageKey = `checkin_${year}_${month}`;
  249. wx.setStorageSync(storageKey, checkedDates);
  250. this.setData({
  251. checkedDates,
  252. calendarDays,
  253. isCheckedToday: true,
  254. 'userInfo.points': this.data.userInfo.points + points
  255. });
  256. wx.showToast({
  257. title: `签到成功,获得${points}积分`,
  258. icon: 'success'
  259. });
  260. // 统计积分(每日登录)
  261. this.addScore();
  262. },
  263. /**
  264. * 统计积分(每日登录)
  265. */
  266. addScore: function() {
  267. if (!util.getUserId()) {
  268. return;
  269. }
  270. App._post_form(
  271. 'scoreStu/dailyLogin',
  272. '', {
  273. stuId: util.getUserId()
  274. },
  275. function(res) {
  276. // 积分统计完成
  277. }
  278. );
  279. },
  280. /**
  281. * 处理任务点击
  282. */
  283. handleTaskClick(e) {
  284. const taskId = e.currentTarget.dataset.id;
  285. const task = this.data.tasks.find(t => t.id == taskId);
  286. wx.showToast({
  287. title: `点击了${task.title}`,
  288. icon: 'none'
  289. });
  290. // 根据不同任务跳转到不同页面
  291. switch(taskId) {
  292. case 1:
  293. case 4:
  294. // 邀请好友
  295. this.inviteFriend();
  296. break;
  297. case 2:
  298. // 观看广告
  299. this.watchAd();
  300. break;
  301. case 3:
  302. // 参与课程
  303. this.joinCourse();
  304. break;
  305. }
  306. },
  307. /**
  308. * 邀请好友
  309. */
  310. inviteFriend() {
  311. wx.showModal({
  312. title: '邀请好友',
  313. content: '邀请好友注册可获得积分奖励',
  314. confirmText: '立即邀请',
  315. success: (res) => {
  316. if (res.confirm) {
  317. // 实现邀请逻辑
  318. wx.showToast({
  319. title: '邀请功能开发中',
  320. icon: 'none'
  321. });
  322. }
  323. }
  324. });
  325. },
  326. /**
  327. * 观看广告
  328. */
  329. watchAd() {
  330. wx.showToast({
  331. title: '广告功能开发中',
  332. icon: 'none'
  333. });
  334. },
  335. /**
  336. * 参与课程
  337. */
  338. joinCourse() {
  339. wx.navigateTo({
  340. url: '/pages/home/index/courseDetail/courseDetail'
  341. });
  342. }
  343. });