feat(WIP): 项目架构
This commit is contained in:
9
kds/database.py
Normal file
9
kds/database.py
Normal 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()
|
||||||
@@ -1,7 +1,10 @@
|
|||||||
|
# Fastapi Import
|
||||||
from typing import Union
|
from typing import Union
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from .routers import callback, root
|
from kds.routers import callback, root
|
||||||
|
|
||||||
|
|
||||||
|
# Fastapi 创建各个路由
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
app.include_router(callback.router)
|
app.include_router(callback.router)
|
||||||
|
|||||||
@@ -56,12 +56,14 @@ async def wechat_callback_post(request: Request):
|
|||||||
|
|
||||||
# 3. 获取消息类型
|
# 3. 获取消息类型
|
||||||
msg_type = root.find("MsgType").text
|
msg_type = root.find("MsgType").text
|
||||||
|
from_user_name = root.find("FromUserName").text
|
||||||
|
|
||||||
# 4. 处理消息
|
# 4. 处理消息
|
||||||
if msg_type == "text":
|
if msg_type == "text":
|
||||||
# 4.1. 处理文本消息
|
# 4.1. 处理文本消息
|
||||||
content = root.find("Content").text
|
content = root.find("Content").text
|
||||||
return JSONResponse(content={"Content": content})
|
return content
|
||||||
|
# return JSONResponse(content={"Content": content})
|
||||||
else:
|
else:
|
||||||
# 4.2. 处理其他类型消息
|
# 4.2. 处理其他类型消息
|
||||||
return JSONResponse(content={"Content": ""})
|
return PlainTextResponse("Some content")
|
||||||
|
|||||||
0
kds/services/__init__.py
Normal file
0
kds/services/__init__.py
Normal file
13
kds/services/msg.py
Normal file
13
kds/services/msg.py
Normal 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
11
kds/services/user.py
Normal 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")
|
||||||
Reference in New Issue
Block a user