|
|
@@ -5,9 +5,9 @@ from datetime import datetime, timedelta
|
|
|
|
|
|
from sqlalchemy.future import select
|
|
|
|
|
|
-from app.config.agent_base_url import RG_USER_LOGIN, DF_USER_LOGIN
|
|
|
+from app.config.agent_base_url import RG_USER_LOGIN, DF_USER_LOGIN, RG_PING, DF_PING
|
|
|
from app.config.config import settings
|
|
|
-from app.config.const import chat_server, workflow_server
|
|
|
+from app.config.const import chat_server, workflow_server, http_200
|
|
|
from app.models import UserTokenModel
|
|
|
from app.models.app_token_model import AppToken
|
|
|
from app.models.base_model import SessionLocal
|
|
|
@@ -29,19 +29,21 @@ async def sync_token():
|
|
|
:return:
|
|
|
"""
|
|
|
app_data = [{"token_id": chat_server, "url": f"{settings.fwr_base_url}{RG_USER_LOGIN}",
|
|
|
- "data": {"email": "", "password": ""}, "is_crypt": True},
|
|
|
+ "data": {"email": "", "password": ""}, "is_crypt": True, "ping_url": f"{settings.fwr_base_url}{RG_PING}",
|
|
|
+ "token": "{}"},
|
|
|
{"token_id": workflow_server, "url": f"{settings.dify_base_url}{DF_USER_LOGIN}",
|
|
|
- "data": {"email": "", "password": "", "remember_me": True, "language": "zh-Hans"}, "is_crypt": False}]
|
|
|
+ "data": {"email": "", "password": "", "remember_me": True, "language": "zh-Hans"}, "is_crypt": False
|
|
|
+ , "ping_url": f"{settings.dify_base_url}{DF_PING}", "token": "Bearer {}"}]
|
|
|
|
|
|
- async def sync_token_chat(token_id, url, data, is_crypt):
|
|
|
+ async def sync_token_chat(token_id, url, data, is_crypt, ping_url, token):
|
|
|
db = SessionLocal()
|
|
|
# pdb = PostgresqlSessionLocal()
|
|
|
current_time = datetime.now() - timedelta(hours=12)
|
|
|
try:
|
|
|
user_token = db.query(UserTokenModel).filter(UserTokenModel.id == token_id).first()
|
|
|
+ chat = ChatBaseApply()
|
|
|
+ if user_token and (user_token.updated_at < current_time or not user_token.access_token or await chat.chat_ping(ping_url, {}, await chat.get_chat_headers(token.format(user_token.access_token))) != http_200):
|
|
|
|
|
|
- if user_token and (user_token.updated_at < current_time or not user_token.access_token):
|
|
|
- chat = ChatBaseApply()
|
|
|
data["email"] = user_token.account
|
|
|
if is_crypt:
|
|
|
data["password"] = await chat.password_encrypt(await password_decrypted(user_token.password))
|
|
|
@@ -61,7 +63,7 @@ async def sync_token():
|
|
|
|
|
|
tasks = []
|
|
|
for app in app_data:
|
|
|
- tasks.append(asyncio.create_task(sync_token_chat(app["token_id"], app["url"], app["data"], app["is_crypt"])))
|
|
|
+ tasks.append(asyncio.create_task(sync_token_chat(app["token_id"], app["url"], app["data"], app["is_crypt"], app["ping_url"], app["token"])))
|
|
|
done, pending = await asyncio.wait(tasks, return_when=asyncio.ALL_COMPLETED)
|
|
|
|
|
|
|