|
|
|
@ -6,7 +6,9 @@ import json
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import random
|
|
|
|
import re
|
|
|
|
import re
|
|
|
|
|
|
|
|
import time
|
|
|
|
import smtplib
|
|
|
|
import smtplib
|
|
|
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from email.mime.text import MIMEText
|
|
|
|
from email.mime.text import MIMEText
|
|
|
|
from email.mime.multipart import MIMEMultipart
|
|
|
|
from email.mime.multipart import MIMEMultipart
|
|
|
|
from typing import Dict, List, Tuple, Optional
|
|
|
|
from typing import Dict, List, Tuple, Optional
|
|
|
|
@ -23,6 +25,8 @@ class DataManager:
|
|
|
|
self.sender_config = DEFAULT_SENDER_CONFIG
|
|
|
|
self.sender_config = DEFAULT_SENDER_CONFIG
|
|
|
|
self.ensure_data_files()
|
|
|
|
self.ensure_data_files()
|
|
|
|
self.load_email_config()
|
|
|
|
self.load_email_config()
|
|
|
|
|
|
|
|
self.verification_code_expiry = 0.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ensure_data_files(self):
|
|
|
|
def ensure_data_files(self):
|
|
|
|
"""确保数据文件存在"""
|
|
|
|
"""确保数据文件存在"""
|
|
|
|
@ -64,9 +68,7 @@ class DataManager:
|
|
|
|
if (self.sender_config["email"] == "your_email@qq.com" or
|
|
|
|
if (self.sender_config["email"] == "your_email@qq.com" or
|
|
|
|
self.sender_config["password"] == "your_app_password"):
|
|
|
|
self.sender_config["password"] == "your_app_password"):
|
|
|
|
print("请先配置发送邮箱信息")
|
|
|
|
print("请先配置发送邮箱信息")
|
|
|
|
# 在没有配置邮箱的情况下,返回验证码供测试使用
|
|
|
|
return False
|
|
|
|
print(f"测试模式 - 验证码: {verification_code}")
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 获取SMTP配置
|
|
|
|
# 获取SMTP配置
|
|
|
|
smtp_config = get_smtp_config(self.sender_config["email"])
|
|
|
|
smtp_config = get_smtp_config(self.sender_config["email"])
|
|
|
|
@ -104,16 +106,21 @@ class DataManager:
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
print(f"邮件发送失败: {e}")
|
|
|
|
print(f"邮件发送失败: {e}")
|
|
|
|
# 测试模式:如果邮件发送失败,在控制台显示验证码
|
|
|
|
return False # 返回True以便测试
|
|
|
|
print(f"测试模式 - 验证码: {verification_code}")
|
|
|
|
|
|
|
|
return True # 返回True以便测试
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def register_user(self, email: str, username: str) -> bool:
|
|
|
|
def register_user(self, email: str, username: str) -> bool:
|
|
|
|
"""用户注册,发送验证码到邮箱"""
|
|
|
|
"""用户注册,发送验证码到邮箱"""
|
|
|
|
users = self.load_users()
|
|
|
|
users = self.load_users()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if email in users:
|
|
|
|
if email in users:
|
|
|
|
raise ValueError("该邮箱已注册")
|
|
|
|
existing_user = users[email]
|
|
|
|
|
|
|
|
# 判断是否为未完成注册状态
|
|
|
|
|
|
|
|
if not existing_user.get("verified") or existing_user.get("password") is None:
|
|
|
|
|
|
|
|
del users[email] # 删除未完成的记录
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
raise ValueError("该邮箱已注册") # 已完成注册则提示
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 检查用户名是否已存在
|
|
|
|
# 检查用户名是否已存在
|
|
|
|
for user_data in users.values():
|
|
|
|
for user_data in users.values():
|
|
|
|
@ -126,11 +133,13 @@ class DataManager:
|
|
|
|
if not self.send_verification_email(email, verification_code):
|
|
|
|
if not self.send_verification_email(email, verification_code):
|
|
|
|
raise ValueError("验证码邮件发送失败,请检查邮箱地址")
|
|
|
|
raise ValueError("验证码邮件发送失败,请检查邮箱地址")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
code_generated_time = datetime.now().timestamp()
|
|
|
|
users[email] = {
|
|
|
|
users[email] = {
|
|
|
|
"username": username,
|
|
|
|
"username": username,
|
|
|
|
"verification_code": verification_code,
|
|
|
|
"verification_code": verification_code,
|
|
|
|
"verified": False,
|
|
|
|
"verified": False,
|
|
|
|
"password": None
|
|
|
|
"password": None,
|
|
|
|
|
|
|
|
"code_generated_time": code_generated_time
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
self.save_users(users)
|
|
|
|
self.save_users(users)
|
|
|
|
@ -143,8 +152,21 @@ class DataManager:
|
|
|
|
if email not in users:
|
|
|
|
if email not in users:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
if users[email]["verification_code"] == code:
|
|
|
|
user = users[email]
|
|
|
|
users[email]["verified"] = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
current_time = datetime.now().timestamp()
|
|
|
|
|
|
|
|
time_diff = current_time - user.get("code_generated_time", 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if time_diff > self.verification_code_expiry * 60:
|
|
|
|
|
|
|
|
# 验证码过期,删除无效记录
|
|
|
|
|
|
|
|
del users[email]
|
|
|
|
|
|
|
|
self.save_users(users)
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if user["verification_code"] == code:
|
|
|
|
|
|
|
|
user["verified"] = True
|
|
|
|
self.save_users(users)
|
|
|
|
self.save_users(users)
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
@ -161,9 +183,15 @@ class DataManager:
|
|
|
|
|
|
|
|
|
|
|
|
return has_upper and has_lower and has_digit
|
|
|
|
return has_upper and has_lower and has_digit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def set_password(self, email: str, password: str) -> bool:
|
|
|
|
def set_password(self, email: str, password: str) -> bool:
|
|
|
|
"""设置用户密码"""
|
|
|
|
"""设置用户密码"""
|
|
|
|
if not self.validate_password(password):
|
|
|
|
if not self.validate_password(password):
|
|
|
|
|
|
|
|
users = self.load_users()
|
|
|
|
|
|
|
|
if email in users and users[email]["verified"]:
|
|
|
|
|
|
|
|
del users[email]
|
|
|
|
|
|
|
|
self.save_users(users)
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
users = self.load_users()
|
|
|
|
users = self.load_users()
|
|
|
|
@ -172,6 +200,7 @@ class DataManager:
|
|
|
|
|
|
|
|
|
|
|
|
users[email]["password"] = password
|
|
|
|
users[email]["password"] = password
|
|
|
|
self.save_users(users)
|
|
|
|
self.save_users(users)
|
|
|
|
|
|
|
|
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def login(self, email: str, password: str) -> bool:
|
|
|
|
def login(self, email: str, password: str) -> bool:
|
|
|
|
@ -182,6 +211,20 @@ class DataManager:
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
user = users[email]
|
|
|
|
user = users[email]
|
|
|
|
|
|
|
|
# 新增:如果用户已验证但未设置密码,清理记录
|
|
|
|
|
|
|
|
if user["verified"] and user["password"] is None:
|
|
|
|
|
|
|
|
del users[email]
|
|
|
|
|
|
|
|
self.save_users(users)
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 检查验证码是否过期(针对未验证用户)
|
|
|
|
|
|
|
|
current_time = datetime.now().timestamp()
|
|
|
|
|
|
|
|
time_diff = current_time - user.get("code_generated_time", 0)
|
|
|
|
|
|
|
|
if time_diff > self.verification_code_expiry * 60:
|
|
|
|
|
|
|
|
del users[email]
|
|
|
|
|
|
|
|
self.save_users(users)
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
if user["verified"] and user["password"] == password:
|
|
|
|
if user["verified"] and user["password"] == password:
|
|
|
|
self.current_user = email
|
|
|
|
self.current_user = email
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
|