AddAttendanceRecord.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. const API = require("../../lib/API");
  2. const { BaseStdResponse } = require("../../BaseStdResponse");
  3. const db = require("../../plugin/DataBase/db");
  4. const AccessControl = require("../../lib/AccessControl");
  5. class AddAttendanceRecord extends API {
  6. constructor() {
  7. super();
  8. this.setPath('/AddAttendanceRecord');
  9. this.setMethod('GET');
  10. }
  11. async onRequest(data, res) {
  12. this.setAllowCORS(res);
  13. let {
  14. uuid,
  15. session,
  16. project_id
  17. } = data;
  18. // 检查必需的参数是否缺失
  19. if ([uuid, session, project_id].some(value => value === '' || value === null || value === undefined)) {
  20. res.json({
  21. ...BaseStdResponse.MISSING_PARAMETER,
  22. endpoint: 1513123
  23. });
  24. return;
  25. }
  26. // 检查 session 是否有效
  27. if (!await AccessControl.checkSession(uuid, session)) {
  28. res.json({
  29. ...BaseStdResponse.ACCESS_DENIED,
  30. endpoint: 48153145
  31. });
  32. return;
  33. }
  34. // 获取考勤项目
  35. const sqlGetProject = 'SELECT user, day_of_week, loopy, begintime, endtime FROM kq_items WHERE id = ?';
  36. let [projectResult] = await db.query(sqlGetProject, [project_id]);
  37. if (!projectResult || projectResult.length === 0) {
  38. res.json({
  39. ...BaseStdResponse.DATABASE_ERR,
  40. endpoint: 154754511
  41. });
  42. return;
  43. }
  44. let projectData = projectResult[0];
  45. let users = JSON.parse(projectData.user);
  46. if (!users.includes(uuid)) {
  47. res.json({
  48. ...BaseStdResponse.ERR,
  49. endpoint: 481454,
  50. msg: '用户不在考勤名单中'
  51. });
  52. return;
  53. }
  54. const isWithinTime = this.isWithinAttendanceTime(projectData);
  55. if (!isWithinTime) {
  56. return res.json({
  57. ...BaseStdResponse.ERR,
  58. endpoint: 513513,
  59. msg: '未在打卡时间段内'
  60. });
  61. }
  62. // 检查是否已存在考勤记录
  63. const sqlCheckRecord = 'SELECT id, uuid, time FROM kq_records WHERE project_id = ? AND uuid = ?';
  64. let [recordsResult] = await db.query(sqlCheckRecord, [project_id, uuid]);
  65. if (!recordsResult) {
  66. return res.json({
  67. ...BaseStdResponse.DATABASE_ERR,
  68. endpoint: 513513
  69. });
  70. }
  71. if (recordsResult.length !== 0 && this.hasRecord(projectData, recordsResult, uuid)) {
  72. return res.json({
  73. ...BaseStdResponse.ERR,
  74. endpoint: 513513,
  75. msg: '用户在本考勤周期已有考勤记录!'
  76. });
  77. }
  78. // 插入考勤记录
  79. const sqlInsertRecord = 'INSERT INTO kq_records (project_id, uuid, time) VALUES (?, ?, ?)';
  80. let [insertResult] = await db.query(sqlInsertRecord, [project_id, uuid, new Date().getTime()]);
  81. if (insertResult.affectedRows !== 1) {
  82. return res.json({
  83. ...BaseStdResponse.DATABASE_ERR,
  84. endpoint: 513513
  85. });
  86. }
  87. res.json({
  88. ...BaseStdResponse.OK
  89. });
  90. }
  91. isWithinAttendanceTime(attendanceTimes) {
  92. const now = new Date();
  93. const dayOfWeek = now.getDay();
  94. const nowTime = now.getTime();
  95. function timeToTodayTimestamp(timeStr) {
  96. const [hours, minutes, seconds] = timeStr.split(':').map(Number);
  97. const todayWithTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), hours, minutes, seconds);
  98. return todayWithTime.getTime();
  99. }
  100. const { createTime, day_of_week, begintime, endtime, loopy } = attendanceTimes;
  101. if (dayOfWeek === day_of_week && (loopy || (Number(createTime) + 604800000 - nowTime > 0)) && timeToTodayTimestamp(begintime) <= nowTime && timeToTodayTimestamp(endtime) >= nowTime) {
  102. return true;
  103. }
  104. return false;
  105. }
  106. hasRecord(attendanceData, records, uuid) {
  107. const { day_of_week, begintime, endtime, loopy } = attendanceData;
  108. if (!loopy) {
  109. return records.some(record => record.uuid === uuid);
  110. }
  111. const now = new Date();
  112. const dayOfWeek = now.getDay();
  113. const nowTime = now.getTime();
  114. function getTimestamp(time) {
  115. const daysUntilTarget = (day_of_week - dayOfWeek + 7) % 7;
  116. const targetDate = new Date(now);
  117. targetDate.setDate(now.getDate() + daysUntilTarget);
  118. const [hours, minutes, seconds] = time.split(':').map(Number);
  119. targetDate.setHours(hours, minutes, seconds, 0);
  120. return targetDate.getTime();
  121. }
  122. const begin = getTimestamp(begintime);
  123. const end = getTimestamp(endtime);
  124. if (nowTime >= begin) {
  125. return records.some(record => record.time >= begin && record.time <= end && record.uuid === uuid);
  126. }
  127. return false;
  128. }
  129. }
  130. module.exports.AddAttendanceRecord = AddAttendanceRecord;