style: 格式化代码
This commit is contained in:
63
main.py
63
main.py
@@ -20,16 +20,14 @@ except ImportError:
|
|||||||
import WXBotService
|
import WXBotService
|
||||||
from planet import planetXml
|
from planet import planetXml
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
import xml.dom.minidom
|
import xml.dom.minidom
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
models.Base.metadata.create_all(bind=engine)
|
models.Base.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Dependency
|
# Dependency
|
||||||
def get_db():
|
def get_db():
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
@@ -49,16 +47,15 @@ async def root():
|
|||||||
@app.post("/users/")
|
@app.post("/users/")
|
||||||
# schemas.UserCreate决定了swagger文档
|
# schemas.UserCreate决定了swagger文档
|
||||||
def create_user(user: schemas.User, db: Session = Depends(get_db)):
|
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))
|
||||||
|
|
||||||
# 检测是否存在wxid
|
# 检测是否存在wxid
|
||||||
db_user = crud.get_user_by_wxid(db, user.wxid)
|
db_user = crud.get_user_by_wxid(db, user.wxid)
|
||||||
print('db_user',db_user)
|
print('db_user', db_user)
|
||||||
|
|
||||||
if db_user:
|
if db_user:
|
||||||
raise HTTPException(status_code=400,
|
raise HTTPException(status_code=400, detail="wxid already registered")
|
||||||
detail="wxid already registered")
|
|
||||||
return crud.create_user(db=db, user=user)
|
return crud.create_user(db=db, user=user)
|
||||||
|
|
||||||
|
|
||||||
@@ -71,12 +68,13 @@ def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
|||||||
|
|
||||||
# 根据openId查询用户
|
# 根据openId查询用户
|
||||||
@app.post("/openId")
|
@app.post("/openId")
|
||||||
def read_user(user:schemas.User, db: Session = Depends(get_db)):
|
def read_user(user: schemas.User, db: Session = Depends(get_db)):
|
||||||
db_user = crud.get_user_by_openId(db, openId=user.openId)
|
db_user = crud.get_user_by_openId(db, openId=user.openId)
|
||||||
if db_user is None:
|
if db_user is None:
|
||||||
raise HTTPException(status_code=404, detail="openId not found")
|
raise HTTPException(status_code=404, detail="openId not found")
|
||||||
return db_user
|
return db_user
|
||||||
|
|
||||||
|
|
||||||
# 根据wxid查询用户
|
# 根据wxid查询用户
|
||||||
@app.get("/users")
|
@app.get("/users")
|
||||||
def read_user(wxid, db: Session = Depends(get_db)):
|
def read_user(wxid, db: Session = Depends(get_db)):
|
||||||
@@ -85,41 +83,43 @@ def read_user(wxid, db: Session = Depends(get_db)):
|
|||||||
raise HTTPException(status_code=404, detail="User-wxid not found")
|
raise HTTPException(status_code=404, detail="User-wxid not found")
|
||||||
return db_user
|
return db_user
|
||||||
|
|
||||||
|
|
||||||
# 监听机器人http-get消息
|
# 监听机器人http-get消息
|
||||||
@app.get("/wxbot")
|
@app.get("/wxbot")
|
||||||
def helloget(q:dict):
|
def helloget(q: dict):
|
||||||
data = q
|
data = q
|
||||||
t = threading.Thread(target=kp, args=(data, )) # 通过当前线程开启新的线程去执行
|
t = threading.Thread(target=kp, args=(data, )) # 通过当前线程开启新的线程去执行
|
||||||
t.start()
|
t.start()
|
||||||
return json.dumps({"Code": 0})
|
return json.dumps({"Code": 0})
|
||||||
|
|
||||||
|
|
||||||
# 监听机器人post日志消息
|
# 监听机器人post日志消息
|
||||||
@app.post("/wxbot")
|
@app.post("/wxbot")
|
||||||
# def hellopost(requestData:schemas.User):
|
# def hellopost(requestData:schemas.User):
|
||||||
def hellopost(requestData:dict):
|
def hellopost(requestData: dict):
|
||||||
print('requestData---',requestData)
|
print('requestData---', requestData)
|
||||||
data = requestData
|
data = requestData
|
||||||
t = threading.Thread(target=kp, args=(data, )) # 通过当前线程开启新的线程去执行
|
t = threading.Thread(target=kp, args=(data, )) # 通过当前线程开启新的线程去执行
|
||||||
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 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):
|
||||||
# 发送小程序
|
# 发送小程序
|
||||||
if data.get('isSender') != 1:
|
if data.get('isSender') != 1:
|
||||||
root = ET.fromstring(msgXml) #xml-字符串形式
|
root = ET.fromstring(msgXml) #xml-字符串形式
|
||||||
|
|
||||||
#打开xml文档
|
#打开xml文档
|
||||||
# current_dir = os.path.dirname(os.path.abspath(__file__))
|
# current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
# print('current_dir+/planet.xml',current_dir+'\planet.xml')
|
# print('current_dir+/planet.xml',current_dir+'\planet.xml')
|
||||||
@@ -234,17 +234,15 @@ def kp(event_data):
|
|||||||
if msg_type == 1: # 1 文本消息/自带表情
|
if msg_type == 1: # 1 文本消息/自带表情
|
||||||
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)
|
saveWxidInfo(account_wxid, wxid)
|
||||||
|
|
||||||
|
|
||||||
elif msg_type == 3: # 3 图片消息
|
elif msg_type == 3: # 3 图片消息
|
||||||
kp_log = f'{type_str} 图片消息 内容:{filepath}'
|
kp_log = f'{type_str} 图片消息 内容:{filepath}'
|
||||||
# 本地dat图片解密
|
# 本地dat图片解密
|
||||||
@@ -264,12 +262,12 @@ def kp(event_data):
|
|||||||
xml_msg.get('encryptusername'),
|
xml_msg.get('encryptusername'),
|
||||||
xml_msg.get('ticket'),
|
xml_msg.get('ticket'),
|
||||||
xml_msg.get('scene'))
|
xml_msg.get('scene'))
|
||||||
newFriendWxid=xml_msg.get('alias')
|
newFriendWxid = xml_msg.get('alias')
|
||||||
# 将好友信息=>保存到数据库
|
# 将好友信息=>保存到数据库
|
||||||
# newFriendDict=WXBotService.get_friend_info(account_wxid, newFriendWxid)
|
# newFriendDict=WXBotService.get_friend_info(account_wxid, newFriendWxid)
|
||||||
# userClass=models.User(**(newFriendDict['data']))
|
# userClass=models.User(**(newFriendDict['data']))
|
||||||
# crud.create_user(db= SessionLocal(),user=userClass)
|
# crud.create_user(db= SessionLocal(),user=userClass)
|
||||||
|
|
||||||
elif msg_type == 42: # 42 名片消息
|
elif msg_type == 42: # 42 名片消息
|
||||||
xml_msg = ET.fromstring(msg)
|
xml_msg = ET.fromstring(msg)
|
||||||
kp_log = f'{type_str} 企业微信名片消息 {xml_msg.get("nickname")}({xml_msg.get("username")})'
|
kp_log = f'{type_str} 企业微信名片消息 {xml_msg.get("nickname")}({xml_msg.get("username")})'
|
||||||
@@ -356,4 +354,3 @@ def kp(event_data):
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
print(kp_log)
|
print(kp_log)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user