tfse-app-api-v0.2/user/user_utils.py

86 lines
2.1 KiB
Python
Raw Normal View History

2022-02-08 17:07:20 +08:00
import re
import random
import time
from common.rsa import decrypt_data
2022-02-15 15:14:47 +08:00
from user.user_db import FIND_USER_INFO, FIND_VERIFY_CODE
2022-02-08 17:07:20 +08:00
def make_id(num):
"""
随机生成字符串
"""
choices = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
salt = ''
for i in range(num):
salt += random.choice(choices)
return salt
def check_mail_fmt(email):
"""
邮箱地址格式校验
Parameters:
email: 邮箱
Returns:
result: 邮箱校验结果正确返回True不正确返回False
"""
regex = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"
case = (len(email) > 7) and (re.match(regex, email) is not None)
result = True if case else False
return result
def check_pwd_fmt(pwd):
"""
密码强度校验
Parameters:
pwd: 密码已加密
Returns:
result: 密码强度校验结果正确返回True不正确返回False
"""
try:
password = decrypt_data(encrypt_msg=pwd)
except Exception:
return False
regex = "^(?![A-Za-z0-9]+$)(?![a-z0-9\\W]+$)(?![A-Za-z\\W]+$)(?![A-Z0-9\\W]+$)^.{8,}$"
case = (len(password) >= 8) and (re.match(regex, password) is not None)
result = True if case else False
return result
def check_registered(email):
"""
检查邮箱是否被注册
Parameters:
email: 邮箱
Returns:
邮箱未被注册 False
邮箱已被注册 True
"""
return False if FIND_USER_INFO({"邮箱": email}) == [] else True
def check_verify_code(email, code):
"""
验证码是否正确
Parameters:
email: 邮箱
code: 验证码
Returns:
没有邮箱记录 False
验证码不正确 False
验证码过期 False
验证码正确 True
"""
2022-02-15 15:14:47 +08:00
record = FIND_VERIFY_CODE({"邮箱": email})
2022-02-08 17:07:20 +08:00
if len(record) == 0:
return '验证码错误'
if record[0]['验证码'] != code:
return '验证码错误'
if time.time()-record[0]['验证有效期'] > 300:
return '验证码过期'
2022-02-15 11:04:55 +08:00
return True