UpdateAccountAndroidApp.js 8.4 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 UpdateAccountAndroidApp extends API {
  12. constructor() {
  13. super()
  14. this.messageQueue = mqName('runforge_message_queue')
  15. this.setPath('/Lepao/UpdateAccountAndroidApp')
  16. this.setMethod('POST')
  17. }
  18. async onRequest(req, res) {
  19. let { reqData, resData, userAgent, deviceModel } = req.body
  20. if ([reqData, resData, userAgent, deviceModel].some(value => value === '' || value === null || value === undefined))
  21. return res.json({
  22. ...BaseStdResponse.MISSING_PARAMETER,
  23. msg: '账号信息不完整,请稍后再试获联系客服处理'
  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
  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系统中添加,请先添加你的账户'
  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 && Array.isArray(account.auto_day) && 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, userAgent = ?, deviceModel = ?, update_type = "android", 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, userAgent = ?, deviceModel = ?, update_type = "android" 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', userAgent ?? '', deviceModel ?? '', 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. user_avatar,
  96. sex,
  97. academy_name,
  98. grade_id,
  99. auto_run: findRows[0].auto_run,
  100. account: student_num,
  101. msg
  102. }
  103. })
  104. let emailData = {
  105. name,
  106. type: 'lepao_update',
  107. umo: findRows[0].bot_umo,
  108. account: student_num,
  109. academy_name,
  110. grade_id,
  111. auto_run: findRows[0].auto_run
  112. }
  113. if (findRows[0].notice_type === 'bot' && findRows[0].bot_umo) {
  114. this.logger.info(`${student_num}发送乐跑更新Bot通知,UMO=${findRows[0].bot_umo}`)
  115. const ch = await mq.getChannel(this.messageQueue)
  116. await ch.assertQueue(this.messageQueue, {
  117. durable: true
  118. })
  119. ch.sendToQueue(
  120. this.messageQueue,
  121. Buffer.from(JSON.stringify(emailData)),
  122. {
  123. persistent: true,
  124. contentType: 'application/json'
  125. }
  126. )
  127. this.logger.info(`${student_num}乐跑更新Bot通知发送完成`)
  128. } else if (findRows[0].notice_type === 'email' && findRows[0].email) {
  129. await EmailTemplate.updateSuccess(findRows[0].email, emailData)
  130. this.logger.info(`${student_num}乐跑更新邮件发送完成`)
  131. }
  132. if (findRows[0].auto_run === 1 && Array.isArray(findRows[0].auto_day) && findRows[0].auto_day.includes(new Date().getDay())) {
  133. enqueueLepaoStartRun(student_num, this.logger)
  134. }
  135. enqueueLepaoSyncAccountInfo(student_num, this.logger)
  136. }
  137. // 获取新加账号中存在的路径
  138. // try {
  139. // let sql = 'SELECT id FROM lepao_record WHERE lepao_account = ?'
  140. // let rows = await db.query(sql, [student_num])
  141. // // 不是老帐号就不获取
  142. // if (!rows || rows.length !== 0) return
  143. // const reqData = { uid, token, school_id, student_id: student_num }
  144. // this.logger.info(`开始请求获取跑步记录 uid=${uid} student_id=${student_num}`)
  145. // } catch (error) {
  146. // this.logger.info(`获取跑步记录出错 ${error.stack}`)
  147. // }
  148. } catch (error) {
  149. this.logger.error(`更新用户信息时出错。${error.stack}`)
  150. return res.json({
  151. ...BaseStdResponse.ERR,
  152. msg: '更新用户信息失败,请重试'
  153. })
  154. }
  155. }
  156. }
  157. module.exports.UpdateAccountAndroidApp = UpdateAccountAndroidApp