86 lines
2.1 KiB
Python
86 lines
2.1 KiB
Python
import re
|
||
import random
|
||
import time
|
||
|
||
from common.rsa import decrypt_data
|
||
from user.user_db import FIND_USER_INFO, FIND_VERIFY_CODE
|
||
|
||
|
||
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
|
||
"""
|
||
record = FIND_VERIFY_CODE({"邮箱": email})
|
||
if len(record) == 0:
|
||
return '验证码错误'
|
||
if record[0]['验证码'] != code:
|
||
return '验证码错误'
|
||
if time.time()-record[0]['验证有效期'] > 300:
|
||
return '验证码过期'
|
||
return True
|