Kaynağa Gözat

修改给出注册用户后默认昵称

root 1 ay önce
ebeveyn
işleme
b634dcc0c3
5 değiştirilmiş dosya ile 68 ekleme ve 7 silme
  1. 49
    1
      README.md
  2. 2
    2
      app/api/v1/auth.py
  3. 5
    3
      app/config.py
  4. 7
    0
      app/core/email.py
  5. 5
    1
      app/services/user_service.py

+ 49
- 1
README.md Dosyayı Görüntüle

@@ -1 +1,49 @@
1
-这是CaiYouHui的fastapi后端实现
1
+这是CaiYouHui的fastapi后端实现
2
+
3
+1. 架构设计(三层架构)
4
+
5
+┌─────────────────┐
6
+│   API 路由层     │ ← 处理 HTTP 请求/响应
7
+│   (api/v1/*.py) │
8
+└────────┬────────┘
9
+         │ 调用
10
+┌────────▼────────┐
11
+│   Service 层    │ ← 业务逻辑处理
12
+│  (services/*.py)│
13
+└────────┬────────┘
14
+         │ 使用
15
+┌────────▼────────┐
16
+│   Core 工具层   │ ← 通用工具函数
17
+│  (core/*.py)    │
18
+└─────────────────┘
19
+
20
+2. 完整的项目结构
21
+
22
+app/
23
+├── api/
24
+│   └── v1/
25
+│       ├── auth.py              # API路由层:只处理HTTP
26
+│       └── users.py
27
+├── services/                    # 服务层:业务逻辑
28
+│   ├── auth_service.py          # 认证相关业务逻辑
29
+│   ├── user_service.py          # 用户相关业务逻辑
30
+│   └── __init__.py
31
+├── core/                        # 核心层:通用工具
32
+│   ├── auth.py                  # 认证工具函数
33
+│   ├── security.py              # 安全工具函数
34
+│   └── __init__.py
35
+├── dependencies/                # 依赖注入
36
+│   └── auth.py                  # 认证依赖
37
+├── models/                      # 数据模型
38
+│   └── user.py
39
+└── schemas/                     # Pydantic模型
40
+    └── user.py
41
+
42
+
43
+3. 调用链总结
44
+
45
+HTTP请求 → API路由层 → 服务层 → 核心工具层
46
+   ↓           ↓          ↓          ↓
47
+auth.py   → auth_service.py → core/auth.py
48
+   ↓
49
+返回HTTP响应

+ 2
- 2
app/api/v1/auth.py Dosyayı Görüntüle

@@ -136,7 +136,7 @@ async def login(
136 136
             "email": user.email,
137 137
             "type": "access"
138 138
         },
139
-        expires_minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES
139
+        expires_minutes = settings.ACCESS_TOKEN_EXPIRE_MINUTES
140 140
     )
141 141
     
142 142
     # 创建刷新令牌
@@ -146,7 +146,7 @@ async def login(
146 146
             "user_id": user.id,
147 147
             "type": "refresh"
148 148
         },
149
-        expires_days=7
149
+        expires_days = 7
150 150
     )
151 151
     
152 152
     # 构建用户响应

+ 5
- 3
app/config.py Dosyayı Görüntüle

@@ -47,9 +47,11 @@ class Settings:
47 47
 
48 48
     # 邮箱验证(可选,后续添加)
49 49
     SMTP_ENABLED: bool = os.getenv("SMTP_ENABLED", "False").lower() == "true"
50
-    SMTP_HOST: str = os.getenv("SMTP_HOST", "")
50
+    SMTP_HOST: str = os.getenv("SMTP_HOST", "afanai.top")
51 51
     SMTP_PORT: int = int(os.getenv("SMTP_PORT", "587"))
52
-    SMTP_USER: str = os.getenv("SMTP_USER", "")
53
-    SMTP_PASSWORD: str = os.getenv("SMTP_PASSWORD", "")
52
+    SMTP_USER: str = os.getenv("SMTP_USER", "caoyuheng@afanai.top")
53
+    SMTP_PASSWORD: str = os.getenv("SMTP_PASSWORD", "111111")
54
+    EMAILS_FROM_EMAIL: str = os.getenv("EMAILS_FROM_EMAIL", "caoyuheng@afanai.top")
55
+    EMAILS_FROM_NAME: str = os.getenv("EMAILS_FROM_NAME", "CAI_YOU")
54 56
 
55 57
 settings = Settings()

+ 7
- 0
app/core/email.py Dosyayı Görüntüle

@@ -1,3 +1,4 @@
1
+import secrets
1 2
 from fastapi import BackgroundTasks
2 3
 from typing import Optional
3 4
 import smtplib
@@ -7,6 +8,7 @@ from jinja2 import Template
7 8
 import aiosmtplib
8 9
 from ..config import settings
9 10
 import os
11
+import string
10 12
 
11 13
 class EmailService:
12 14
     def __init__(self):
@@ -155,5 +157,10 @@ class EmailService:
155 157
         text_content = f"Welcome {username}! We're glad to have you on board."
156 158
         
157 159
         return await self.send_email_async(email_to, subject, html_content, text_content)
160
+    
161
+    def generate_random_string(self, length: int = 6) -> str:
162
+        """生成随机数字加字母字符串"""
163
+        name = string.digits + string.ascii_letters
164
+        return ''.join(secrets.choice(name) for _ in range(length))
158 165
 
159 166
 email_service = EmailService()

+ 5
- 1
app/services/user_service.py Dosyayı Görüntüle

@@ -6,6 +6,7 @@ from typing import List, Optional, Dict, Any
6 6
 from datetime import datetime, timezone, timedelta
7 7
 
8 8
 from app.models.user import User
9
+from app.core.email import email_service
9 10
 from app.core.security import password_hasher, password_validator
10 11
 from ..schemas.user import UserCreate, UserUpdate, UserResponse
11 12
 
@@ -40,13 +41,16 @@ class UserService:
40 41
         
41 42
         # 哈希密码
42 43
         hashed_password = password_hasher.hash_password(user_data.password)
44
+
45
+        # 生成随机昵称
46
+        nick_name = "CY_" + email_service.generate_random_string(12)
43 47
         
44 48
         # 创建用户对象
45 49
         user = User(
46 50
             username = user_data.username,
47 51
             email = user_data.email,
48 52
             hashed_password = hashed_password,
49
-            full_name = user_data.full_name,
53
+            full_name = nick_name,
50 54
             is_active = True,
51 55
             is_verified = False
52 56
         )