GetOfficialTermRecords.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const API = require('../../../lib/API')
  2. const AccessControl = require('../../../lib/AccessControl')
  3. const { BaseStdResponse } = require('../../../BaseStdResponse')
  4. const { getCurrentTerm, getTermRunRecord } = require('../../../lib/Lepao/lepaoOfficialApi')
  5. const {
  6. isMissing,
  7. loadUserOfficialAccount,
  8. mapOfficialError,
  9. syncOfficialTotalNum
  10. } = require('../../../lib/Lepao/officialAccountAccess')
  11. class GetOfficialTermRecords extends API {
  12. constructor() {
  13. super()
  14. this.setPath('/Lepao/OfficialTermRecords')
  15. this.setMethod('GET')
  16. }
  17. async onRequest(req, res) {
  18. const { uuid, session, student_num } = req.query
  19. const page = Number(req.query.page || 1)
  20. if ([uuid, session, student_num].some(isMissing)) {
  21. return res.json({
  22. ...BaseStdResponse.MISSING_PARAMETER
  23. })
  24. }
  25. if (!Number.isFinite(page) || page <= 0) {
  26. return res.json({
  27. ...BaseStdResponse.ERR,
  28. msg: '参数错误'
  29. })
  30. }
  31. if (!await AccessControl.checkSession(uuid, session)) {
  32. return res.status(401).json({
  33. ...BaseStdResponse.ACCESS_DENIED
  34. })
  35. }
  36. try {
  37. const { account, error } = await loadUserOfficialAccount(uuid, student_num)
  38. if (error) return res.json(error)
  39. const currentTerm = await getCurrentTerm(account, { logger: this.logger })
  40. const recordResult = await getTermRunRecord(
  41. account,
  42. { termId: currentTerm.term_id, page },
  43. { logger: this.logger }
  44. )
  45. const recordData = recordResult.data || {}
  46. await syncOfficialTotalNum(student_num, recordData, this.logger)
  47. return res.json({
  48. ...BaseStdResponse.OK,
  49. data: {
  50. term_id: currentTerm.term_id,
  51. term_name: currentTerm.term_name,
  52. page,
  53. ...recordData
  54. }
  55. })
  56. } catch (error) {
  57. this.logger.error(`获取官方学期乐跑记录失败 ${student_num}: ${error.stack || error}`)
  58. return res.json(await mapOfficialError(error, student_num, this.logger))
  59. }
  60. }
  61. }
  62. module.exports.GetOfficialTermRecords = GetOfficialTermRecords