12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- const API = require("../../lib/API");
- const { BaseStdResponse } = require("../../BaseStdResponse");
- const Redis = require('../../plugin/DataBase/Redis');
- const sendEmail = require('../../plugin/Email/Email');
- // 发送邮箱验证码
- class SendEmail extends API {
- constructor() {
- super();
- this.setMethod("POST");
- this.setPath("/Captcha/SendEmail");
- }
- async onRequest(req, res) {
- const { email, text, id, type } = req.body;
- if ([email, text, id, type].some(value => value === '' || value === null || value === undefined)) {
- res.json({
- ...BaseStdResponse.MISSING_PARAMETER,
- endpoint: 1513126
- });
- return;
- }
- try {
- const code = await Redis.get(`captcha:${id}`);
- if (!code || code != text.toLowerCase())
- return res.json({
- ...BaseStdResponse.SMS_CHECK_FAIL,
- msg: '验证码输入错误或已过期'
- })
- await Redis.del(`captcha:${id}`);
- } catch (err) {
- this.logger.error(`验证图片验证码失败!${err.stack}`);
- return res.json({
- ...BaseStdResponse.DATABASE_ERR,
- msg: '验证失败!'
- })
- }
- let content;
- switch (type) {
- case 'register':
- content = '您正在注册GitNexus账号,';
- break;
- case 'forget':
- content = '您正在找回GitNexus账号,';
- break;
- case 'bind':
- content = '您正在进行换绑邮箱操作,';
- break;
- default:
- return res.json({
- ...BaseStdResponse.METHOD_NOT_EXIST
- })
- }
- const code = Math.random().toFixed(6).slice(-6);
- try {
- await Redis.set(`email:${email}`, code, {
- EX: 600
- });
- } catch (err) {
- this.logger.error(`发送邮箱验证码失败!${err.stack}`);
- return res.json({
- ...BaseStdResponse.SMS_SEND_FAIL,
- msg: '请检查邮箱格式后再试!'
- })
- }
- res.json({
- ...BaseStdResponse.OK
- })
- // 先返回后发送
- await sendEmail(email, '验证码', `${content}您的验证码为:${code}。此验证码10分钟内有效,请妥善保管您的验证码,非本人操作请忽略。`);
- }
- }
- module.exports.SendEmail = SendEmail;
|