index.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. currentDate: new Date(),
  14. currentYear: new Date().getFullYear(),
  15. currentMonth: new Date().getMonth() + 1,
  16. checkedDates: [], // 已签到日期,从服务器或本地存储加载
  17. today: new Date().getDate(), // 今天日期
  18. isCheckedToday: false, // 今天是否已签到
  19. weekdays: ['一', '二', '三', '四', '五', '六', '日'],
  20. calendarDays: [],
  21. tasks: [],
  22. totalScore: 0
  23. },
  24. loadList() {
  25. let that = this;
  26. wx.showLoading({
  27. title: '努力加载中...',
  28. })
  29. App._post_form('scoreTask/list', '', {
  30. stuId: util.getUserId()
  31. },
  32. function (res) {
  33. if (res.code == 0 && res.data) {
  34. // console.log(res);
  35. let list = res.data.dayTask ? [...res.data.dayTask] : [];
  36. let list1 = res.data.infiniteTask ? [...res.data.infiniteTask] : [];
  37. let list2 = res.data.singleTask ? [...res.data.singleTask] : [];
  38. console.log([...list, ...list1, ...list2]);
  39. that.setData({
  40. tasks: [...list, ...list1, ...list2]
  41. })
  42. // that.setData({
  43. // list: [{
  44. // name: '单次任务',
  45. // children: res.data.singleTask.map(item => {
  46. // return {
  47. // taskName: item.taskName,
  48. // taskScore: item.taskScore
  49. // }
  50. // })
  51. // }, {
  52. // name: '每日任务',
  53. // children: res.data.dayTask.map(item => {
  54. // return {
  55. // taskName: item.taskName,
  56. // taskScore: item.taskScore
  57. // }
  58. // })
  59. // }, {
  60. // name: '不限次任务',
  61. // children: res.data.infiniteTask.map(item => {
  62. // return {
  63. // taskName: item.taskName,
  64. // taskScore: item.taskScore
  65. // }
  66. // })
  67. // }]
  68. // })
  69. }
  70. })
  71. },
  72. onPointsTap() {
  73. wx.navigateTo({
  74. url: "/pointExchange/pages/center/center?totalScore=" + this.data.totalScore,
  75. });
  76. },
  77. /**
  78. * 查询用户数据根据id
  79. */
  80. getUserInfo() {
  81. let _this = this;
  82. let id = util.getUserId();
  83. let openid = wx.getStorageSync("openid");
  84. if (id) {
  85. let parm = {
  86. id,
  87. };
  88. App._post_form(
  89. "member/apiSelectMeberInfo",
  90. "application/json",
  91. JSON.stringify(parm),
  92. function (res) {
  93. // console.log(res);
  94. if (res.code === 0) {
  95. wx.setStorageSync("USER", res.member);
  96. _this.setData({
  97. userInfo: res.member,
  98. });
  99. // _this.loadUser()
  100. } else {
  101. wx.removeStorageSync("USER");
  102. wx.navigateTo({
  103. url: "/pages/login",
  104. });
  105. }
  106. }
  107. );
  108. } else {
  109. wx.navigateTo({
  110. url: '/pages/login',
  111. })
  112. }
  113. },
  114. loadData() {
  115. let that = this;
  116. App._post_form('scoreStu/totalScore', '', {
  117. stuId: util.getUserId()
  118. },
  119. function (res) {
  120. if (res.code == 0) {
  121. that.setData({
  122. totalScore: res.data
  123. })
  124. }
  125. })
  126. },
  127. /**
  128. * 生命周期函数--监听页面加载
  129. */
  130. onLoad: function (options) {
  131. wx.setNavigationBarTitle({
  132. title: '每日签到'
  133. });
  134. this.loadData();
  135. this.getUserInfo();
  136. this.loadList();
  137. },
  138. /**
  139. * 生命周期函数--监听页面显示
  140. */
  141. onShow: async function () {
  142. // 页面显示时重新加载签到状态
  143. await this.getCheckinHistoryFromServer();
  144. this.loadData();
  145. this.initCalendar();
  146. },
  147. /**
  148. * 下拉刷新
  149. */
  150. onPullDownRefresh: async function () {
  151. await this.getCheckinHistoryFromServer();
  152. this.loadData();
  153. this.initCalendar();
  154. this.getUserInfo();
  155. wx.stopPullDownRefresh();
  156. },
  157. /**
  158. * 从服务器获取签到历史
  159. */
  160. getCheckinHistoryFromServer() {
  161. const userId = util.getUserId();
  162. wx.showLoading({
  163. title: '加载中...',
  164. mask: true
  165. });
  166. if (!userId) return;
  167. App._get(
  168. 'user/dayList/' + userId,
  169. {},
  170. (res) => {
  171. wx.hideLoading();
  172. if (res.code === 0) {
  173. // let data = res.data.map(v => {
  174. // return parseInt(v.substr(6, 2));
  175. // })
  176. this.setData({
  177. checkedDates: res.data || []
  178. });
  179. this.initCalendar();
  180. }
  181. }
  182. );
  183. },
  184. /**
  185. * 初始化日历
  186. */
  187. initCalendar() {
  188. // 使用当前系统时间
  189. const date = new Date();
  190. const year = date.getFullYear();
  191. const month = date.getMonth();
  192. const today = date.getDate();
  193. // 获取当月第一天是星期几
  194. const firstDay = new Date(year, month, 1).getDay();
  195. const adjustedFirstDay = firstDay === 0 ? 7 : firstDay; // 将周日从0调整为7
  196. // 获取当月天数
  197. const daysInMonth = new Date(year, month + 1, 0).getDate();
  198. // 生成日历数组
  199. const calendarDays = [];
  200. // 添加空白格子(上个月的日期)
  201. for (let i = 1; i < adjustedFirstDay; i++) {
  202. calendarDays.push({ day: '', isEmpty: true });
  203. }
  204. // 添加当月日期
  205. for (let day = 1; day <= daysInMonth; day++) {
  206. let date1 = `${year}${month + 1 > 9 ? month + 1 : '0' + (month + 1)}${day > 9 ? day : '0' + day}`
  207. calendarDays.push({
  208. day: day,
  209. isEmpty: false,
  210. isToday: day === today,
  211. isChecked: this.data.checkedDates.includes(date1)
  212. });
  213. }
  214. let date1 = `${year}${month + 1 > 9 ? month + 1 : '0' + (month + 1)}${today > 9 ? today : '0' + today}`
  215. console.log('date1', date1)
  216. // console.log('aaa', calendarDays, this.data.checkedDates.includes(today))
  217. this.setData({
  218. calendarDays,
  219. currentYear: year,
  220. currentMonth: month + 1,
  221. today: today,
  222. isCheckedToday: this.data.checkedDates.includes(date1)
  223. });
  224. },
  225. /**
  226. * 立即签到
  227. */
  228. handleCheckin() {
  229. if (this.data.isCheckedToday) {
  230. wx.showToast({
  231. title: '今日已签到',
  232. icon: 'none'
  233. });
  234. return;
  235. }
  236. wx.showLoading({
  237. title: '签到中...'
  238. });
  239. // 执行签到操作
  240. this.performCheckin();
  241. },
  242. /**
  243. * 执行签到操作
  244. */
  245. performCheckin() {
  246. const userId = util.getUserId();
  247. const currentDate = new Date();
  248. let _this = this;
  249. // TODO: 调用签到API
  250. App._post_form(
  251. 'user/dailySign',
  252. 'application/json',
  253. {
  254. stuId: userId
  255. },
  256. async (res) => {
  257. if (res.code === 0) {
  258. // 积分统计完成
  259. wx.showToast({
  260. title: `签到成功`,
  261. icon: 'success'
  262. });
  263. await _this.getCheckinHistoryFromServer();
  264. _this.initCalendar();
  265. } else {
  266. wx.hideLoading();
  267. wx.showToast({
  268. title: res.message || '签到失败',
  269. icon: 'none'
  270. });
  271. }
  272. }
  273. );
  274. },
  275. /**
  276. * 处理任务点击
  277. */
  278. handleTaskClick(e) {
  279. console.log('handleTaskClick', e);
  280. return;
  281. // const taskId = e.currentTarget.dataset.id;
  282. // const task = this.data.tasks.find(t => t.id == taskId);
  283. // wx.showToast({
  284. // title: `点击了${task.title}`,
  285. // icon: 'none'
  286. // });
  287. // 根据不同任务跳转到不同页面
  288. switch (taskId) {
  289. case 1:
  290. case 4:
  291. // 邀请好友
  292. this.inviteFriend();
  293. break;
  294. case 2:
  295. // 观看广告
  296. this.watchAd();
  297. break;
  298. case 3:
  299. // 参与课程
  300. this.joinCourse();
  301. break;
  302. }
  303. },
  304. });