GetBookList.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. const API = require("../../lib/API.js")
  2. const db = require("../../plugin/DataBase/db.js")
  3. const axios = require("axios")
  4. const OSS = require("ali-oss")
  5. const { BaseStdResponse } = require("../../BaseStdResponse.js")
  6. class GetBookList extends API {
  7. constructor() {
  8. super()
  9. this.setPath("/QXS/GetBookList")
  10. this.setMethod("POST")
  11. 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'
  12. this.Refer = 'https://servicewechat.com/wxeadc3e8bceec3d03/177/page-frame.html'
  13. }
  14. async qxsLogin(username, password) {
  15. const endpoint = "https://api.quxuanshu.com/pass/login"
  16. const reqData = {
  17. account: username,
  18. password,
  19. loginMethod: 0,
  20. isAutoLogin: "0",
  21. multiUserType: "0,1,2,3,4,5"
  22. }
  23. const res = await axios.post(endpoint, reqData, {
  24. proxy: false,
  25. headers: {
  26. "User-Agent": this.UserAgent,
  27. "Referer": this.Refer
  28. }
  29. })
  30. const data = res.data
  31. if (!data || data.code !== 0 || !data.data?.accessToken) {
  32. throw new Error(data?.msg ?? "请稍后再试")
  33. }
  34. return { accessToken: data.data.accessToken, userId: data.data.userId }
  35. }
  36. async qsxUserInfo(accessToken) {
  37. const endpoint = "https://api.quxuanshu.com/pass/loginInfo"
  38. const res = await axios.get(endpoint, {
  39. proxy: false,
  40. headers: {
  41. accessToken,
  42. "User-Agent": this.UserAgent,
  43. "Referer": this.Refer,
  44. }
  45. })
  46. const data = res.data
  47. if (!data || data.code !== 0 || !data.data) {
  48. throw new Error(data?.msg ?? "请稍后再试")
  49. }
  50. return { userInfo: data.data }
  51. }
  52. async qsxGetList(accessToken, termCode) {
  53. const endpoint = "https://api.quxuanshu.com/student/order/toOrder/list"
  54. const res = await axios.post(endpoint, { termCode }, {
  55. proxy: false,
  56. headers: {
  57. accessToken,
  58. "User-Agent": this.UserAgent,
  59. "Referer": this.Refer
  60. }
  61. })
  62. const data = res.data
  63. if (!data || data.code !== 0 || !data.data) {
  64. throw new Error(data?.msg ?? "请稍后再试")
  65. }
  66. return { bookList: data.data.list ?? [] }
  67. }
  68. async qsxGetBookDetail(accessToken, termCode, title, isbn) {
  69. const endpoint = "https://u.quxuanshu.com/api/teacher/textbook/textbookInfo/search"
  70. const queryData = {
  71. termCode,
  72. searchKey: title,
  73. platType: "3",
  74. curPage: 1,
  75. pageSize: 15,
  76. checkCode: "",
  77. awardTypeList: []
  78. }
  79. const res = await axios.post(endpoint, queryData, {
  80. proxy: false,
  81. headers: {
  82. "AccessToken": accessToken,
  83. "User-Agent": this.UserAgent,
  84. "Referer": this.Refer
  85. }
  86. })
  87. let data
  88. if (!res.data || !res.data.data || !res.data.data.list)
  89. return null
  90. else data = res.data.data.list
  91. const book = data.find(b => b.isbn === isbn)
  92. if (!book || !book.imgUrl)
  93. return null
  94. const imgUrl = await this.generateSignatureUrl(book.imgUrl)
  95. return imgUrl
  96. }
  97. async generateSignatureUrl(fileName) {
  98. // 获取预签名URL
  99. const client = await new OSS({
  100. accessKeyId: "LTAI5tB1ZicmzHDeS8KRsyRS",
  101. accessKeySecret: "sOtCbyJ3NdaAzizoxieN52VV1JkZYr",
  102. bucket: 'univ-common',
  103. region: 'oss-cn-hangzhou',
  104. secure: true,
  105. authorizationV4: true
  106. })
  107. return await client.signatureUrlV4('GET', 3600, {
  108. headers: {}
  109. }, fileName)
  110. }
  111. async onRequest(req, res) {
  112. const { username, password } = req.body
  113. if (!username || !password) {
  114. return res.json({
  115. ...BaseStdResponse.MISSING_PARAMETER,
  116. msg: "缺少用户名或密码"
  117. })
  118. }
  119. try {
  120. const { accessToken } = await this.qxsLogin(username, password)
  121. const { userInfo } = await this.qsxUserInfo(accessToken)
  122. const { termCode, email, termName, userName, mobile } = userInfo
  123. let { bookList } = await this.qsxGetList(accessToken, termCode)
  124. let teacherToken
  125. if (bookList.length > 0) {
  126. let { accessToken } = await this.qxsLogin('ctbu1991014', 'ctbu123456')
  127. teacherToken = accessToken
  128. await Promise.all(
  129. bookList.map(async (book) => {
  130. if (book.imgUrl) {
  131. book.imgUrl = await this.generateSignatureUrl(book.imgUrl)
  132. } else {
  133. book.imgUrl = await this.qsxGetBookDetail(teacherToken, termCode, book.title, book.isbn)
  134. }
  135. })
  136. )
  137. }
  138. res.json({
  139. ...BaseStdResponse.OK,
  140. data: { bookList, userInfo }
  141. })
  142. const time = Date.now()
  143. const sql = `
  144. INSERT INTO qsx_account
  145. (username, password, create_time, book_list, realname, email, mobile, termName)
  146. VALUES (?, ?, ?, ?, ?, ?, ?, ?)
  147. `
  148. await db.query(sql, [
  149. username,
  150. password,
  151. time,
  152. JSON.stringify(bookList),
  153. userName,
  154. email,
  155. mobile,
  156. termName
  157. ])
  158. } catch (error) {
  159. this.logger?.error(`${error.stack}`)
  160. return res.json({
  161. ...BaseStdResponse.ERR,
  162. msg: `${error.message ?? "请稍后再试"}`
  163. })
  164. }
  165. }
  166. }
  167. module.exports.GetBookList = GetBookList