GetBookList.js 7.1 KB

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