lepaoOfficialApi.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const { URLSearchParams } = require('url')
  2. const { dataEncrypt, dataDecrypt, dataSign } = require('../../plugin/Lepao/Crypto')
  3. const { postLepaoSchool } = require('./lepaoSchoolHttp')
  4. const BASE_URL = 'https://lepao.ctbu.edu.cn/v3/api.php'
  5. const DEFAULT_USER_AGENT = 'Mozilla/5.0 (Linux; Android 16; 2211133C Build/BP2A.250605.031.A3; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/138.0.7204.180 Mobile Safari/537.36 XWEB/1380347 MMWEBSDK/20250202 MMWEBID/1020 wxwork/5.0.6.66174 MicroMessenger/8.0.28.48(0x28001c30) MiniProgramEnv/android Luggage/3.0.2.95ef3f83 NetType/WIFI Language/zh_CN ABI/arm64'
  6. function lepaoTimestamp() {
  7. return Number((Date.now() / 1000).toFixed(3))
  8. }
  9. function lepaoNonce() {
  10. return String(Math.floor(Math.random() * 900000 + 100000))
  11. }
  12. function buildSignedRaw(account, extraFields = {}) {
  13. const studentNum = account.student_num || account.student_id
  14. const raw = {
  15. school_id: account.school_id,
  16. term_id: 0,
  17. course_id: 0,
  18. class_id: 0,
  19. student_num: studentNum,
  20. card_id: studentNum,
  21. uid: account.uid,
  22. token: account.token,
  23. timestamp: lepaoTimestamp(),
  24. version: 1,
  25. nonce: lepaoNonce(),
  26. ostype: 5,
  27. ...extraFields
  28. }
  29. raw.sign = dataSign(raw)
  30. return raw
  31. }
  32. function unwrapLepaoResponse(result) {
  33. if (result?.data && Number(result.is_encrypt) === 1) {
  34. const decrypted = dataDecrypt(result.data)
  35. if (!decrypted) {
  36. const err = new Error('官方乐跑接口解密失败')
  37. err.code = 'LEPAO_DECRYPT_FAILED'
  38. throw err
  39. }
  40. result.data = JSON.parse(decrypted)
  41. }
  42. const failedByStatus = Object.prototype.hasOwnProperty.call(result || {}, 'status') && Number(result.status) !== 1
  43. const failedByCode = Object.prototype.hasOwnProperty.call(result || {}, 'code') && Number(result.code) !== 1 && Number(result.code) !== 200
  44. if (failedByStatus || failedByCode) {
  45. const msg = result?.info || result?.msg || '官方乐跑接口请求失败'
  46. const err = new Error(msg)
  47. err.code = Number(result?.status) === 101 ? 'LEPAO_LOGIN_EXPIRED' : 'LEPAO_OFFICIAL_FAILED'
  48. err.result = result
  49. throw err
  50. }
  51. return result
  52. }
  53. async function postLepaoApi(pathSuffix, account, extraFields = {}, options = {}) {
  54. const raw = buildSignedRaw(account, extraFields)
  55. const form = new URLSearchParams()
  56. form.append('ostype', '5')
  57. form.append('data', dataEncrypt(JSON.stringify(raw)))
  58. const res = await postLepaoSchool(`${BASE_URL}${pathSuffix}`, form, {
  59. headers: {
  60. 'Content-Type': 'application/x-www-form-urlencoded',
  61. 'Accept': '*/*',
  62. 'Accept-Language': 'zh-CN,zh-Hans;q=0.9',
  63. 'Accept-Encoding': 'gzip, deflate, br',
  64. 'Referer': 'https://servicewechat.com/wxf94c4ddb63d87ede/32/page-frame.html',
  65. 'User-Agent': account.userAgent || DEFAULT_USER_AGENT
  66. },
  67. timeout: options.timeout || 20000,
  68. logger: options.logger
  69. })
  70. return unwrapLepaoResponse(res.data)
  71. }
  72. async function getCurrentTerm(account, options = {}) {
  73. const result = await postLepaoApi('/WpRun/getTermList', account, {}, options)
  74. const list = Array.isArray(result.data) ? result.data : []
  75. const current = list.find(item => String(item.status) === '1')
  76. if (!current?.term_id) {
  77. const err = new Error('未找到当前学期')
  78. err.code = 'LEPAO_TERM_NOT_FOUND'
  79. throw err
  80. }
  81. return current
  82. }
  83. async function getTermRunRecord(account, { termId, page = 1 } = {}, options = {}) {
  84. const currentPage = Number(page)
  85. return postLepaoApi(
  86. '/WpRun/getTermRunRecord',
  87. account,
  88. {
  89. term_id: String(termId),
  90. page: Number.isFinite(currentPage) && currentPage > 0 ? currentPage : 1
  91. },
  92. options
  93. )
  94. }
  95. module.exports = {
  96. getCurrentTerm,
  97. getTermRunRecord
  98. }