7-core-workflows.md 2.1 KB

7. Core Workflows

使用序列图说明关键系统工作流:

7.1 用户登录工作流

sequenceDiagram
    participant User as 用户
    participant Frontend as 前端应用
    participant AuthService as 认证服务
    participant UserService as 用户服务
    participant Redis as Redis缓存
    participant DB as MySQL数据库

    User->>Frontend: 输入邮箱密码
    Frontend->>AuthService: POST /auth/login
    AuthService->>UserService: 验证用户凭证
    UserService->>DB: 查询用户信息
    DB-->>UserService: 返回用户数据
    UserService-->>AuthService: 验证结果

    alt 验证成功
        AuthService->>AuthService: 生成JWT令牌
        AuthService->>Redis: 存储令牌信息
        AuthService-->>Frontend: 返回用户信息和令牌
        Frontend->>Frontend: 存储令牌到localStorage
        Frontend-->>User: 登录成功,跳转首页
    else 验证失败
        AuthService-->>Frontend: 返回错误信息
        Frontend-->>User: 显示错误提示
    end

7.2 用户注册工作流

sequenceDiagram
    participant User as 用户
    participant Frontend as 前端应用
    participant AuthService as 认证服务
    participant UserService as 用户服务
    participant DB as MySQL数据库

    User->>Frontend: 填写注册信息
    Frontend->>Frontend: Zod表单验证
    Frontend->>AuthService: POST /auth/register
    AuthService->>UserService: 创建新用户
    UserService->>DB: 检查邮箱是否已存在
    DB-->>UserService: 返回检查结果

    alt 邮箱可用
        UserService->>UserService: 加密密码
        UserService->>DB: 插入用户记录
        DB-->>UserService: 返回创建的用户
        UserService-->>AuthService: 用户创建成功
        AuthService->>AuthService: 生成JWT令牌
        AuthService-->>Frontend: 返回用户信息和令牌
        Frontend-->>User: 注册成功,自动登录
    else 邮箱已存在
        UserService-->>AuthService: 返回错误
        AuthService-->>Frontend: 返回错误信息
        Frontend-->>User: 显示邮箱已存在提示
    end