diff --git a/README.md b/README.md index a421cce..b18a60f 100644 --- a/README.md +++ b/README.md @@ -17,3 +17,5 @@ npm run build ### Customize configuration See [Configuration Reference](https://cli.vuejs.org/config/). +# LOVEGET + diff --git a/fast/1.py b/fast/1.py new file mode 100644 index 0000000..e69de29 diff --git a/fast/__pycache__/database.cpython-312.pyc b/fast/__pycache__/database.cpython-312.pyc new file mode 100644 index 0000000..fee397d Binary files /dev/null and b/fast/__pycache__/database.cpython-312.pyc differ diff --git a/fast/__pycache__/main.cpython-312.pyc b/fast/__pycache__/main.cpython-312.pyc new file mode 100644 index 0000000..142bd23 Binary files /dev/null and b/fast/__pycache__/main.cpython-312.pyc differ diff --git a/fast/__pycache__/models.cpython-312.pyc b/fast/__pycache__/models.cpython-312.pyc new file mode 100644 index 0000000..3595e65 Binary files /dev/null and b/fast/__pycache__/models.cpython-312.pyc differ diff --git a/fast/database.py b/fast/database.py new file mode 100644 index 0000000..b78e33b --- /dev/null +++ b/fast/database.py @@ -0,0 +1,14 @@ +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker, declarative_base + +# 数据库连接字符串 +SQLALCHEMY_DATABASE_URL = "mysql+pymysql://root:mypassword12@127.0.0.1:3306/lianai" + +# 创建数据库引擎 +engine = create_engine(SQLALCHEMY_DATABASE_URL) + +# 创建会话本地类 +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) + +# 声明基类 +Base = declarative_base() \ No newline at end of file diff --git a/fast/main.py b/fast/main.py new file mode 100644 index 0000000..60d07ae --- /dev/null +++ b/fast/main.py @@ -0,0 +1,70 @@ +from fastapi import FastAPI, Depends, HTTPException, Request +from database import engine, SessionLocal +from models import User +from sqlalchemy.orm import Session +import uvicorn +from models import User + + +app = FastAPI() + +# 创建所有表 +User.metadata.create_all(bind=engine) + +# 依赖注入:获取数据库会话 +def get_db(): + db = SessionLocal() + try: + yield db + finally: + db.close() + + +@app.post("/login") +async def login(request: Request, db: Session = Depends(get_db)): + user_data =await request.json() + print(user_data) + user=db.query(User).filter(User.username==user_data['username']).first() + if user: + if user.password==user_data['password']: + return {"code":200,"msg":"登录成功"} + else: + return {"code":400,"msg":"密码错误"} + return {"code":501,"msg":"账号不存在"} + +@app.post("/register") +async def register(request: Request, db: Session = Depends(get_db)): + user_data =await request.json() + users=db.query(User).filter(User.username==user_data['username']).first() + if users: + return {"code":501,"msg":"账号已存在"} + else: + new_user=User(username=user_data['username'],password=user_data['password']) + db.add(new_user) + db.commit() + db.refresh(new_user) + print(new_user) + return {"code":200,"msg":"注册成功"} + + + + + + + + + + +from fastapi.middleware.cors import CORSMiddleware + +app.add_middleware( + CORSMiddleware, + allow_origins=["*"], + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + +if __name__ == '__main__': + # 运行fastapi程序 + uvicorn.run(app="main:app", host="127.0.0.1", port=8000, reload=True) diff --git a/fast/models.py b/fast/models.py new file mode 100644 index 0000000..b9cbbdc --- /dev/null +++ b/fast/models.py @@ -0,0 +1,11 @@ +from sqlalchemy import Column, Integer, String +from database import Base + + + +class User(Base): + __tablename__ = "users" + username = Column(String(30),index=True,primary_key=True) + password = Column(String(100), index=True,nullable=False) + +