Spaces:
Running
Running
Upload router_users.py
Browse files- router_users.py +36 -26
router_users.py
CHANGED
|
@@ -4,8 +4,8 @@ import time
|
|
| 4 |
import re
|
| 5 |
import random
|
| 6 |
import os
|
| 7 |
-
import
|
| 8 |
-
|
| 9 |
import 数据库连接 as db
|
| 10 |
from notifications import add_notification
|
| 11 |
from models import UserRegister, UserLogin, UserUpdate, PasswordReset, FollowToggle, PrivacySettings, SendCodeRequest
|
|
@@ -18,36 +18,46 @@ router = APIRouter()
|
|
| 18 |
VERIFY_CODES = {}
|
| 19 |
|
| 20 |
def send_email_code(to_email: str, code: str, action: str):
|
| 21 |
-
"""
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
if not sender or not pwd:
|
| 28 |
-
print("警告: 未配置 SMTP_USER 或 SMTP_PASS,跳过邮件发送")
|
| 29 |
return
|
| 30 |
|
| 31 |
action_str = "注册账号" if action == "register" else "修改/找回密码"
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
try:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
with smtplib.SMTP_SSL(server, port, timeout=10) as smtp:
|
| 41 |
-
smtp.login(sender, pwd)
|
| 42 |
-
smtp.send_message(msg)
|
| 43 |
-
else:
|
| 44 |
-
with smtplib.SMTP(server, port, timeout=10) as smtp:
|
| 45 |
-
smtp.starttls()
|
| 46 |
-
smtp.login(sender, pwd)
|
| 47 |
-
smtp.send_message(msg)
|
| 48 |
-
print(f"✅ 成功发送验证码 {code} 至 {to_email}")
|
| 49 |
except Exception as e:
|
| 50 |
-
print(f"❌
|
| 51 |
|
| 52 |
def send_sms_code(phone: str, code: str, action: str):
|
| 53 |
"""短信发送接口挂载点"""
|
|
|
|
| 4 |
import re
|
| 5 |
import random
|
| 6 |
import os
|
| 7 |
+
import json
|
| 8 |
+
import urllib.request
|
| 9 |
import 数据库连接 as db
|
| 10 |
from notifications import add_notification
|
| 11 |
from models import UserRegister, UserLogin, UserUpdate, PasswordReset, FollowToggle, PrivacySettings, SendCodeRequest
|
|
|
|
| 18 |
VERIFY_CODES = {}
|
| 19 |
|
| 20 |
def send_email_code(to_email: str, code: str, action: str):
|
| 21 |
+
"""利用自动化 Webhook 作为 HTTPS 到 SMTP 的代理桥梁"""
|
| 22 |
+
webhook_url = os.environ.get("MAKE_WEBHOOK_URL")
|
| 23 |
+
|
| 24 |
+
if not webhook_url:
|
| 25 |
+
print("警告: 未配置 MAKE_WEBHOOK_URL,跳过邮件发送")
|
|
|
|
|
|
|
|
|
|
| 26 |
return
|
| 27 |
|
| 28 |
action_str = "注册账号" if action == "register" else "修改/找回密码"
|
| 29 |
+
subject = f"ComfyUI 社区 - {action_str}验证码"
|
| 30 |
+
|
| 31 |
+
html_content = f"""
|
| 32 |
+
<div style="background:#f9f9f9; padding:20px; font-family:sans-serif;">
|
| 33 |
+
<div style="background:#fff; padding:20px; border-radius:8px; max-width:500px; margin:0 auto; box-shadow:0 2px 10px rgba(0,0,0,0.05);">
|
| 34 |
+
<h2 style="color:#4CAF50; margin-top:0;">ComfyUI 社区精选</h2>
|
| 35 |
+
<p>您好,</p>
|
| 36 |
+
<p>您正在请求<strong>{action_str}</strong>,您的验证码是:</p>
|
| 37 |
+
<div style="font-size:24px; font-weight:bold; color:#2196F3; background:#e3f2fd; padding:15px; text-align:center; border-radius:6px; letter-spacing: 5px;">{code}</div>
|
| 38 |
+
<p style="color:#888; font-size:12px; margin-top:20px;">该验证码在 10 分钟内有效。如非本人操作,请忽略此邮件。</p>
|
| 39 |
+
</div>
|
| 40 |
+
</div>
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
# 组装要发送给 Make.com 的数据
|
| 44 |
+
data = {
|
| 45 |
+
"to": to_email,
|
| 46 |
+
"subject": subject,
|
| 47 |
+
"html": html_content
|
| 48 |
+
}
|
| 49 |
|
| 50 |
+
# 发送普通的 HTTPS POST 请求,绝对不会被拦截
|
| 51 |
+
req = urllib.request.Request(
|
| 52 |
+
webhook_url,
|
| 53 |
+
data=json.dumps(data).encode('utf-8'),
|
| 54 |
+
headers={'Content-Type': 'application/json'}
|
| 55 |
+
)
|
| 56 |
try:
|
| 57 |
+
with urllib.request.urlopen(req, timeout=10) as response:
|
| 58 |
+
print(f"✅ 成功触发 Webhook 发送验证码 {code} 至 {to_email}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
+
print(f"❌ Webhook 触发失败: {e}")
|
| 61 |
|
| 62 |
def send_sms_code(phone: str, code: str, action: str):
|
| 63 |
"""短信发送接口挂载点"""
|