Merge cangpin v1.2.12 to main - resolve conflict
This commit is contained in:
commit
8c5c6a0b79
|
|
@ -3,18 +3,20 @@ from sqlalchemy import create_engine
|
|||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
# 支持 MySQL, PostgreSQL
|
||||
DATABASE_URL = os.getenv(
|
||||
"DATABASE_URL",
|
||||
"postgresql://postgres:postgres@localhost:5432/zodiac"
|
||||
DATABASE_URL,
|
||||
postgresql://postgres:postgres@localhost:5432/zodiac
|
||||
)
|
||||
|
||||
# 数据库引擎配置
|
||||
engine = create_engine(
|
||||
DATABASE_URL,
|
||||
pool_pre_ping=True,
|
||||
pool_size=10,
|
||||
max_overflow=20,
|
||||
pool_recycle=3600,
|
||||
pool_timeout=30,
|
||||
pool_recycle=3600, # 1小时回收连接
|
||||
pool_timeout=30, # 获取连接超时30秒
|
||||
echo=False
|
||||
)
|
||||
|
||||
|
|
@ -24,6 +26,7 @@ Base = declarative_base()
|
|||
|
||||
|
||||
def get_db():
|
||||
"""获取数据库会话"""
|
||||
db = SessionLocal(expire_on_commit=False)
|
||||
try:
|
||||
yield db
|
||||
|
|
|
|||
|
|
@ -5,34 +5,32 @@ import string
|
|||
import time
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Optional
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# 加载环境变量
|
||||
load_dotenv()
|
||||
|
||||
# 阿里云短信配置
|
||||
SMS_CONFIG = {
|
||||
"access_key_id": os.getenv("SMS_ACCESS_KEY_ID", "LTAI5t6HUnpFBLEK9194kPVG"),
|
||||
"access_key_secret": os.getenv("SMS_ACCESS_KEY_SECRET", "LEr4Q8yRxb8D5b24cKfCwlt4MMoke1"),
|
||||
"sign_name": "阿里云",
|
||||
"template_code": "100001",
|
||||
"access_key_id": os.getenv("SMS_ACCESS_KEY_ID", "LTAI5tQAx5niD7JQVqGE5acE"),
|
||||
"access_key_secret": os.getenv("SMS_ACCESS_KEY_SECRET", "QsQFAEKBkaNynIoKyvdIi3BUyWVZu1"),
|
||||
"sign_name": os.getenv("SMS_SIGN_NAME", "苏州算力"),
|
||||
"template_code": os.getenv("SMS_TEMPLATE_CODE", "SMS_501590956"),
|
||||
}
|
||||
|
||||
# 验证码缓存(生产环境建议用Redis)
|
||||
# 格式: { phone: { code: "123456", expire: 1234567890 } }
|
||||
# 验证码缓存
|
||||
VERIFICATION_CODES = {}
|
||||
|
||||
|
||||
def generate_code(length: int = 6) -> str:
|
||||
"""生成6位数字验证码"""
|
||||
return ''.join(random.choices(string.digits, k=length))
|
||||
|
||||
|
||||
def send_verification_code(phone: str) -> dict:
|
||||
"""发送短信验证码"""
|
||||
from alibabacloud_dysmsapi20170525 import models
|
||||
from alibabacloud_dysmsapi20170525.client import Client
|
||||
from alibabacloud_tea_openapi import models as open_models
|
||||
|
||||
try:
|
||||
# 生成验证码
|
||||
code = generate_code(6)
|
||||
code = generate_code()
|
||||
|
||||
# 配置客户端
|
||||
config = open_models.Config(
|
||||
|
|
@ -57,10 +55,9 @@ def send_verification_code(phone: str) -> dict:
|
|||
|
||||
# 检查结果
|
||||
if response.body.code == "OK":
|
||||
# 保存验证码
|
||||
VERIFICATION_CODES[phone] = {
|
||||
"code": code,
|
||||
"expire": int(time.time()) + 300 # 5分钟有效
|
||||
"expire": int(time.time()) + 300
|
||||
}
|
||||
return {
|
||||
"success": True,
|
||||
|
|
@ -72,35 +69,23 @@ def send_verification_code(phone: str) -> dict:
|
|||
"success": False,
|
||||
"message": f"发送失败: {response.body.message}"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
return {
|
||||
"success": False,
|
||||
"message": f"发送失败: {str(e)}"
|
||||
}
|
||||
|
||||
|
||||
def verify_code(phone: str, code: str) -> bool:
|
||||
"""验证验证码"""
|
||||
if phone not in VERIFICATION_CODES:
|
||||
return False
|
||||
|
||||
stored = VERIFICATION_CODES[phone]
|
||||
|
||||
# 检查是否过期
|
||||
if int(time.time()) > stored["expire"]:
|
||||
cached = VERIFICATION_CODES[phone]
|
||||
if int(time.time()) > cached["expire"]:
|
||||
del VERIFICATION_CODES[phone]
|
||||
return False
|
||||
|
||||
# 验证码匹配
|
||||
if stored["code"] == code:
|
||||
# 验证成功,删除验证码
|
||||
if cached["code"] == code:
|
||||
del VERIFICATION_CODES[phone]
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def check_code_exists(phone: str) -> bool:
|
||||
"""检查是否已发送过验证码"""
|
||||
return phone in VERIFICATION_CODES
|
||||
|
|
|
|||
Loading…
Reference in New Issue