This commit is contained in:
liubiren 2026-07-22 01:33:29 +08:00
parent a53535b785
commit e39a445608
4 changed files with 52 additions and 37 deletions

View File

@ -598,8 +598,8 @@ def render_create_conversation_button() -> rx.Component:
),
background_color=rx.color("mauve", 1),
),
open=ConversationState.is_creating_conversation,
on_open_change=ConversationState.toggle_creating_conversation,
open=ConversationState.is_conversation_creating,
on_open_change=ConversationState.toggle_conversation_creating,
),
display="flex",
width="100%",

View File

@ -2,15 +2,17 @@
"""
认证状态
"""
from asyncio import sleep, create_task
from asyncio import sleep
from email.header import Header
from email.mime.text import MIMEText
import re
from typing import Any, AsyncGenerator, Coroutine, cast
from typing import AsyncGenerator
from aiosmtplib import SMTP, SMTPException
import reflex as rx
from reflex.event import EventCallback
from application.states.conversation import ConversationState
from application.states.database import DatabaseState
# 验证码HTML模板
@ -203,12 +205,12 @@ class AuthState(rx.State):
if not self.validate_email():
return
# 验证码非空校验
# 校验验证码非空
if not self.captcha:
self.login_error_message = "请输入验证码"
return
# 验证码格式校验
# 校验验证码格式
if not re.fullmatch(r"^\d{6}$", self.captcha):
self.login_error_message = "验证码格式不正确,请重新输入"
return
@ -220,7 +222,7 @@ class AuthState(rx.State):
self.is_logging_in = True
# 更新验证码记录,若更新成功则创建用户记录
# 更新验证码记录
database_state = await self.get_state(DatabaseState)
if not await database_state.update_captcha(
email=self.email, captcha=self.captcha
@ -228,7 +230,14 @@ class AuthState(rx.State):
self.is_logging_in = False
self.login_error_message = "验证码错误"
return
self.user_id = await database_state.create_users_record(email=self.email)
# 创建用户记录
user_id = await database_state.create_users_record(email=self.email)
# 初始化
conversation_state = await self.get_state(ConversationState)
await conversation_state.init(user_id=user_id)
self.user_id = user_id
self.email = ""
self.captcha = ""
@ -248,12 +257,20 @@ class AuthState(rx.State):
self.is_settings_hover_card_open = is_settings_hover_card_open
@rx.event
def logout(self) -> None:
async def logout(self) -> None:
"""
退出登录
:return: None
"""
self.is_settings_hover_card_open = False
conversation_state = await self.get_state(ConversationState)
conversation_state.user_id = ""
conversation_state.conversations = {}
conversation_state.conversation_id = ""
conversation_state.is_conversation_history_shown = False
conversation_state.is_conversation_creating = False
self.user_id = ""
@rx.event

View File

@ -77,38 +77,27 @@ class ConversationState(rx.State):
# 会话历史展示状态True表示展示False表示隐藏
is_conversation_history_shown: bool = False
# 正在新建会话状态True表示正在新建False表示未正在新
is_creating_conversation: bool = False
# 会话创建状态True表示正在创建False表示未正在创
is_conversation_creating: bool = False
@rx.event
def load_conversation_id(self, conversation_id: str) -> None:
async def init(self, user_id: str) -> None:
"""
设置当前会话唯一标识
:param conversation_id: 当前会话唯一标识
初始化
:param user_id: 用户唯一标识
:return: None
"""
self.conversation_id = conversation_id
@rx.event
async def on_mount(self):
"""
页面挂载时初始化会话列表和会话唯一标识
"""
# 获取当前用户唯一标识
auth_state = await self.get_state(AuthState)
self.user_id = auth_state.user_id
self.user_id = user_id
if not self.user_id:
return
# 获取数据库状态
# 数据库状态
database_state = await self.get_state(DatabaseState)
# 检索会话字典并赋值
# 获取会话字典
self.conversations = await database_state.retrieve_conversations(
user_id=self.user_id
)
# 若会话字典为空则先创建会话记录再在会话字典中添加默认会话
# 若会话字典为空则先创建会话记录再在会话字典中添加
if not self.conversations:
conversation_id, created_at = (
await database_state.create_conversations_record(user_id=self.user_id)
@ -118,6 +107,16 @@ class ConversationState(rx.State):
# 将最后一个会话的唯一标识设置为当前会话唯一标识
self.conversation_id = next(reversed(self.conversations.keys()))
@rx.event
async def on_mount(self):
"""
挂载时
"""
# 获取当前用户唯一标识
auth_state = await self.get_state(AuthState)
# 初始化
await self.init(user_id=auth_state.user_id)
@rx.event
def toggle_conversation_history_shown(self) -> None:
"""
@ -144,7 +143,7 @@ class ConversationState(rx.State):
# 获取会话描述
description = form_data["description"].strip() or "新会话"
# 先创建会话记录再在会话字典中添加会话
# 先创建会话记录再在会话字典中添加
database_state = await self.get_state(DatabaseState)
conversation_id, created_at = await database_state.create_conversations_record(
user_id=self.user_id, description=description
@ -152,11 +151,8 @@ class ConversationState(rx.State):
self.conversations[conversation_id] = Conversation(
description=description, created_at=created_at
)
# 将所添加的会话唯一标识设置为当前会话唯一标识
self.conversation_id = conversation_id
# 将正在新建会话状态设置为已完成
self.is_creating_conversation = False
self.is_conversation_creating = False
@rx.event
async def delete_conversation(self, conversation_id: str) -> None:
@ -170,7 +166,9 @@ class ConversationState(rx.State):
# 先逻辑删除会话记录再在会话字典中删除会话
database_state = await self.get_state(DatabaseState)
await database_state.delete_conversations_record(conversation_id=conversation_id)
await database_state.delete_conversations_record(
conversation_id=conversation_id
)
del self.conversations[conversation_id]
# 删除后,若会话字典为空则先创建会话记录再在会话字典中添加默认会话
@ -196,11 +194,11 @@ class ConversationState(rx.State):
self.conversation_id = conversation_id
@rx.event
def toggle_creating_conversation(self) -> None:
def toggle_conversation_creating(self) -> None:
"""
切换正在新建会话状态
"""
self.is_creating_conversation = not self.is_creating_conversation
self.is_conversation_creating = not self.is_conversation_creating
@rx.var
def dialog_history(self) -> Dict[str, Dialog]:

Binary file not shown.