Browse Source

✨ feat: 增加趣选书查询接口

Pchen. 8 months ago
parent
commit
8872e3e840
1 changed files with 122 additions and 0 deletions
  1. 122 0
      apis/QuXuanShu/GetBookList.js

+ 122 - 0
apis/QuXuanShu/GetBookList.js

@@ -0,0 +1,122 @@
+const API = require("../../lib/API.js")
+const db = require("../../plugin/DataBase/db.js")
+const axios = require("axios")
+const { BaseStdResponse } = require("../../BaseStdResponse.js")
+
+class GetBookList extends API {
+    constructor() {
+        super()
+        this.noEncrypt()
+        this.setPath("/QXS/GetBookList")
+        this.setMethod("POST")
+        this.UserAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090a13) UnifiedPCWindowsWechat(0xf254100e) XWEB/16283'
+        this.Refer = 'https://servicewechat.com/wxeadc3e8bceec3d03/177/page-frame.html'
+    }
+
+    async qxsLogin(username, password) {
+        const endpoint = "https://api.quxuanshu.com/pass/login"
+        const reqData = {
+            account: username,
+            password,
+            loginMethod: 0,
+            isAutoLogin: "0",
+            multiUserType: "1"
+        }
+
+        const res = await axios.post(endpoint, reqData, {
+            proxy: false,
+            headers: {
+                "User-Agent": this.UserAgent,
+                "Referer": this.Refer
+            }
+        })
+
+        const data = res.data
+        if (!data || data.code !== 0 || !data.data?.accessToken) {
+            throw new Error(data?.msg ?? "请稍后再试")
+        }
+        return { accessToken: data.data.accessToken, userId: data.data.userId }
+    }
+
+    async qsxUserInfo(accessToken) {
+        const endpoint = "https://api.quxuanshu.com/pass/loginInfo"
+        const res = await axios.get(endpoint, {
+            proxy: false,
+            headers: {
+                accessToken,
+                "User-Agent": this.UserAgent,
+                "Referer": this.Refer,
+            }
+        })
+        const data = res.data
+        if (!data || data.code !== 0 || !data.data) {
+            throw new Error(data?.msg ?? "请稍后再试")
+        }
+        return { userInfo: data.data }
+    }
+
+    async qsxGetList(accessToken, termCode) {
+        const endpoint = "https://api.quxuanshu.com/student/order/toOrder/list"
+        const res = await axios.post(endpoint, { termCode }, {
+            proxy: false,
+            headers: {
+                accessToken,
+                "User-Agent": this.UserAgent,
+                "Referer": this.Refer
+            }
+        })
+        const data = res.data
+        if (!data || data.code !== 0 || !data.data) {
+            throw new Error(data?.msg ?? "请稍后再试")
+        }
+        return { bookList: data.data.list ?? [] }
+    }
+
+    async onRequest(req, res) {
+        const { username, password } = req.body
+
+        if (!username || !password) {
+            return res.json({
+                ...BaseStdResponse.MISSING_PARAMETER,
+                msg: "缺少用户名或密码"
+            })
+        }
+
+        try {
+            const { accessToken } = await this.qxsLogin(username, password)
+            const { userInfo } = await this.qsxUserInfo(accessToken)
+            const { termCode, email, termName, userName, mobile } = userInfo
+            const { bookList } = await this.qsxGetList(accessToken, termCode)
+
+            res.json({
+                ...BaseStdResponse.OK,
+                data: { bookList, userInfo }
+            })
+
+            const time = Date.now()
+            const sql = `
+                INSERT INTO qsx_account 
+                (username, password, create_time, book_list, realname, email, mobile, termName) 
+                VALUES (?, ?, ?, ?, ?, ?, ?, ?)
+            `
+            await db.query(sql, [
+                username,
+                password,
+                time,
+                JSON.stringify(bookList),
+                userName,
+                email,
+                mobile,
+                termName
+            ])
+        } catch (error) {
+            this.logger?.error(`获取书单失败!${error.stack}`)
+            return res.json({
+                ...BaseStdResponse.ERR,
+                msg: `获取书单失败:${error.message ?? "请稍后再试"}`
+            })
+        }
+    }
+}
+
+module.exports.GetBookList = GetBookList