fix: 修复db本地连接

This commit is contained in:
hackrobot
2024-03-20 21:31:43 +08:00
parent 8968cc7c87
commit e4ddd6d277
9 changed files with 28 additions and 10 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -2,7 +2,8 @@ from sqlalchemy.orm import Session
# from . import models, schemas
import models, schemas
import json
import time
# 通过id获取用户
def get_user(db: Session, user_id: int):
@@ -23,6 +24,7 @@ def get_users(db: Session, skip: int = 0, limit: int = 100):
# user:schemas.UserCreate
# **user
def create_user(db: Session, user:schemas.User):
# def create_user(db: Session, user):
db_user = models.User(
wxid=user.wxid,
openId=user.openId,
@@ -35,7 +37,12 @@ def create_user(db: Session, user:schemas.User):
wxaccount=user.wxaccount,
remark=user.remark,
smallhead=user.smallhead,
bighead=user.bighead,
labels=user.labels,
source=user.source,
v1=user.v1
)
db.add(db_user)
db.commit()
db.refresh(db_user)

Binary file not shown.

13
main.py
View File

@@ -48,6 +48,9 @@ async def root():
@app.post("/users/")
# schemas.UserCreate决定了swagger文档
def create_user(user: schemas.User, db: Session = Depends(get_db)):
print('user',user)
print('typeuser',type(user))
db_user = crud.create_user(db, user)
if db_user:
raise HTTPException(status_code=400,
@@ -212,8 +215,9 @@ def kp(event_data):
# 将好友信息=>保存到数据库
newFriendDict=WXBotService.get_friend_info(account_wxid, wxid)
print('newFriendDict[data]---',newFriendDict['data'])
# print(type(newFriendDict['data']))
crud.create_user(db=get_db(),user=json.dumps(newFriendDict['data']))
userClass=models.User(**(newFriendDict['data']))
print('userClass====',type(userClass))
crud.create_user(db=SessionLocal(),user=userClass)
elif msg_type == 3: # 3 图片消息
@@ -237,8 +241,9 @@ def kp(event_data):
xml_msg.get('scene'))
newFriendWxid=xml_msg.get('alias')
# 将好友信息=>保存到数据库
newFriendDict=WXBotService.get_friend_info(account_wxid, newFriendWxid)
crud.create_user(db=get_db(),user=newFriendDict)
# newFriendDict=WXBotService.get_friend_info(account_wxid, newFriendWxid)
# userClass=models.User(**(newFriendDict['data']))
# crud.create_user(db= SessionLocal(),user=userClass)
elif msg_type == 42: # 42 名片消息
xml_msg = ET.fromstring(msg)

View File

@@ -20,11 +20,11 @@ class User(Base):
wxaccount = Column(String) #微信号
remark = Column(String) #备注
smallhead = Column(String) #头像
# bighead = Column(String) #头像-打
# labels = Column(String) #
# source = Column(String) #来源
# generator = Column(String)
# v1 = Column(String)
bighead = Column(String) #头像-打
labels = Column(String) #
source = Column(String) #来源
generator = Column(String)
v1 = Column(String)

View File

@@ -13,6 +13,12 @@ class UserCreate(BaseModel):
wxaccount: str
remark: str
smallhead: str
bighead: str
labels: str
source: str
v1: str