1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- 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("/User/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)
- 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 = '您正在注册Double_X考勤系统账号,';
- break;
- case 'forget':
- content = '您正在找回Double_X考勤系统账号,';
- 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
- });
- await sendEmail(email, '验证码', `${content}您的验证码为:${code}。此验证码10分钟内有效,请妥善保管您的验证码,非本人操作请忽略。`);
- } catch (err) {
- this.logger.error(`发送邮箱验证码失败!${err.stack}`);
- return res.json({
- ...BaseStdResponse.SMS_SEND_FAIL,
- msg: '请检查邮箱格式后再试!'
- })
- }
- res.json({
- ...BaseStdResponse.OK
- })
- }
- }
- module.exports.SendEmail = SendEmail;
|