From cf59941688a4c1620d6c6e1d1d12c8f2992bfd14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=9D=E5=B7=9D?= Date: Wed, 27 Apr 2022 11:31:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E9=80=81=E9=AA=8C=E8=AF=81=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E9=82=AE=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Implements/Others/EmailNotice.py | 21 ------- Implements/Others/EmailNoticeImpl.py | 91 ++++++++++++++++++++++++++++ ObjectsCommon/Others/EmailNotice.py | 21 +++++++ Routes/Other/EmailNoticeRoute.py | 25 ++++++++ Routes/Other/__init__.py | 0 app.py | 2 + 6 files changed, 139 insertions(+), 21 deletions(-) delete mode 100644 Implements/Others/EmailNotice.py create mode 100644 Implements/Others/EmailNoticeImpl.py create mode 100644 ObjectsCommon/Others/EmailNotice.py create mode 100644 Routes/Other/EmailNoticeRoute.py create mode 100644 Routes/Other/__init__.py diff --git a/Implements/Others/EmailNotice.py b/Implements/Others/EmailNotice.py deleted file mode 100644 index b2f9c39..0000000 --- a/Implements/Others/EmailNotice.py +++ /dev/null @@ -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): - """发送邮件""" diff --git a/Implements/Others/EmailNoticeImpl.py b/Implements/Others/EmailNoticeImpl.py new file mode 100644 index 0000000..c732471 --- /dev/null +++ b/Implements/Others/EmailNoticeImpl.py @@ -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() diff --git a/ObjectsCommon/Others/EmailNotice.py b/ObjectsCommon/Others/EmailNotice.py new file mode 100644 index 0000000..3768806 --- /dev/null +++ b/ObjectsCommon/Others/EmailNotice.py @@ -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): + """发送邮件""" diff --git a/Routes/Other/EmailNoticeRoute.py b/Routes/Other/EmailNoticeRoute.py new file mode 100644 index 0000000..069bc97 --- /dev/null +++ b/Routes/Other/EmailNoticeRoute.py @@ -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 diff --git a/Routes/Other/__init__.py b/Routes/Other/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app.py b/app.py index 281c928..b223ead 100644 --- a/app.py +++ b/app.py @@ -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')