index.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. // this.getCheckDate();
  68. },
  69. /**
  70. * 生命周期函数--监听页面显示
  71. */
  72. onShow: function () {
  73. // 页面显示时重新加载签到状态
  74. this.loadCheckinHistory();
  75. this.initCalendar();
  76. },
  77. /**
  78. * 下拉刷新
  79. */
  80. onPullDownRefresh: function () {
  81. this.loadCheckinHistory();
  82. this.initCalendar();
  83. this.getUserInfo();
  84. wx.stopPullDownRefresh();
  85. },
  86. /**
  87. * 加载签到历史记录
  88. */
  89. loadCheckinHistory () {
  90. // const currentDate = new Date();
  91. // const year = currentDate.getFullYear();
  92. // const month = currentDate.getMonth() + 1;
  93. // const storageKey = `checkin_${year}_${month}`;
  94. // // 从本地存储加载当月签到记录
  95. // let checkedDates = wx.getStorageSync(storageKey) || [];
  96. // // 确保数据是数组格式
  97. // if (!Array.isArray(checkedDates)) {
  98. // checkedDates = [];
  99. // }
  100. // // 过滤掉无效的日期(大于当月最大天数)
  101. // const daysInMonth = new Date(year, month, 0).getDate();
  102. // checkedDates = checkedDates.filter(day => day >= 1 && day <= daysInMonth);
  103. // this.setData({
  104. // checkedDates
  105. // });
  106. // TODO: 这里可以调用API从服务器获取签到记录
  107. this.getCheckinHistoryFromServer();
  108. },
  109. //获取用户信息
  110. getUserInfo () {
  111. let _this = this;
  112. let id = util.getUserId();
  113. if (id) {
  114. let parm = {
  115. id,
  116. };
  117. App._post_form(
  118. "member/apiSelectMeberInfo",
  119. "application/json",
  120. JSON.stringify(parm),
  121. function (res) {
  122. console.log(res);
  123. if (res.code === 0) {
  124. wx.setStorageSync("USER", res.member);
  125. _this.setData({
  126. userInfo: res.member,
  127. });
  128. } else {
  129. wx.removeStorageSync("USER");
  130. wx.navigateTo({
  131. url: "/pages/login",
  132. });
  133. }
  134. }
  135. );
  136. }
  137. },
  138. // 获取已签到日期
  139. getCheckDate () {
  140. App._get(
  141. '/scoreStu/list',
  142. { stuId: util.getUserId() }).then(res => {
  143. });
  144. },
  145. /**
  146. * 从服务器获取签到历史(备用)
  147. */
  148. getCheckinHistoryFromServer () {
  149. const userId = util.getUserId();
  150. if (!userId) return;
  151. App._get(
  152. '/scoreStu/list',
  153. {
  154. stuId: userId,
  155. },
  156. (res) => {
  157. if (res.code === 0) {
  158. let data = res.data.map(v => {
  159. return parseInt(v.substr(6, 2));
  160. })
  161. this.setData({
  162. checkedDates: data
  163. });
  164. this.initCalendar();
  165. }
  166. }
  167. );
  168. },
  169. /**
  170. * 初始化日历
  171. */
  172. initCalendar () {
  173. // 使用当前系统时间
  174. const date = new Date();
  175. const year = date.getFullYear();
  176. const month = date.getMonth();
  177. const today = date.getDate();
  178. // 获取当月第一天是星期几
  179. const firstDay = new Date(year, month, 1).getDay();
  180. const adjustedFirstDay = firstDay === 0 ? 7 : firstDay; // 将周日从0调整为7
  181. // 获取当月天数
  182. const daysInMonth = new Date(year, month + 1, 0).getDate();
  183. // 生成日历数组
  184. const calendarDays = [];
  185. // 添加空白格子(上个月的日期)
  186. for (let i = 1; i < adjustedFirstDay; i++) {
  187. calendarDays.push({ day: '', isEmpty: true });
  188. }
  189. // 添加当月日期
  190. for (let day = 1; day <= daysInMonth; day++) {
  191. calendarDays.push({
  192. day: day,
  193. isEmpty: false,
  194. isToday: day === today,
  195. isChecked: this.data.checkedDates.includes(day)
  196. });
  197. }
  198. console.log('aaa', calendarDays, this.data.checkedDates.includes(26))
  199. this.setData({
  200. calendarDays,
  201. currentYear: year,
  202. currentMonth: month + 1,
  203. today: today,
  204. isCheckedToday: this.data.checkedDates.includes(today)
  205. });
  206. },
  207. /**
  208. * 立即签到
  209. */
  210. handleCheckin () {
  211. if (this.data.isCheckedToday) {
  212. wx.showToast({
  213. title: '今日已签到',
  214. icon: 'none'
  215. });
  216. return;
  217. }
  218. wx.showLoading({
  219. title: '签到中...'
  220. });
  221. // 执行签到操作
  222. this.performCheckin();
  223. },
  224. /**
  225. * 执行签到操作
  226. */
  227. performCheckin () {
  228. const userId = util.getUserId();
  229. const currentDate = new Date();
  230. // TODO: 调用签到API
  231. // App._post_form(
  232. // '/api/scoreStu/dailyLogin',
  233. // 'application/json',
  234. // {
  235. // userId: userId,
  236. // },
  237. // (res) => {
  238. // if (res.code === 0) {
  239. // this.updateCheckinSuccess(res.data.points || 10);
  240. // } else {
  241. // wx.hideLoading();
  242. // wx.showToast({
  243. // title: res.message || '签到失败',
  244. // icon: 'none'
  245. // });
  246. // }
  247. // }
  248. // );
  249. // 模拟成功签到
  250. // setTimeout(() => {
  251. this.updateCheckinSuccess(10);
  252. // }, 1000);
  253. },
  254. /**
  255. * 更新签到成功状态
  256. */
  257. updateCheckinSuccess (points = 10) {
  258. // wx.hideLoading();
  259. // const today = this.data.today;
  260. // const checkedDates = [...this.data.checkedDates, today];
  261. // const calendarDays = this.data.calendarDays.map(item => {
  262. // if (item.day === today) {
  263. // return { ...item, isChecked: true };
  264. // }
  265. // return item;
  266. // });
  267. // // 保存到本地存储
  268. // const currentDate = new Date();
  269. // const year = currentDate.getFullYear();
  270. // const month = currentDate.getMonth() + 1;
  271. // const storageKey = `checkin_${year}_${month}`;
  272. // wx.setStorageSync(storageKey, checkedDates);
  273. // this.setData({
  274. // checkedDates,
  275. // calendarDays,
  276. // isCheckedToday: true,
  277. // 'userInfo.points': this.data.userInfo.points + points
  278. // });
  279. // wx.showToast({
  280. // title: `签到成功,获得${points}积分`,
  281. // icon: 'success'
  282. // });
  283. // 统计积分(每日登录)
  284. // this.addScore();
  285. if (!util.getUserId()) {
  286. return;
  287. }
  288. let _this = this;
  289. App._post_form(
  290. 'scoreStu/dailySign',
  291. '', {
  292. stuId: util.getUserId(),
  293. },
  294. function (res) {
  295. // 积分统计完成
  296. wx.showToast({
  297. title: `签到成功,获得${points}积分`,
  298. icon: 'success'
  299. });
  300. _this.loadCheckinHistory();
  301. _this.initCalendar();
  302. }
  303. );
  304. },
  305. /**
  306. * 统计积分(每日登录)
  307. */
  308. addScore: function () {
  309. if (!util.getUserId()) {
  310. return;
  311. }
  312. App._post_form(
  313. 'scoreStu/dailyLogin/dailySign',
  314. '', {
  315. stuId: util.getUserId(),
  316. },
  317. function (res) {
  318. // 积分统计完成
  319. }
  320. );
  321. },
  322. /**
  323. * 处理任务点击
  324. */
  325. handleTaskClick (e) {
  326. const taskId = e.currentTarget.dataset.id;
  327. const task = this.data.tasks.find(t => t.id == taskId);
  328. wx.showToast({
  329. title: `点击了${task.title}`,
  330. icon: 'none'
  331. });
  332. // 根据不同任务跳转到不同页面
  333. switch (taskId) {
  334. case 1:
  335. case 4:
  336. // 邀请好友
  337. this.inviteFriend();
  338. break;
  339. case 2:
  340. // 观看广告
  341. this.watchAd();
  342. break;
  343. case 3:
  344. // 参与课程
  345. this.joinCourse();
  346. break;
  347. }
  348. },
  349. /**
  350. * 邀请好友
  351. */
  352. inviteFriend () {
  353. wx.showModal({
  354. title: '邀请好友',
  355. content: '邀请好友注册可获得积分奖励',
  356. confirmText: '立即邀请',
  357. success: (res) => {
  358. if (res.confirm) {
  359. // 实现邀请逻辑
  360. wx.showToast({
  361. title: '邀请功能开发中',
  362. icon: 'none'
  363. });
  364. }
  365. }
  366. });
  367. },
  368. /**
  369. * 观看广告
  370. */
  371. watchAd () {
  372. wx.showToast({
  373. title: '广告功能开发中',
  374. icon: 'none'
  375. });
  376. },
  377. /**
  378. * 参与课程
  379. */
  380. joinCourse () {
  381. wx.navigateTo({
  382. url: '/pages/home/index/courseDetail/courseDetail'
  383. });
  384. }
  385. });