SendEmail.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const API = require("../../lib/API")
  2. const { BaseStdResponse } = require("../../BaseStdResponse")
  3. const Redis = require('../../plugin/DataBase/Redis')
  4. const EmailTemplate = require('../../plugin/Email/emailTemplate')
  5. // 发送邮箱验证码
  6. class SendEmail extends API {
  7. constructor() {
  8. super()
  9. this.setMethod("POST")
  10. this.setPath("/Captcha/SendEmail")
  11. }
  12. async onRequest(req, res) {
  13. const { email, text, id, type } = req.body
  14. if ([email, text, id, type].some(value => value === '' || value === null || value === undefined))
  15. return res.json({
  16. ...BaseStdResponse.MISSING_PARAMETER,
  17. endpoint: 1513126
  18. })
  19. try {
  20. const code = await Redis.get(`captcha:${id}`)
  21. if (!code || code != text.toLowerCase())
  22. return res.json({
  23. ...BaseStdResponse.SMS_CHECK_FAIL,
  24. msg: '验证码输入错误或已过期'
  25. })
  26. await Redis.del(`captcha:${id}`)
  27. } catch (err) {
  28. this.logger.error(`验证图片验证码失败!${err.stack}`)
  29. return res.json({
  30. ...BaseStdResponse.DATABASE_ERR,
  31. msg: '验证失败!'
  32. })
  33. }
  34. const code = Math.random().toFixed(6).slice(-6)
  35. try {
  36. await Redis.set(`email:${email}`, code, {
  37. EX: 600
  38. })
  39. } catch (err) {
  40. this.logger.error(`发送邮箱验证码失败!${err.stack}`)
  41. return res.json({
  42. ...BaseStdResponse.SMS_SEND_FAIL,
  43. msg: '请检查邮箱格式后再试!'
  44. })
  45. }
  46. res.json({
  47. ...BaseStdResponse.OK
  48. })
  49. // 先返回后发送
  50. EmailTemplate.checkEmail(email, code)
  51. }
  52. }
  53. module.exports.SendEmail = SendEmail