feat: 获取保存用户数据
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
5
crud.py
5
crud.py
@@ -15,6 +15,11 @@ def get_user_by_openId(db: Session, openId: str):
|
|||||||
return db.query(models.User).filter(models.User.openId == openId).first()
|
return db.query(models.User).filter(models.User.openId == openId).first()
|
||||||
|
|
||||||
|
|
||||||
|
# 通过wxid岔村用户
|
||||||
|
def get_user_by_wxid(db: Session, wxid: str):
|
||||||
|
return db.query(models.User).filter(models.User.wxid == wxid).first()
|
||||||
|
|
||||||
|
|
||||||
# 分页查询-用户all
|
# 分页查询-用户all
|
||||||
def get_users(db: Session, skip: int = 0, limit: int = 100):
|
def get_users(db: Session, skip: int = 0, limit: int = 100):
|
||||||
return db.query(models.User).offset(skip).limit(limit).all()
|
return db.query(models.User).offset(skip).limit(limit).all()
|
||||||
|
|||||||
BIN
kunpeng.db
BIN
kunpeng.db
Binary file not shown.
46
main.py
46
main.py
@@ -51,10 +51,13 @@ def create_user(user: schemas.User, db: Session = Depends(get_db)):
|
|||||||
print('user',user)
|
print('user',user)
|
||||||
print('typeuser',type(user))
|
print('typeuser',type(user))
|
||||||
|
|
||||||
db_user = crud.create_user(db, user)
|
# 检测是否存在wxid
|
||||||
|
db_user = crud.get_user_by_wxid(db, user.wxid)
|
||||||
|
print('db_user',db_user)
|
||||||
|
|
||||||
if db_user:
|
if db_user:
|
||||||
raise HTTPException(status_code=400,
|
raise HTTPException(status_code=400,
|
||||||
detail="openId already registered")
|
detail="wxid already registered")
|
||||||
return crud.create_user(db=db, user=user)
|
return crud.create_user(db=db, user=user)
|
||||||
|
|
||||||
|
|
||||||
@@ -66,13 +69,20 @@ def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
|||||||
|
|
||||||
|
|
||||||
# 根据openId查询用户
|
# 根据openId查询用户
|
||||||
@app.get("/users/{openId}")
|
# @app.get("/users")
|
||||||
def read_user(openId, db: Session = Depends(get_db)):
|
# def read_user(openId, db: Session = Depends(get_db)):
|
||||||
db_user = crud.get_user_by_openId(db, openId=openId)
|
# db_user = crud.get_user_by_openId(db, openId=openId)
|
||||||
if db_user is None:
|
# if db_user is None:
|
||||||
raise HTTPException(status_code=404, detail="User not found")
|
# raise HTTPException(status_code=404, detail="openId not found")
|
||||||
return db_user
|
# return db_user
|
||||||
|
|
||||||
|
# 根据wxid查询用户
|
||||||
|
@app.get("/users")
|
||||||
|
def read_user(wxid, db: Session = Depends(get_db)):
|
||||||
|
db_user = crud.get_user_by_wxid(db, wxid=wxid)
|
||||||
|
if db_user is None:
|
||||||
|
raise HTTPException(status_code=404, detail="User-wxid not found")
|
||||||
|
return db_user
|
||||||
|
|
||||||
# 监听机器人http-get消息
|
# 监听机器人http-get消息
|
||||||
@app.get("/wxbot")
|
@app.get("/wxbot")
|
||||||
@@ -92,6 +102,16 @@ def hellopost(requestData:dict):
|
|||||||
t.start()
|
t.start()
|
||||||
return json.dumps({"Code": 0})
|
return json.dumps({"Code": 0})
|
||||||
|
|
||||||
|
# 保存好友信息
|
||||||
|
def saveWxidInfo(account_wxid,wxid):
|
||||||
|
allFriend= WXBotService.get_address_list(account_wxid)
|
||||||
|
for friendItem in allFriend['data']["contactList"]:
|
||||||
|
if friendItem['wxid']==wxid:
|
||||||
|
friendItemObj=models.User(**friendItem)
|
||||||
|
# 当前的路由函数
|
||||||
|
create_user(db=SessionLocal(),user=friendItemObj)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# 发送小程序模版,建议远程更新
|
# 发送小程序模版,建议远程更新
|
||||||
def sendminiProgram(data, account_wxid, wxid, msgXml):
|
def sendminiProgram(data, account_wxid, wxid, msgXml):
|
||||||
@@ -214,10 +234,14 @@ def kp(event_data):
|
|||||||
kp_log = f'{type_str} 文本消息 内容:{msg}'
|
kp_log = f'{type_str} 文本消息 内容:{msg}'
|
||||||
# 将好友信息=>保存到数据库
|
# 将好友信息=>保存到数据库
|
||||||
newFriendDict=WXBotService.get_friend_info(account_wxid, wxid)
|
newFriendDict=WXBotService.get_friend_info(account_wxid, wxid)
|
||||||
print('newFriendDict[data]---',newFriendDict['data'])
|
# print('newFriendDict[data]---',newFriendDict['data'])
|
||||||
userClass=models.User(**(newFriendDict['data']))
|
userClass=models.User(**(newFriendDict['data']))
|
||||||
print('userClass====',type(userClass))
|
# print('userClass====',type(userClass))
|
||||||
crud.create_user(db=SessionLocal(),user=userClass)
|
# crud.create_user(db=SessionLocal(),user=userClass)
|
||||||
|
|
||||||
|
|
||||||
|
# 保存好友信息
|
||||||
|
saveWxidInfo(account_wxid,wxid)
|
||||||
|
|
||||||
|
|
||||||
elif msg_type == 3: # 3 图片消息
|
elif msg_type == 3: # 3 图片消息
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ class User(Base):
|
|||||||
id = Column(Integer, primary_key=True)
|
id = Column(Integer, primary_key=True)
|
||||||
wxid = Column(String)
|
wxid = Column(String)
|
||||||
openId = Column(String)
|
openId = Column(String)
|
||||||
sex = Column(String) #性别
|
sex = Column(String) #性别 男/女
|
||||||
city = Column(String) #城市
|
city = Column(String) #城市
|
||||||
country = Column(String) #"CN"
|
country = Column(String) #"CN"
|
||||||
province = Column(String) #"Hubei"
|
province = Column(String) #"Hubei"
|
||||||
@@ -27,6 +27,8 @@ class User(Base):
|
|||||||
v1 = Column(String)
|
v1 = Column(String)
|
||||||
|
|
||||||
|
|
||||||
|
# {'bighead': 'https://wx.qlogo.cn/mmhead/ver_1/sAOrfgticJnOSE39qcB2mtImUMyANI4NoumWzKbdf87yMYp89Jhszic3mYVgFomW45CSeBxTQK2eyiciakUr9njdvAMtZKHL8w9uIcF8feicZU9ama4xMaU1TlnT4VYam5WHN/0', 'city': 'Jingzhou', 'country': 'CN', 'labels': '', 'nickname': 'Eric', 'province': 'Hubei', 'remark': '', 'sex': '男', 'smallhead': 'https://wx.qlogo.cn/mmhead/ver_1/sAOrfgticJnOSE39qcB2mtImUMyA NI4NoumWzKbdf87yMYp89Jhszic3mYVgFomW45CSeBxTQK2eyiciakUr9njdvAMtZKHL8w9uIcF8feicZU9ama4xMaU1TlnT4VYam5WHN/132', 'source': 6, 'sourceStr': '通过朋友验证消息添加', 'usertype': 1, 'wxaccount': 'small
|
||||||
|
# zhiyun', 'wxid': 'wxid_4413224132412'}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -18,11 +18,8 @@ class UserCreate(BaseModel):
|
|||||||
source: str
|
source: str
|
||||||
v1: str
|
v1: str
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class User(UserCreate):
|
class User(UserCreate):
|
||||||
id: int
|
id: int
|
||||||
class Config:
|
class Config:
|
||||||
orm_mode = True
|
orm_mode = True
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user