GetBookList.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const API = require("../../lib/API.js")
  2. const db = require("../../plugin/DataBase/db.js")
  3. const axios = require("axios")
  4. const { BaseStdResponse } = require("../../BaseStdResponse.js")
  5. class GetBookList extends API {
  6. constructor() {
  7. super()
  8. this.setPath("/QXS/GetBookList")
  9. this.setMethod("POST")
  10. 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'
  11. this.Refer = 'https://servicewechat.com/wxeadc3e8bceec3d03/177/page-frame.html'
  12. }
  13. async qxsLogin(username, password) {
  14. const endpoint = "https://api.quxuanshu.com/pass/login"
  15. const reqData = {
  16. account: username,
  17. password,
  18. loginMethod: 0,
  19. isAutoLogin: "0",
  20. multiUserType: "1"
  21. }
  22. const res = await axios.post(endpoint, reqData, {
  23. proxy: false,
  24. headers: {
  25. "User-Agent": this.UserAgent,
  26. "Referer": this.Refer
  27. }
  28. })
  29. const data = res.data
  30. if (!data || data.code !== 0 || !data.data?.accessToken) {
  31. throw new Error(data?.msg ?? "请稍后再试")
  32. }
  33. return { accessToken: data.data.accessToken, userId: data.data.userId }
  34. }
  35. async qsxUserInfo(accessToken) {
  36. const endpoint = "https://api.quxuanshu.com/pass/loginInfo"
  37. const res = await axios.get(endpoint, {
  38. proxy: false,
  39. headers: {
  40. accessToken,
  41. "User-Agent": this.UserAgent,
  42. "Referer": this.Refer,
  43. }
  44. })
  45. const data = res.data
  46. if (!data || data.code !== 0 || !data.data) {
  47. throw new Error(data?.msg ?? "请稍后再试")
  48. }
  49. return { userInfo: data.data }
  50. }
  51. async qsxGetList(accessToken, termCode) {
  52. const endpoint = "https://api.quxuanshu.com/student/order/toOrder/list"
  53. const res = await axios.post(endpoint, { termCode }, {
  54. proxy: false,
  55. headers: {
  56. accessToken,
  57. "User-Agent": this.UserAgent,
  58. "Referer": this.Refer
  59. }
  60. })
  61. const data = res.data
  62. if (!data || data.code !== 0 || !data.data) {
  63. throw new Error(data?.msg ?? "请稍后再试")
  64. }
  65. return { bookList: data.data.list ?? [] }
  66. }
  67. async onRequest(req, res) {
  68. const { username, password } = req.body
  69. if (!username || !password) {
  70. return res.json({
  71. ...BaseStdResponse.MISSING_PARAMETER,
  72. msg: "缺少用户名或密码"
  73. })
  74. }
  75. try {
  76. const { accessToken } = await this.qxsLogin(username, password)
  77. const { userInfo } = await this.qsxUserInfo(accessToken)
  78. const { termCode, email, termName, userName, mobile } = userInfo
  79. const { bookList } = await this.qsxGetList(accessToken, termCode)
  80. res.json({
  81. ...BaseStdResponse.OK,
  82. data: { bookList, userInfo }
  83. })
  84. const time = Date.now()
  85. const sql = `
  86. INSERT INTO qsx_account
  87. (username, password, create_time, book_list, realname, email, mobile, termName)
  88. VALUES (?, ?, ?, ?, ?, ?, ?, ?)
  89. `
  90. await db.query(sql, [
  91. username,
  92. password,
  93. time,
  94. JSON.stringify(bookList),
  95. userName,
  96. email,
  97. mobile,
  98. termName
  99. ])
  100. } catch (error) {
  101. this.logger?.error(`获取书单失败!${error.stack}`)
  102. return res.json({
  103. ...BaseStdResponse.ERR,
  104. msg: `获取书单失败:${error.message ?? "请稍后再试"}`
  105. })
  106. }
  107. }
  108. }
  109. module.exports.GetBookList = GetBookList