92 lines
2.7 KiB
Python
92 lines
2.7 KiB
Python
import time
|
||
import json
|
||
import random
|
||
import requests
|
||
|
||
from DBHelper.MongoHelper import MongoHelper
|
||
from Objects.Common.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()
|