发送验证信息邮件

This commit is contained in:
王思川 2022-04-27 11:31:14 +08:00
parent 0ee3b3f579
commit cf59941688
6 changed files with 139 additions and 21 deletions

View File

@ -1,21 +0,0 @@
from Utils.ObjUtil import SpecObject
from Utils.ValidateUtil import ValidateAttr, Validate
class EmailNotice(SpecObject):
"""用户邮件通知"""
email = ValidateAttr(field='email', func=Validate.email, error_info='邮箱格式异常', error_code=200)
v_period = ValidateAttr(field='v_period', type=float, error_info='验证有效期格式异常', error_code=200)
v_code = ValidateAttr(field='v_code', type=str, length=6, error_info='验证码格式异常', error_code=200)
v_type = ValidateAttr(field="v_type", type=str, in_list=["register", "resetemail", "resetpwd"], error_info="邮件验证类型错误", error_code=200)
fields_map = {
"email": "邮箱",
"v_period": "验证有效期",
"v_code": "验证码",
"v_type": "验证类型"
}
def send_email(self):
"""发送邮件"""

View File

@ -0,0 +1,91 @@
import time
import json
import random
import requests
from DBHelper.MongoHelper import MongoHelper
from ObjectsCommon.Others.EmailNotice import EmailNotice
from Utils.ErrorUtil import ReturnConditionCheckFailed
class EmailNoticeImpl(EmailNotice):
"""邮件通知实现类"""
db = MongoHelper("tfse_v0.21")
def make_verify_code(self):
"""生成邮箱验证码"""
def make_verify_code():
"""随机生成6位数字验证码"""
num_code = ""
for i in range(6):
ch = chr(random.randrange(ord('0'), ord('9') + 1))
num_code += ch
return num_code
self.v_code = make_verify_code()
self.v_period = time.time() + 300
self.db.upsert_single_data(
"应用端",
"邮箱验证码记录",
{"邮箱": self.email},
self.dict_to_save()
)
def transfer_email_api(self):
"""发送邮件"""
# 邮箱服务接口
email_api = 'http://116.63.130.34:30001'
v_types = {
"register": "注册账号",
"resetpwd": "修改密码",
"resetemail": "修改邮箱"
}
headers = {"Content-Type": "application/json;charset=UTF-8"}
data = {
"title": "【远东资信】{}".format(v_types[self.v_type]),
"sender": 'fecribd@fecr.com.cn',
"recipients": [self.email],
"msg_body": "{}的验证码为 【{}5分钟内有效。".format(v_types[self.v_type], self.v_code)
}
response = requests.post(url=email_api + '/send_mail', headers=headers, data=json.dumps(data))
return response.text
def check_email_registered(self):
"""检查邮箱已被注册"""
email = self.db.find_single_column(
"应用端",
"企业用户",
{"邮箱": self.email},
"邮箱"
)
if email is not None:
raise ReturnConditionCheckFailed("邮箱已被注册", 200)
def check_email_not_registered(self):
"""检查邮箱未被注册"""
email = self.db.find_single_column(
"应用端",
"企业用户",
{"邮箱": self.email},
"邮箱"
)
if email is None:
raise ReturnConditionCheckFailed("邮箱未被注册", 200)
def send_email(self):
"""发送通知邮件"""
if self.v_type == 'register' or self.v_type == 'resetemail':
self.check_email_registered()
self.make_verify_code()
self.transfer_email_api()
elif self.v_type == 'resetpwd':
self.check_email_not_registered()
self.make_verify_code()
self.transfer_email_api()

View File

@ -0,0 +1,21 @@
from Utils.ObjUtil import SpecObject
from Utils.ValidateUtil import ValidateAttr, Validate
class EmailNotice(SpecObject):
"""用户邮件通知"""
email = ValidateAttr(field='email', func=Validate.email)
v_period = ValidateAttr(field='v_period', type=float)
v_code = ValidateAttr(field='v_code', type=str, length=6)
v_type = ValidateAttr(field="v_type", in_list=["register", "resetemail", "resetpwd"])
fields_map = {
"email": "邮箱",
"v_period": "验证有效期",
"v_code": "验证码",
"v_type": "验证类型"
}
def send_email(self):
"""发送邮件"""

View File

@ -0,0 +1,25 @@
from flask import Blueprint, request, Response
from Implements.Others.EmailNoticeImpl import EmailNoticeImpl
from Utils.AuthUtil import check_block
from Utils.ErrorUtil import ReturnConditionCheckFailed
email_route = Blueprint('email', __name__)
@email_route.route('/send_verify_info', methods=['POST'])
@check_block
def verify_email():
"""发送邮箱验证码"""
try:
req = request.json
impl = EmailNoticeImpl()
impl.email = req['email']
impl.v_type = req['v_type']
impl.send_email()
return {"info": "邮件已发送"}, 200
except ReturnConditionCheckFailed as e:
e.log_error()
return {"info": e.failed_info}, e.status_code
except KeyError:
return {"info": "参数异常"}, 400

0
Routes/Other/__init__.py Normal file
View File

2
app.py
View File

@ -3,6 +3,7 @@ from flask_cors import *
from Routes.Company.Index import company_route
from Routes.Other.EmailNoticeRoute import email_route
from Routes.User.TFSECompanyUserRoute import company_user_route
from file.file_routes import file_route
@ -17,6 +18,7 @@ app.config['JSON_SORT_KEYS'] = False
app.register_blueprint(company_route, url_prefix='/app/company')
app.register_blueprint(file_route, url_prefix='/app/file')
app.register_blueprint(company_user_route, url_prefix='/app/user')
app.register_blueprint(email_route, url_prefix='/app/notice/email')
app.register_blueprint(credit_route, url_prefix='/app/input')
app.register_blueprint(esg_route, url_prefix='/app/esg')