From fe8e77104db4503d65f13ad66eaa48d631619d7c Mon Sep 17 00:00:00 2001 From: zhaoqingang <zhaoqg0118@163.com> Date: 星期三, 15 一月 2025 17:28:16 +0800 Subject: [PATCH] tmp --- app/service/v2/initialize_data.py | 89 +++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 82 insertions(+), 7 deletions(-) diff --git a/app/service/v2/initialize_data.py b/app/service/v2/initialize_data.py index 1bb860c..32e7020 100644 --- a/app/service/v2/initialize_data.py +++ b/app/service/v2/initialize_data.py @@ -1,9 +1,13 @@ import json import time +import os + +import yaml + from Log import logger -from app.config.const import DIFY +from app.config.const import DIFY, ENV_CONF_PATH from app.models import MenuCapacityModel, WebMenuModel, GroupModel, RoleModel, DialogModel, UserModel, UserAppModel, \ - cipher_suite + cipher_suite, UserTokenModel from app.service.auth import UserAppDao from app.service.bisheng import BishengService from app.service.difyService import DifyService @@ -16,7 +20,7 @@ async def dialog_menu_sync(db): menu_list = [] - with open("env_conf/menu_conf.json", 'r', encoding='utf-8') as file: + with open(os.path.join(ENV_CONF_PATH, "menu_conf.json") , 'r', encoding='utf-8') as file: # 鍔犺浇JSON鏁版嵁 data = json.load(file) menu_list = data.get("data", []) @@ -36,6 +40,47 @@ menu_obj = WebMenuModel(**menu) db.add(menu_obj) db.commit() + + +async def create_menu_sync(db): + # json_file_path = "env_conf/menu_conf.json.template" + json_file_path = os.path.join(ENV_CONF_PATH, "menu_conf.json.template") + with open(json_file_path, 'r', encoding='utf-8') as file: + json_data = json.load(file).get("data", []) + # for menu in json_data: + # menu['dialog'].clear() + dialogs = db.query(DialogModel).all() + + dialog_dict = {} + for dialog in dialogs: + if dialog.name not in dialog_dict: + dialog_dict[dialog.name] = [] + dialog_dict[dialog.name].append({ + 'id': dialog.id, + 'chat_id': dialog.id, + 'chat_type': '', + 'agentType': dialog.dialog_type + }) + + for menu in json_data: + # if menu['title'] in dialog_dict: + # for dialog in dialog_dict[menu['title']]: + # new_dialog_item = { + # 'id': dialog.id, + # 'chat_id': dialog.id, + # 'chat_type': '', + # 'agentType': dialog.dialog_type + # } + menu['dialog']= dialog_dict.get(menu['title'], []) + json_data = {"data": json_data} + new_file_name = f"menu_conf.json.template" + new_file_path = os.path.join(os.path.dirname(json_file_path), new_file_name) + with open(new_file_path, 'w', encoding='utf-8') as new_file: + json.dump(json_data, new_file, ensure_ascii=False, indent=4) + return { + "file_name": new_file_name, + "json_data": json_data + } async def default_group_sync(db): @@ -66,7 +111,7 @@ async def app_register_sync(db): app_dict = {} - with open("env_conf/app_register_conf.json", 'r', encoding='utf-8') as file: + with open(os.path.join(ENV_CONF_PATH, "app_register_conf.json"), 'r', encoding='utf-8') as file: # 鍔犺浇JSON鏁版嵁 app_dict = json.load(file) try: @@ -78,7 +123,7 @@ async def basic_agent_sync(db): agent_list = [] - with open("env_conf/default_agent_conf.json", 'r', encoding='utf-8') as file: + with open(os.path.join(ENV_CONF_PATH, "default_agent_conf.json"), 'r', encoding='utf-8') as file: # 鍔犺浇JSON鏁版嵁 agent_dict = json.load(file) agent_list = agent_dict.get("basic", []) @@ -90,6 +135,7 @@ dialog.name = agent["name"] dialog.description = agent["description"] dialog.icon = agent["icon"] + dialog.parameters = json.dumps(agent["parameters"]) db.commit() except Exception as e: logger.error(e) @@ -97,7 +143,7 @@ try: dialog = DialogModel(id=agent["id"], name=agent["name"], description=agent["description"], icon=agent["icon"], tenant_id=user.id if user else "", dialog_type="3", - agent_id=agent["id"]) + parameters = json.dumps(agent["parameters"])) db.add(dialog) db.commit() db.refresh(dialog) @@ -177,4 +223,33 @@ user_id = await user_app_dao.insert_user_app_data(username, password, email, user_id, app_id, app_type) if not user_id: raise Exception("Failed to register with app") - print({"msg": "User registered successfully", "userFlag": user_id}) \ No newline at end of file + print({"msg": "User registered successfully", "userFlag": user_id}) + + +async def admin_account_sync(db): + try: + config = {} + now_account =[] + with open(os.path.join(ENV_CONF_PATH, "account.yaml"), 'r', encoding='utf-8') as file: + # 鍔犺浇JSON鏁版嵁 + config = yaml.safe_load(file) + account_list = db.query(UserTokenModel).all() + for account in account_list: + if account.id in config: + + if account.account != config[account.id]["account"] or account.password != config[account.id]["password"]: + db.query(UserTokenModel).filter_by(id=account.id).update({"account": config[account.id]["account"], + "password": config[account.id]["password"], + "access_token": "" + }) + now_account.append(account.id) + else: + db.query(UserTokenModel).filter_by(id=account.id).delete() + for k, v in config.items(): + if k not in now_account: + new_account = UserTokenModel(id=k, account=v["account"], password=v["password"]) + db.add(new_account) + db.commit() + except Exception as e: + print(e) + db.rollback() \ No newline at end of file -- Gitblit v1.8.0