xuyonghao
2024-12-18 cd8744be716365d72f6ebef8365b475d1802c0ea
app/service/v2/initialize_data.py
@@ -1,5 +1,6 @@
import json
import time
import os
from Log import logger
from app.config.const import DIFY
from app.models import MenuCapacityModel, WebMenuModel, GroupModel, RoleModel, DialogModel, UserModel, UserAppModel, \
@@ -36,6 +37,41 @@
        menu_obj = WebMenuModel(**menu)
        db.add(menu_obj)
    db.commit()
async def create_menu_sync(db):
    json_file_path = "env_conf/menu_conf.json"
    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(dialog)
    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'].append(new_dialog_item)
    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):
@@ -114,6 +150,7 @@
    register_dict = {}
    token = ""
    app_password = await generate_password(10)
    crypt_password = UserAppModel.encrypted_password(app_password)
    for app in app_register:
        if app["id"] == 'ragflow_app':
            user_rag_app = db.query(UserAppModel).filter(UserAppModel.user_id == userid,
@@ -127,7 +164,7 @@
                app_name = register_info.get("name")
                app_id = register_info.get("id")
                app_email = register_info.get("email")
                await save_db(db, app_name, user.password, app_email, user.id, app_id, "ragflow_app")
                await save_db(db, app_name, crypt_password, app_email, user.id, app_id, "ragflow_app")
        elif app["id"] == 'bisheng_app':
            user_bs_app = db.query(UserAppModel).filter(UserAppModel.user_id == userid,
                                                        UserAppModel.app_type == 'bisheng_app').all()
@@ -140,7 +177,7 @@
                app_name = register_info.get("name")
                app_id = register_info.get("id")
                app_email = register_info.get("email")
                await save_db(db, app_name, user.password, app_email, user.id, app_id, "bisheng_app")
                await save_db(db, app_name, crypt_password, app_email, user.id, app_id, "bisheng_app")
        elif app["id"] == 'dify_app':
            user_df_app = db.query(UserAppModel).filter(UserAppModel.user_id == userid,
                                                        UserAppModel.app_type == 'dify_app').all()
@@ -156,9 +193,7 @@
                app_name = register_info.get("name")
                app_id = register_info.get("id")
                app_email = register_info.get("email")
                df_password = await generate_password(10)
                password = cipher_suite.encrypt(df_password.encode("utf-8")).decode("utf-8")
                await save_db(db, app_name, password, app_email, user.id, app_id, "dify_app")
                await save_db(db, app_name, crypt_password, app_email, user.id, app_id, "dify_app")
        else:
            raise Exception("未知注册应用---")
@@ -178,4 +213,4 @@
    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})
    print({"msg": "User registered successfully", "userFlag": user_id})