feat: 直接数据库新建用户

This commit is contained in:
hackrobot
2024-03-20 19:43:50 +08:00
parent 16e7de9453
commit 8968cc7c87
12 changed files with 23 additions and 10 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -21,7 +21,8 @@ def get_users(db: Session, skip: int = 0, limit: int = 100):
# 创建用户
# user:schemas.UserCreate
def create_user(db: Session, user:schemas.UserCreate):
# **user
def create_user(db: Session, user:schemas.User):
db_user = models.User(
wxid=user.wxid,
openId=user.openId,

14
main.py
View File

@@ -47,7 +47,7 @@ async def root():
# 创建用户
@app.post("/users/")
# schemas.UserCreate决定了swagger文档
def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
def create_user(user: schemas.User, db: Session = Depends(get_db)):
db_user = crud.create_user(db, user)
if db_user:
raise HTTPException(status_code=400,
@@ -71,6 +71,7 @@ def read_user(openId, db: Session = Depends(get_db)):
return db_user
# 监听机器人http-get消息
@app.get("/wxbot")
def helloget(q:dict):
data = q
@@ -78,11 +79,10 @@ def helloget(q:dict):
t.start()
return json.dumps({"Code": 0})
# 监听机器人post日志消息
@app.post("/wxbot")
# def hellopost(requestData:schemas.User):
def hellopost(requestData:dict):
# data = request.get_json()
print('requestData---',requestData)
data = requestData
t = threading.Thread(target=kp, args=(data, )) # 通过当前线程开启新的线程去执行
@@ -209,6 +209,13 @@ def kp(event_data):
if msg_type == 1: # 1 文本消息/自带表情
kp_log = f'{type_str} 文本消息 内容:{msg}'
# 将好友信息=>保存到数据库
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']))
elif msg_type == 3: # 3 图片消息
kp_log = f'{type_str} 图片消息 内容:{filepath}'
# 本地dat图片解密
@@ -231,6 +238,7 @@ def kp(event_data):
newFriendWxid=xml_msg.get('alias')
# 将好友信息=>保存到数据库
newFriendDict=WXBotService.get_friend_info(account_wxid, newFriendWxid)
crud.create_user(db=get_db(),user=newFriendDict)
elif msg_type == 42: # 42 名片消息
xml_msg = ET.fromstring(msg)

View File

@@ -11,6 +11,7 @@ class User(Base):
wxid = Column(String)
openId = Column(String)
sex = Column(String) #性别
city = Column(String) #城市
country = Column(String) #"CN"
province = Column(String) #"Hubei"
sourceStr = Column(String) #:"通过朋友验证消息添加"
@@ -19,4 +20,13 @@ 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)

View File

@@ -1,11 +1,7 @@
from pydantic import BaseModel
# 用户信息
class UserCreate(BaseModel):
password: str
wxid: str
openId: str
sex: str
@@ -22,7 +18,5 @@ class UserCreate(BaseModel):
class User(UserCreate):
id: int
is_active: bool
class Config:
orm_mode = True