Files
sdu-course-bot/src/poll.ts

90 lines
3.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {IJsonCourse} from "./types"
import {getTimeNow, logAndNotifyUser, resetCookie, sduAxios, sleep} from "./includes"
import {appConfig} from "./config"
import * as qs from "qs"
export let reqBody = 'sEcho=1&iColumns=15&sColumns=&iDisplayStart=0&iDisplayLength=2000&mDataProp_0=kch&mDataProp_1=kcmc&mDataProp_2=kxhnew&mDataProp_3=jkfs&mDataProp_4=xmmc&mDataProp_5=fzmc&mDataProp_6=ktmc&mDataProp_7=xf&mDataProp_8=skls&mDataProp_9=sksj&mDataProp_10=skdd&mDataProp_11=xqmc&mDataProp_12=syrs&mDataProp_13=ctsm&mDataProp_14=czOper'
let currentErrorCount = 0
let totalErrorCount = 0
export const JsonCourseList = <{ bx: IJsonCourse[], xx: IJsonCourse[], rx: IJsonCourse[] }>{
bx: [],
xx: [],
rx: []
}
async function updateBx() {
try {
let resp = await sduAxios.post('/jsxsd/xsxkkc/xsxkBxxk?1=1&kcxx=&skls=&skfs=&xqid=', reqBody)
if (resp.data.aaData.length) {
JsonCourseList.bx.length = 0
JsonCourseList.bx.push(...resp.data.aaData)
}
} catch (_e) {
console.log('获取必修JsonList失败 at ' + getTimeNow())
await processError(_e as Error)
}
}
async function updateXx() {
try {
let resp = await sduAxios.post('/jsxsd/xsxkkc/xsxkXxxk?1=1&kcxx=&skls=&skfs=&xqid=', reqBody)
if (resp.data.aaData.length) {
JsonCourseList.xx.length = 0
JsonCourseList.xx.push(...resp.data.aaData)
}
} catch (_e) {
console.log('获取限选JsonList失败 at ' + getTimeNow())
await processError(_e as Error)
}
}
async function updateRx() {
try {
let resp = await sduAxios.post('/jsxsd/xsxkkc/xsxkGgxxkxk?kcxx=&skls=&skxq=&skjc=&sfym=false&sfct=false&szjylb=&sfxx=true&skfs=&xqid=', reqBody)
if (resp.data.aaData.length) {
JsonCourseList.rx.length = 0
JsonCourseList.rx.push(...resp.data.aaData)
}
} catch (_e) {
console.log('获取任选JsonList失败 at ' + getTimeNow())
await processError(_e as Error)
}
}
export async function poll() {
if (appConfig.channels.includes('bx')) {
await updateBx()
await sleep(appConfig.interval)
}
if (appConfig.channels.includes('xx')) {
await updateXx()
await sleep(appConfig.interval)
}
if (appConfig.channels.includes('rx')) {
await updateRx()
await sleep(appConfig.interval)
}
setImmediate(poll)
}
async function processError(e: Error) {
currentErrorCount++
if (currentErrorCount >= 30) {
console.log('[error]出现轮询error过多进行重新登录。上一次异常信息' + e.message)
try {
await resetCookie()
currentErrorCount = 0
} catch (_e) {
console.log('[error]重新登录失败')
}
if (totalErrorCount != -1) {
totalErrorCount++
}
}
if (totalErrorCount >= 10) {
logAndNotifyUser('[error]重新登录失败次数过多,请检查 at ' + getTimeNow())
totalErrorCount = -1
}
}