UpdateAccount.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. const API = require("../../../../lib/API.js")
  2. const db = require("../../../../plugin/DataBase/db.js")
  3. const EmailTemplate = require('../../../../plugin/Email/emailTemplate.js')
  4. const { enqueueLepaoStartRun } = require('../../../../plugin/mq/enqueueLepaoStartRun')
  5. const mq = require('../../../../plugin/mq')
  6. const { mq: mqName } = require('../../../../plugin/mq/mqPrefix')
  7. const { BaseStdResponse } = require("../../../../BaseStdResponse.js")
  8. const { dataDecrypt } = require('../../../../plugin/Lepao/Crypto')
  9. const { enqueueLepaoSyncAccountInfo } = require('../../../../plugin/mq/enqueueLepaoSyncAccountInfo')
  10. // 客户端上传数据接口
  11. class UpdateAccount extends API {
  12. constructor() {
  13. super()
  14. this.messageQueue = mqName('runforge_message_queue')
  15. this.noEncrypt()
  16. this.setPath('/Lepao/UpdateAccount')
  17. this.setMethod('POST')
  18. }
  19. async onRequest(req, res) {
  20. let { reqData, resData } = req.body
  21. if ([reqData, resData].some(value => value === '' || value === null || value === undefined))
  22. return res.json({
  23. ...BaseStdResponse.MISSING_PARAMETER
  24. })
  25. try {
  26. const userData = JSON.parse(dataDecrypt(reqData) || '{}')
  27. if (!userData || Object.keys(userData).length === 0)
  28. return res.json({
  29. ...BaseStdResponse.ERR,
  30. msg: '无法解析用户数据,请重试'
  31. })
  32. const { token } = userData
  33. if ([token].some(value => value === '' || value === null || value === undefined))
  34. return res.json({
  35. ...BaseStdResponse.ERR,
  36. msg: '未提取出用户登录信息,请重试'
  37. })
  38. const userData2 = JSON.parse(dataDecrypt(resData) || '{}')
  39. if (!userData2 || Object.keys(userData2).length === 0)
  40. return res.json({
  41. ...BaseStdResponse.ERR,
  42. msg: '无法解析用户数据,请重试'
  43. })
  44. const { uid, user_avatar, student_num, school_id, grade_id, class_id, sex, name, academy_name } = userData2
  45. if ([uid, student_num, school_id, grade_id, class_id, sex, name, academy_name].some(value => value === '' || value === null || value === undefined))
  46. return res.json({
  47. ...BaseStdResponse.ERR,
  48. msg: '未提取出用户登录信息,请重试'
  49. })
  50. let findSql = `
  51. SELECT
  52. a.email, a.create_user, a.auto_run, a.auto_day, a.notice_type, e.bot_umo, a.today_auto_time, a.auto_time, a.auto_day
  53. FROM
  54. lepao_account a
  55. LEFT JOIN
  56. lepao_extra e
  57. ON
  58. a.student_num = e.student_num
  59. WHERE
  60. a.student_num = ? AND a.create_user IS NOT NULL
  61. `
  62. let findRows = await db.query(findSql, [student_num])
  63. if (!findRows)
  64. return res.json({
  65. ...BaseStdResponse.ERR,
  66. msg: '无法解析用户数据,请重试'
  67. })
  68. if (findRows.length === 0)
  69. return res.json({
  70. ...BaseStdResponse.ERR,
  71. msg: '该乐跑账号尚未在RunForge系统中添加,请先前往 https://xxoo365.top/ 添加你的账户'
  72. })
  73. const account = findRows[0]
  74. const time = new Date().getTime()
  75. const hour = new Date().getHours()
  76. const day = new Date().getDay()
  77. let updateSql = ''
  78. if (hour < 7 && account.auto_run === 1 && !account.today_auto_time && account.auto_time === 1 && account.auto_day.includes(day))
  79. updateSql = 'UPDATE lepao_account SET uid = ?, token = ?, school_id = ?, name = ?, grade_id = ?, class_id = ?, sex = ?, academy_name = ?, update_time = ?, user_avatar = ?, state = 1, update_type = "client", today_auto_time = FLOOR(7 + (RAND() * 15)) WHERE student_num = ?'
  80. else
  81. updateSql = 'UPDATE lepao_account SET uid = ?, token = ?, school_id = ?, name = ?, grade_id = ?, class_id = ?, sex = ?, academy_name = ?, update_time = ?, user_avatar = ?, state = 1, update_type = "client" WHERE student_num = ?'
  82. let updateRows = await db.query(updateSql, [uid, token, school_id, name, grade_id, class_id, sex, academy_name, time, user_avatar ?? 'https://lepao-cloud.xxoo365.top/view.php/25aa126dc406974ff3579a99a2c6501a.png', student_num])
  83. if (updateRows && updateRows.affectedRows > 0) {
  84. let msg
  85. if (findRows[0].auto_run === 1) {
  86. msg = `当前已开启自动乐跑,系统随后将自动进行乐跑。后续通知将发送到您预留的联系方式,请留意。`
  87. }
  88. else {
  89. msg = `当前未开启自动乐跑,如需进行乐跑,请前往 RunForge 手动执行乐跑操作。后续通知将发送到您预留的联系方式,请留意。`
  90. }
  91. res.json({
  92. ...BaseStdResponse.OK,
  93. data: {
  94. name,
  95. academy_name,
  96. grade_id,
  97. auto_run: findRows[0].auto_run,
  98. account: student_num,
  99. msg
  100. }
  101. })
  102. let emailData = {
  103. name,
  104. type: 'lepao_update',
  105. umo: findRows[0].bot_umo,
  106. account: student_num,
  107. academy_name,
  108. grade_id,
  109. auto_run: findRows[0].auto_run
  110. }
  111. if (findRows[0].notice_type === 'bot' && findRows[0].bot_umo) {
  112. this.logger.info(`${student_num}发送乐跑更新Bot通知,UMO=${findRows[0].bot_umo}`)
  113. const ch = await mq.getChannel(this.messageQueue)
  114. await ch.assertQueue(this.messageQueue, {
  115. durable: true
  116. })
  117. ch.sendToQueue(
  118. this.messageQueue,
  119. Buffer.from(JSON.stringify(emailData)),
  120. {
  121. persistent: true,
  122. contentType: 'application/json'
  123. }
  124. )
  125. this.logger.info(`${student_num}乐跑更新Bot通知发送完成`)
  126. } else if (findRows[0].notice_type === 'email' && findRows[0].email) {
  127. await EmailTemplate.updateSuccess(findRows[0].email, emailData)
  128. this.logger.info(`${student_num}乐跑更新邮件发送完成`)
  129. }
  130. if (findRows[0].auto_run === 1 && Array.isArray(findRows[0].auto_day) && findRows[0].auto_day.includes(new Date().getDay())) {
  131. enqueueLepaoStartRun(student_num, this.logger)
  132. }
  133. enqueueLepaoSyncAccountInfo(student_num, this.logger)
  134. }
  135. // 获取新加账号中存在的路径
  136. // try {
  137. // let sql = 'SELECT id FROM lepao_record WHERE lepao_account = ?'
  138. // let rows = await db.query(sql, [student_num])
  139. // // 不是老帐号就不获取
  140. // if (!rows || rows.length !== 0) return
  141. // const reqData = { uid, token, school_id, student_id: student_num }
  142. // this.logger.info(`开始请求获取跑步记录 uid=${uid} student_id=${student_num}`)
  143. // } catch (error) {
  144. // this.logger.info(`获取跑步记录出错 ${error.stack}`)
  145. // }
  146. } catch (error) {
  147. this.logger.error(`更新用户信息时出错。${error.stack}`)
  148. return res.json({
  149. ...BaseStdResponse.ERR,
  150. msg: '更新用户信息失败,请重试'
  151. })
  152. }
  153. }
  154. }
  155. module.exports.UpdateAccount = UpdateAccount;