diff --git a/kds/database.py b/kds/database.py new file mode 100644 index 0000000..44f290e --- /dev/null +++ b/kds/database.py @@ -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() diff --git a/kds/main.py b/kds/main.py index e91acb3..377bed6 100644 --- a/kds/main.py +++ b/kds/main.py @@ -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) diff --git a/kds/routers/callback.py b/kds/routers/callback.py index 317663a..ea68547 100644 --- a/kds/routers/callback.py +++ b/kds/routers/callback.py @@ -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") diff --git a/kds/services/__init__.py b/kds/services/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/kds/services/msg.py b/kds/services/msg.py new file mode 100644 index 0000000..86ff2a4 --- /dev/null +++ b/kds/services/msg.py @@ -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") diff --git a/kds/services/user.py b/kds/services/user.py new file mode 100644 index 0000000..20b5634 --- /dev/null +++ b/kds/services/user.py @@ -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")