fastapi例子open接口返回二维码结果

This commit is contained in:
evilbeast 2022-09-28 15:48:32 +08:00
parent ae450372b1
commit f7fb727257
2 changed files with 21 additions and 4 deletions

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import uvicorn
import threading
from functools import wraps
from fastapi import FastAPI
from mgr import ClientManager
@ -56,8 +57,14 @@ async def client_create():
response_model=models.ResponseModel)
@catch_exception()
async def client_open(model: models.ClientOpenReqModel):
ret = client_mgr.get_client(model.guid).open(model.smart, model.show_login_qrcode)
return response_json(1 if ret else 0)
client = client_mgr.get_client(model.guid)
ret = client.open(model.smart, model.show_login_qrcode)
# 当show_login_qrcode=True时, 打开微信时会显示二维码界面
if model.show_login_qrcode:
client.qrcode_event = threading.Event()
client.qrcode_event.wait(timeout=10)
return response_json(1 if ret else 0, {'qrcode': client.qrcode})
@app.post("/global/set_callback_url", summary="设置接收通知地址", tags=["Global"],

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import ntchat
import threading
import requests
from typing import Dict, Union
from ntchat.utils.singleton import Singleton
@ -9,6 +10,8 @@ from exception import ClientNotExists
class ClientWeChat(ntchat.WeChat):
guid: str = ""
qrcode_event: threading.Event = None
qrcode: str = ""
class ClientManager(metaclass=Singleton):
@ -35,7 +38,7 @@ class ClientManager(metaclass=Singleton):
wechat.on(ntchat.MT_RECV_WECHAT_QUIT_MSG, self.__on_quit_callback)
return guid
def get_client(self, guid: str) -> Union[None, ntchat.WeChat]:
def get_client(self, guid: str) -> ClientWeChat:
client = self.__client_map.get(guid, None)
if client is None:
raise ClientNotExists(guid)
@ -45,7 +48,14 @@ class ClientManager(metaclass=Singleton):
if guid in self.__client_map:
del self.__client_map[guid]
def __on_callback(self, wechat, message):
def __on_callback(self, wechat: ClientWeChat, message: dict):
# 通知二维码显示
msg_type = message['type']
if msg_type == ntchat.MT_RECV_LOGIN_QRCODE_MSG and wechat.qrcode_event:
wechat.qrcode = message["data"]["code"]
wechat.qrcode_event.set()
if not self.callback_url:
return