Email supports password login, and the system configuration has added a new email login switch.

This commit is contained in:
Liujian committed 2025-07-23 15:50:14 +08:00
1 parent 3e9f0fcf43
commit 4d4f63a911
12 files changed
+278 -27

No files matched your search

+28 -5
View File
@@ -3,7 +3,9 @@ from sqlalchemy.orm import Session
from services.common.database import get_db
from services.common.utils.response_utils import ResponseUtils
from services.admin_service.services.auth_service import AuthService
from services.admin_service.services.sys_config_service import SysConfigService
from services.admin_service.utils.user_utils import UserUtils
from services.admin_service.constants import sys_config_key
router = APIRouter()
@@ -11,15 +13,36 @@ router = APIRouter()
def get_auth_service(db: Session = Depends(get_db)) -> AuthService:
return AuthService(db)
def get_sys_config_service(db: Session = Depends(get_db)) -> SysConfigService:
return SysConfigService(db)
@router.post("/email/sign", response_model=dict)
def email_login(body: dict = Body(...), auth_service: AuthService = Depends(get_auth_service)):
def email_login(
body: dict = Body(...),
auth_service: AuthService = Depends(get_auth_service),
sys_config_service: SysConfigService = Depends(get_sys_config_service),
):
"""Authenticate user via email and captcha code."""
email_is_enabled_raw = sys_config_service.get_value_by_key(sys_config_key.KEY_LOGIN_EMAIL_ENABLE) or "false"
# Convert to boolean
email_is_enabled = email_is_enabled_raw.lower() in ("true", "t", "yes", "y", "1")
if not email_is_enabled:
return ResponseUtils.error(message="email login not enabled", code=400)
email_mode = sys_config_service.get_value_by_key(sys_config_key.KEY_LOGIN_EMAIL_MODE) or "password"
email = body.get("user_email")
captcha = body.get("captcha")
if not email or not captcha:
return ResponseUtils.error(message="email and captcha required", code=400)
token = auth_service.email_login(email, captcha)
token = None
match email_mode:
case "password":
password = body.get("password")
if not email or not password:
return ResponseUtils.error(message="email and password required", code=400)
token = auth_service.email_login_by_password(email=email, password=password)
case "captcha":
captcha = body.get("captcha")
if not email or not captcha:
return ResponseUtils.error(message="email and captcha required", code=400)
token = auth_service.email_login_by_captcha(email=email, captcha=captcha)
if token:
return ResponseUtils.success({"user_token": token})
else:
@@ -13,8 +13,12 @@ from services.admin_service.constants.sys_config_key import (
KEY_HEADLINE,
KEY_SUBHEADLINE,
KEY_LANGUAGE,
KEY_THEME,
KEY_ABOUT_PAGE,
KEY_LOGIN_GOOGLE_CLIENT,
KEY_LOGIN_GOOGLE_ENABLE,
KEY_LOGIN_EMAIL_ENABLE,
KEY_LOGIN_EMAIL_MODE,
)
logger = logging.getLogger(__name__)
@@ -36,16 +40,27 @@ def get_config(db: Session = Depends(get_db)):
headline = sys_config_service.get_value_by_key(KEY_HEADLINE) or ""
subheadline = sys_config_service.get_value_by_key(KEY_SUBHEADLINE) or ""
language = sys_config_service.get_value_by_key(KEY_LANGUAGE) or ""
theme = sys_config_service.get_value_by_key(KEY_THEME) or ""
about_page = sys_config_service.get_value_by_key(KEY_ABOUT_PAGE) or ""
# Get login config
google_client_id = sys_config_service.get_value_by_key(KEY_LOGIN_GOOGLE_CLIENT) or ""
google_is_enabled_raw = sys_config_service.get_value_by_key(KEY_LOGIN_GOOGLE_ENABLE) or "false"
# Convert to boolean
google_is_enabled = google_is_enabled_raw.lower() in ("true", "t", "yes", "y", "1")
# Get login email config
email_is_enabled_raw = sys_config_service.get_value_by_key(KEY_LOGIN_EMAIL_ENABLE) or "false"
# Convert to boolean
email_is_enabled = email_is_enabled_raw.lower() in ("true", "t", "yes", "y", "1")
email_mode = sys_config_service.get_value_by_key(KEY_LOGIN_EMAIL_MODE) or "password"
# Build response data
config_data = {
"login": {"google": {"client_id": google_client_id, "is_enabled": google_is_enabled}},
"login": {
"google": {"client_id": google_client_id, "is_enabled": google_is_enabled},
"email": {"is_enabled": email_is_enabled, "mode": email_mode},
},
"platform": {
"name": platform_name,
"logo": platform_logo,
@@ -53,6 +68,8 @@ def get_config(db: Session = Depends(get_db)):
"headline": headline,
"subheadline": subheadline,
"language": language,
"theme": theme,
"about_page": about_page,
},
}
@@ -34,16 +34,27 @@ def get_sysconfig(
headline = sysconfig_service.get_value_by_key(sys_config_key.KEY_HEADLINE)
subheadline = sysconfig_service.get_value_by_key(sys_config_key.KEY_SUBHEADLINE)
language = sysconfig_service.get_value_by_key(sys_config_key.KEY_LANGUAGE)
theme = sysconfig_service.get_value_by_key(sys_config_key.KEY_THEME)
about_page = sysconfig_service.get_value_by_key(sys_config_key.KEY_ABOUT_PAGE,True)
admin_username = ""
if admin_user and admin_user.name:
admin_username = admin_user.name
login_google_client = sysconfig_service.get_value_by_key(sys_config_key.KEY_LOGIN_GOOGLE_CLIENT)
login_google_secret = sysconfig_service.get_value_by_key(sys_config_key.KEY_LOGIN_GOOGLE_SECRET)
login_google_enable = sysconfig_service.get_value_by_key(sys_config_key.KEY_LOGIN_GOOGLE_ENABLE)
if not login_google_enable:
login_google_enable = False
else:
login_google_enable = bool(login_google_enable)
google_is_enabled_raw = sysconfig_service.get_value_by_key(sys_config_key.KEY_LOGIN_GOOGLE_ENABLE) or "false"
# Convert to boolean
login_google_enable = google_is_enabled_raw.lower() in ("true", "t", "yes", "y", "1")
# Get login email config
email_is_enabled_raw = sysconfig_service.get_value_by_key(sys_config_key.KEY_LOGIN_EMAIL_ENABLE) or "false"
# Convert to boolean
login_email_enable = email_is_enabled_raw.lower() in ("true", "t", "yes", "y", "1")
login_email_mode = sysconfig_service.get_value_by_key(sys_config_key.KEY_LOGIN_EMAIL_MODE)
if not login_email_mode:
login_email_mode = "password"
# Get email configuration
email_smtp_host = sysconfig_service.get_value_by_key(sys_config_key.KEY_EMAIL_SMTP_HOST)
@@ -62,6 +73,8 @@ def get_sysconfig(
"headline": headline,
"subheadline": subheadline,
"language": language,
"theme": theme,
"about_page": about_page,
},
"account": {
"username": admin_username,
@@ -79,6 +92,10 @@ def get_sysconfig(
"client_secret": login_google_secret,
"is_enabled": login_google_enable,
},
"email":{
"is_enabled": login_email_enable,
"mode": login_email_mode,
}
},
}
)
@@ -119,6 +136,8 @@ def set_sysconfig(
headline = platform.get("headline")
subheadline = platform.get("subheadline")
language = platform.get("language")
theme = platform.get("theme")
about_page = platform.get("about_page")
account = body.get("account", {})
admin_username = account.get("username")
@@ -128,8 +147,11 @@ def set_sysconfig(
google_config = login.get("google", {})
login_google_client = google_config.get("client_id")
login_google_secret = google_config.get("client_secret")
login_google_enable = google_config.get("is_enabled", False) # 设置默认值为 False
login_google_enable = google_config.get("is_enabled") # 设置默认值为 False
login_email_config = login.get("email", {})
login_email_mode = login_email_config.get("mode")
login_email_is_enable = login_email_config.get("is_enabled") # 设置默认值为 False
print("email info:",login_email_is_enable,login_email_mode)
# 获取邮件配置 - 现在从外层获取
email_config = body.get("email", {})
email_smtp_host = email_config.get("smtp_host")
@@ -138,19 +160,27 @@ def set_sysconfig(
email_smtp_password = email_config.get("smtp_password")
email_smtp_sender = email_config.get("smtp_sender")
if not login_google_enable or login_google_enable == "false":
login_google_enable = "False"
else:
login_google_enable = "True"
# if login_google_enable:
# if login_google_enable == "false":
# login_google_enable = "False"
# else:
# login_google_enable = "True"
# if login_email_is_enable:
# if login_email_is_enable == "false":
# login_email_is_enable = "False"
# else:
# login_email_is_enable = "True"
# 批量更新配置
configs = [
(sys_config_key.KEY_PLATFORM_NAME, platform_name, "Platform name"),
(sys_config_key.KEY_PLATFORM_LOGO, platform_logo, "Platform logo"),
(sys_config_key.KEY_PLATFORM_LOGO, platform_logo, "Platform logo",),
(sys_config_key.KEY_PLATFORM_URL, platform_url, "Platform URL"),
(sys_config_key.KEY_WEBSITE_TITLE, website_title, "Website title"),
(sys_config_key.KEY_HEADLINE, headline, "Homepage title"),
(sys_config_key.KEY_SUBHEADLINE, subheadline, "Subtitle"),
(sys_config_key.KEY_LANGUAGE, language, "Language"),
(sys_config_key.KEY_THEME, theme, "Theme"),
(sys_config_key.KEY_ABOUT_PAGE, about_page, "About page"),
(sys_config_key.KEY_LOGIN_GOOGLE_CLIENT, login_google_client, "Google login client ID"),
(sys_config_key.KEY_LOGIN_GOOGLE_SECRET, login_google_secret, "Google login client secret"),
(sys_config_key.KEY_LOGIN_GOOGLE_ENABLE, login_google_enable, "谷歌登录是否启用"),
@@ -159,6 +189,8 @@ def set_sysconfig(
(sys_config_key.KEY_EMAIL_SMTP_USER, email_smtp_user, "邮件SMTP用户名"),
(sys_config_key.KEY_EMAIL_SMTP_PASSWORD, email_smtp_password, "邮件SMTP密码"),
(sys_config_key.KEY_EMAIL_SMTP_SENDER, email_smtp_sender, "邮件发送者地址"),
(sys_config_key.KEY_LOGIN_EMAIL_ENABLE, login_email_is_enable, "邮件是否启用"),
(sys_config_key.KEY_LOGIN_EMAIL_MODE, login_email_mode, "邮件模式"),
]
# 更新管理员用户名和密码
@@ -166,7 +198,10 @@ def set_sysconfig(
for key, value, desc in configs:
if value is not None: # 只更新有值的配置
sysconfig_service.set_value_by_key(key, value, desc)
is_large = False
if key == sys_config_key.KEY_ABOUT_PAGE:
is_large = True
sysconfig_service.set_value_by_key(key, value, desc,is_large)
return get_sysconfig(sysconfig_service, user_service)
@@ -0,0 +1,50 @@
from fastapi import APIRouter, UploadFile, Form, File
import hashlib
import os
import shutil
from datetime import datetime
from services.common.utils.response_utils import ResponseUtils
router = APIRouter()
@router.post("/image")
async def upload(
img: UploadFile = File(None),
sha256: str = Form(None),
):
# 1. 获取上传文件的名称
if not img:
return ResponseUtils.error(message="未提供文件", code=400)
filename = img.filename
if not filename:
return ResponseUtils.error(message="未提供文件名称", code=400)
# 2. 计算上传文件的sha256值
contents = await img.read()
file_hash = hashlib.sha256(contents).hexdigest()
# 重置文件指针,以便后续保存文件
await img.seek(0)
# 检查sha256是否一致
if sha256 and file_hash != sha256:
return ResponseUtils.error(message="文件SHA256校验失败", code=400)
# 3. 根据当前日期创建目录
today = datetime.now()
date_dir = today.strftime("%Y%m%d")
# 创建上传目录
upload_dir = os.path.join("uploads","images")
upload_dir = os.path.join(upload_dir, date_dir)
os.makedirs(upload_dir, exist_ok=True)
# 保存文件
file_path = os.path.join(upload_dir, filename)
with open(file_path, "wb") as f:
shutil.copyfileobj(img.file, f)
return ResponseUtils.success(data={
"file_path": file_path
})