Browse Source

增加提前结束功能

Pchen. 1 year ago
parent
commit
279769f1c7
2 changed files with 85 additions and 0 deletions
  1. 56 0
      apis/IC/Seat/EndAhead.js
  2. 29 0
      lib/IC/IC.js

+ 56 - 0
apis/IC/Seat/EndAhead.js

@@ -0,0 +1,56 @@
+const API = require("../../../lib/API");
+const AccessControl = require("../../../lib/AccessControl");
+const ic = require("../../../lib/IC/IC").IC;
+const { BaseStdResponse } = require("../../../BaseStdResponse");
+
+class EndAhead extends API {
+    constructor() {
+        super();
+
+        this.setPath('/IC/Seat/EndAhead')
+        this.setMethod('post')
+    }
+
+    async onRequest(req, res) {
+        let { uuid, session, username, seatuuid } = req.body
+
+        if ([uuid, session, username, seatuuid].some(value => value === '' || value === null || value === undefined))
+            return res.json({
+                ...BaseStdResponse.MISSING_PARAMETER
+            })
+
+        // 检查 session
+        if (!await AccessControl.checkSession(uuid, session))
+            return res.status(401).json({
+                ...BaseStdResponse.ACCESS_DENIED
+            })
+
+        const password = await AccessControl.checkJwAccount(uuid, username)
+        if(!password)
+            res.json({
+                ...BaseStdResponse.ERR,
+                msg: '教务系统账号未绑定或未激活'
+            })
+
+        try {
+            let cookie = await ic.getCookie(username, password)
+            let resdata = await ic.endAhead(cookie, seatuuid)
+            if (resdata.code === 300) {
+                cookie = await ic.resetCookie(username, password)
+                resdata = await ic.endAhead(cookie, seatuuid)
+            }
+
+            res.json({
+                ...BaseStdResponse.OK,
+                data: resdata.data
+            })
+        } catch (error) {
+            res.json({
+                ...BaseStdResponse.ERR,
+                msg: error.message || '取消预约失败!请稍后再试'
+            })
+        }
+    }
+}
+
+module.exports.EndAhead = EndAhead

+ 29 - 0
lib/IC/IC.js

@@ -163,6 +163,35 @@ class IC {
         });
     }
 
+    async endAhead(cookie, uuid) {
+        return new Promise(async (resolve, reject) => {
+
+            const url = 'https://ic.ctbu.edu.cn/ic-web/reserve/endAhaed'
+
+            try {
+                const res = await axios.post(url, { uuid }, {
+                    headers: {
+                        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
+                        'Cookie': cookie,
+                        'Accept-Language': 'zh-CN,zh;q=0.9'
+                    }
+                });
+
+                if (!res || res.data.code !== 0) {
+                    if (res.data.code === 300)
+                        return resolve(res.data)
+
+                    return reject(new Error(`提前结束失败!${res.data.message || ''}`))
+                }
+
+                resolve(res.data);
+            } catch (error) {
+                reject(error)
+            }
+        });
+    }
+
+
     async getCaptcha(cookie) {
         return new Promise(async (resolve, reject) => {
             const url = `https://ic.ctbu.edu.cn/ic-web/captcha?id=${new Date().getTime()}`