GetBookList.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. if (data.msg === '统一订购模式下无权限查看订购数据!') {
  65. const endpoint = "https://api.quxuanshu.com/student/order/orderInfo/list"
  66. const res = await axios.post(endpoint, { type: 0, termCode }, {
  67. proxy: false,
  68. headers: {
  69. accessToken,
  70. "User-Agent": this.UserAgent,
  71. "Referer": this.Refer
  72. }
  73. })
  74. const data = res.data
  75. if (!data || data.code !== 0 || !data.data || !data.data.list[0] || !data.data.list[0].orderItem) {
  76. throw new Error(data?.msg ?? "请稍后再试")
  77. }
  78. return { bookList: data.data.list[0].orderItem ?? [] }
  79. }
  80. throw new Error(data?.msg ?? "请稍后再试")
  81. }
  82. return { bookList: data.data.list ?? [] }
  83. }
  84. async qsxGetBookDetail(accessToken, termCode, title, isbn) {
  85. const endpoint = "https://u.quxuanshu.com/api/teacher/textbook/textbookInfo/search"
  86. const queryData = {
  87. termCode,
  88. searchKey: title,
  89. platType: "3",
  90. curPage: 1,
  91. pageSize: 15,
  92. checkCode: "",
  93. awardTypeList: []
  94. }
  95. const res = await axios.post(endpoint, queryData, {
  96. proxy: false,
  97. headers: {
  98. "AccessToken": accessToken,
  99. "User-Agent": this.UserAgent,
  100. "Referer": this.Refer
  101. }
  102. })
  103. let data
  104. if (!res.data || !res.data.data || !res.data.data.list)
  105. return null
  106. else data = res.data.data.list
  107. const book = data.find(b => b.isbn === isbn)
  108. if (!book || !book.imgUrl)
  109. return null
  110. const imgUrl = await this.generateSignatureUrl(book.imgUrl)
  111. return imgUrl
  112. }
  113. async generateSignatureUrl(fileName) {
  114. // 获取预签名URL
  115. const client = await new OSS({
  116. accessKeyId: "LTAI5tB1ZicmzHDeS8KRsyRS",
  117. accessKeySecret: "sOtCbyJ3NdaAzizoxieN52VV1JkZYr",
  118. bucket: 'univ-common',
  119. region: 'oss-cn-hangzhou',
  120. secure: true,
  121. authorizationV4: true
  122. })
  123. return await client.signatureUrlV4('GET', 3600, {
  124. headers: {}
  125. }, fileName)
  126. }
  127. async onRequest(req, res) {
  128. const { username, password } = req.body
  129. if (!username || !password) {
  130. return res.json({
  131. ...BaseStdResponse.MISSING_PARAMETER,
  132. msg: "缺少用户名或密码"
  133. })
  134. }
  135. try {
  136. const { accessToken } = await this.qxsLogin(username, password)
  137. const { userInfo } = await this.qsxUserInfo(accessToken)
  138. const { termCode, email, termName, userName, mobile } = userInfo
  139. let { bookList } = await this.qsxGetList(accessToken, termCode)
  140. let teacherToken
  141. if (bookList.length > 0) {
  142. let { accessToken } = await this.qxsLogin('ctbu1991014', 'ctbu123456')
  143. teacherToken = accessToken
  144. await Promise.all(
  145. bookList.map(async (book) => {
  146. if (book.imgUrl) {
  147. book.imgUrl = await this.generateSignatureUrl(book.imgUrl)
  148. } else {
  149. book.imgUrl = await this.qsxGetBookDetail(teacherToken, termCode, book.title, book.isbn)
  150. }
  151. })
  152. )
  153. }
  154. res.json({
  155. ...BaseStdResponse.OK,
  156. data: { bookList, userInfo }
  157. })
  158. const time = Date.now()
  159. const sql = `
  160. INSERT INTO qsx_account
  161. (username, password, create_time, book_list, realname, email, mobile, termName)
  162. VALUES (?, ?, ?, ?, ?, ?, ?, ?)
  163. `
  164. await db.query(sql, [
  165. username,
  166. password,
  167. time,
  168. JSON.stringify(bookList),
  169. userName,
  170. email,
  171. mobile,
  172. termName
  173. ])
  174. } catch (error) {
  175. this.logger?.error(`${error.stack}`)
  176. return res.json({
  177. ...BaseStdResponse.ERR,
  178. msg: `${error.message ?? "请稍后再试"}`
  179. })
  180. }
  181. }
  182. }
  183. module.exports.GetBookList = GetBookList