index.js 7.6 KB

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