GetJkesRunOverview.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. const API = require('../../lib/API.js')
  2. const db = require('../../plugin/DataBase/db.js')
  3. const { BaseStdResponse } = require('../../BaseStdResponse.js')
  4. const AccessControl = require('../../lib/AccessControl.js')
  5. const { fetchJkesMonthKm, fetchJkesTotalKm } = require('../../plugin/jkes/stats')
  6. const {
  7. readState,
  8. DOUBLE_RUNS_CAP,
  9. refreshMonthKmFromApi,
  10. normalizeMonthTargetKm
  11. } = require('../../plugin/jkes/monthPolicy')
  12. /**
  13. * 从 JKES 官方接口拉取本月/累计里程,并返回本地月度缓存。
  14. * monthTargetKm 为学校要求的「本月最低」参考,非可跑里程上限。
  15. */
  16. class GetJkesRunOverview extends API {
  17. constructor() {
  18. super()
  19. this.setPath('/Lepao/JkesRunOverview')
  20. this.setMethod('GET')
  21. }
  22. async onRequest(req, res) {
  23. const { uuid, session, student_num } = req.query
  24. if ([uuid, session, student_num].some((v) => v === '' || v == null))
  25. return res.json({
  26. ...BaseStdResponse.MISSING_PARAMETER
  27. })
  28. if (!await AccessControl.checkSession(uuid, session))
  29. return res.status(401).json({
  30. ...BaseStdResponse.ACCESS_DENIED
  31. })
  32. try {
  33. const rows = await db.query(
  34. `SELECT student_num, token, create_user, target_count
  35. FROM lepao_account WHERE student_num = ?`,
  36. [student_num]
  37. )
  38. if (!rows || rows.length === 0)
  39. return res.json({
  40. ...BaseStdResponse.ERR,
  41. msg: '未找到乐跑账号'
  42. })
  43. const acc = rows[0]
  44. if (acc.create_user !== uuid) {
  45. const permission = await AccessControl.getPermission(uuid)
  46. if (!permission.includes('admin') && !permission.includes('service'))
  47. return res.json({
  48. ...BaseStdResponse.ERR,
  49. msg: '无权查看该账号'
  50. })
  51. }
  52. if (!acc.token)
  53. return res.json({
  54. ...BaseStdResponse.ERR,
  55. msg: '账号未登录或 token 无效,请先更新账号'
  56. })
  57. const now = new Date()
  58. const y = now.getFullYear()
  59. const m = now.getMonth() + 1
  60. await refreshMonthKmFromApi(student_num, acc.token, now)
  61. const monthKm = await fetchJkesMonthKm(acc.token, y, m)
  62. const totalKm = await fetchJkesTotalKm(acc.token)
  63. const local = await readState(student_num, now)
  64. const monthTargetKm = normalizeMonthTargetKm(acc.target_count)
  65. return res.json({
  66. ...BaseStdResponse.OK,
  67. data: {
  68. student_num,
  69. monthKmOfficial: monthKm,
  70. totalKmOfficial: totalKm,
  71. monthKmCached: local.km,
  72. doubleRunsUsed: local.doubles,
  73. monthTargetKm,
  74. monthTargetIsMinimumNotCap: true,
  75. plannedDoubleRunDays: DOUBLE_RUNS_CAP
  76. }
  77. })
  78. } catch (e) {
  79. this.logger.error(`JkesRunOverview ${e.stack || e}`)
  80. return res.json({
  81. ...BaseStdResponse.ERR,
  82. msg: '获取 JKES 跑步数据失败'
  83. })
  84. }
  85. }
  86. }
  87. module.exports.GetJkesRunOverview = GetJkesRunOverview