feat(WIP): 项目架构

This commit is contained in:
sleepwithoutbz
2025-10-06 02:14:07 +08:00
parent 11cd17cbf4
commit 1e3cf5d212
6 changed files with 41 additions and 3 deletions

9
kds/database.py Normal file
View File

@@ -0,0 +1,9 @@
# Sqlalchemy Import
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
# Sqlalchemy 创建数据库连接
engine = create_engine("sqlite:///../kds.db", connect_args={"check_same_thread": False})
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()

View File

@@ -1,7 +1,10 @@
# Fastapi Import
from typing import Union
from fastapi import FastAPI
from .routers import callback, root
from kds.routers import callback, root
# Fastapi 创建各个路由
app = FastAPI()
app.include_router(callback.router)

View File

@@ -56,12 +56,14 @@ async def wechat_callback_post(request: Request):
# 3. 获取消息类型
msg_type = root.find("MsgType").text
from_user_name = root.find("FromUserName").text
# 4. 处理消息
if msg_type == "text":
# 4.1. 处理文本消息
content = root.find("Content").text
return JSONResponse(content={"Content": content})
return content
# return JSONResponse(content={"Content": content})
else:
# 4.2. 处理其他类型消息
return JSONResponse(content={"Content": ""})
return PlainTextResponse("Some content")

0
kds/services/__init__.py Normal file
View File

13
kds/services/msg.py Normal file
View File

@@ -0,0 +1,13 @@
from kds.database import Base
from sqlalchemy import Column, Integer, String, relationship, ForeignKey
class Msg(Base):
__tablename__ = "msg"
id = Column(Integer, primary_key=True, index=True)
msg_id = Column(Integer)
content = Column(String(300))
user_id = Column(Integer, ForeignKey("user.id"))
user = relationship("User", back_populates="posts")

11
kds/services/user.py Normal file
View File

@@ -0,0 +1,11 @@
# 从中央database模块导入Base确保所有模型使用同一个基类
from kds.database import Base
from sqlalchemy import Column, Integer, String
class User(Base):
__tablename__ = "user"
id = Column(Integer, primary_key=True, index=True)
open_id = Column(String(100))
msgs = relationship("Msg", back_populates="user")