updateAccountCore.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const EmailTemplate = require('../Email/emailTemplate.js')
  2. const { enqueueLepaoStartRun } = require('../mq/enqueueLepaoStartRun')
  3. const mq = require('../mq')
  4. const { BaseStdResponse } = require('../../BaseStdResponse.js')
  5. const Redis = require('../DataBase/Redis.js')
  6. const jkesRedisKeys = require('./redisKeys.js')
  7. const { syncLepaoAccountFromToken } = require('./syncLepaoAccountFromToken.js')
  8. const { buildAutoRunTaskExtrasFromAccountRow } = require('./autoRunAccountOptions.js')
  9. async function executeLepaoTokenUpdate(ctx, req, res) {
  10. const { logger, messageQueue } = ctx
  11. const { token } = req.body
  12. if ([token].some((value) => value === '' || value === null || value === undefined)) {
  13. return res.json({
  14. ...BaseStdResponse.MISSING_PARAMETER
  15. })
  16. }
  17. try {
  18. const sync = await syncLepaoAccountFromToken(token)
  19. if (!sync.ok) {
  20. logger.error(`获取/更新用户信息失败: ${sync.msg}`)
  21. return res.json({
  22. ...BaseStdResponse.ERR,
  23. msg: sync.msg
  24. })
  25. }
  26. const { student_num, findRows, profile } = sync
  27. try {
  28. await Redis.set(jkesRedisKeys.runnerFlag(student_num), '1')
  29. } catch (e) {
  30. logger.error(`${student_num} 写入 jkes_runner 标记失败:${e.message || e}`)
  31. }
  32. let msg
  33. if (findRows[0].auto_run === 1) {
  34. msg = `当前已开启自动乐跑,系统将自动进行乐跑。后续通知将发送到您预留的联系方式,请留意。`
  35. } else {
  36. msg = `当前未开启自动乐跑,如需进行乐跑,请前往 哪吒乐跑 手动执行乐跑操作。后续通知将发送到您预留的联系方式,请留意。`
  37. }
  38. res.json({
  39. ...BaseStdResponse.OK,
  40. data: {
  41. name: profile.name,
  42. user_avatar: profile.user_avatar,
  43. sex: profile.sex,
  44. academy_name: profile.academy_name,
  45. grade_id: profile.grade_id,
  46. class_id: profile.class_id,
  47. auto_run: findRows[0].auto_run,
  48. account: student_num,
  49. msg
  50. }
  51. })
  52. const emailData = {
  53. name: profile.name,
  54. type: 'lepao_update',
  55. umo: findRows[0].bot_umo,
  56. account: student_num,
  57. academy_name: profile.academy_name,
  58. grade_id: profile.grade_id,
  59. auto_run: findRows[0].auto_run
  60. }
  61. if (findRows[0].notice_type === 'bot' && findRows[0].bot_umo) {
  62. logger.info(`${student_num}发送乐跑更新Bot通知,UMO=${findRows[0].bot_umo}`)
  63. const ch = await mq.getChannel(messageQueue)
  64. await ch.assertQueue(messageQueue, { durable: true })
  65. await mq.sendToQueueSafe(
  66. messageQueue,
  67. messageQueue,
  68. Buffer.from(JSON.stringify(emailData)),
  69. { persistent: true, contentType: 'application/json' }
  70. )
  71. logger.info(`${student_num}乐跑更新Bot通知发送完成`)
  72. } else if (findRows[0].notice_type === 'email' && findRows[0].email) {
  73. await EmailTemplate.updateSuccess(findRows[0].email, emailData)
  74. logger.info(`${student_num}乐跑更新邮件发送完成`)
  75. }
  76. if (
  77. findRows[0].auto_run === 1 &&
  78. Array.isArray(findRows[0].auto_day) &&
  79. findRows[0].auto_day.includes(new Date().getDay())
  80. ) {
  81. const { planJkesAutoRun } = require('./monthPolicy')
  82. planJkesAutoRun(student_num, findRows[0].auto_day, token, {
  83. monthTargetKm: findRows[0].target_count,
  84. stopAfterMinimum: true
  85. })
  86. .then((plan) => {
  87. if (plan.run) {
  88. const extras = buildAutoRunTaskExtrasFromAccountRow(findRows[0])
  89. enqueueLepaoStartRun(student_num, logger, {
  90. targetKm: extras.targetKm,
  91. autoDoubleSlot: extras.autoDoubleSlot,
  92. paceRandomMinSecPerKm: extras.paceRandomMinSecPerKm,
  93. paceRandomMaxSecPerKm: extras.paceRandomMaxSecPerKm
  94. })
  95. } else {
  96. logger.info(`${student_num} 登录后未触发 JKES 自动乐跑:${plan.reason}`)
  97. }
  98. })
  99. .catch((e) => {
  100. logger.error(`${student_num} JKES 自动乐跑计划失败:${e.message || e}`)
  101. })
  102. }
  103. } catch (error) {
  104. logger.error(`更新用户信息时出错。${error.stack}`)
  105. return res.json({
  106. ...BaseStdResponse.ERR,
  107. msg: '更新用户信息失败,请重试'
  108. })
  109. }
  110. }
  111. module.exports = {
  112. executeLepaoTokenUpdate
  113. }