tmp
zhaoqingang
2025-01-14 00fe58a94292a3b9921ce134542ee38d74cd9401
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import json
import time
import os
 
import yaml
 
from Log import logger
from app.config.const import DIFY, ENV_CONF_PATH
from app.models import MenuCapacityModel, WebMenuModel, GroupModel, RoleModel, DialogModel, UserModel, UserAppModel, \
    cipher_suite, UserTokenModel
from app.service.auth import UserAppDao
from app.service.bisheng import BishengService
from app.service.difyService import DifyService
from app.service.ragflow import RagflowService
from app.service.service_token import get_new_token
from app.service.v2.app_register import AppRegisterDao
from app.config.config import settings
from app.utils.password_handle import generate_password
 
 
async def dialog_menu_sync(db):
    menu_list = []
    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", [])
 
    db.query(WebMenuModel).delete()
    db.query(MenuCapacityModel).delete()
    db.commit()
 
    for menu in menu_list:
        # print(menu)
        dialog = menu.pop("dialog", [])
        for i in dialog:
            capacity = MenuCapacityModel(menu_id=menu["id"], capacity_id=i["id"], capacity_type=i["agentType"],
                                         chat_id=i["id"] if not i["chat_id"] else i["chat_id"],
                                         chat_type=i["chat_type"])
            db.add(capacity)
        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):
    group = db.query(GroupModel).filter_by(group_type=2).first()
    if not group:
        logger.error("未初始默认组, 开始初始化!")
 
        try:
            group = GroupModel(group_name="默认用户组", group_description="默认组", group_type=2)
            db.add(group)
            db.commit()
        except Exception as e:
            logger.error(e)
 
 
async def default_role_sync(db):
    role = db.query(RoleModel).filter_by(role_type=2).first()
    if not role:
        logger.error("未初始默认角色, 开始初始化!")
 
        try:
            group = RoleModel(id="morenjuese1234567890", name="默认角色", description="默认角色", role_type=2)
            db.add(group)
            db.commit()
        except Exception as e:
            logger.error(e)
 
 
async def app_register_sync(db):
    app_dict = {}
    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:
            for app_id, status in app_dict.items():
                AppRegisterDao(db).update_and_insert_app(app_id, status)
        except Exception as e:
            logger.error(e)
 
 
async def basic_agent_sync(db):
    agent_list = []
    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", [])
    user = db.query(UserModel).filter_by(permission="admin").first()
    for agent in agent_list:
        dialog = db.query(DialogModel).filter(DialogModel.id == agent["id"]).first()
        if dialog:
            try:
                dialog.name = agent["name"]
                dialog.description = agent["description"]
                dialog.icon = agent["icon"]
                db.commit()
            except Exception as e:
                logger.error(e)
        else:
            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"])
                db.add(dialog)
                db.commit()
                db.refresh(dialog)
            except Exception as e:
                print(e)
                db.rollback()
 
 
async def user_update_app(userid, db):
    user = db.query(UserModel).filter(UserModel.id == userid).first()
    if not user:
        raise Exception("User id not found")
    app_register = AppRegisterDao(db).get_apps()
    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,
                                                         UserAppModel.app_type == 'ragflow_app').all()
            if not user_rag_app:
                service = RagflowService(settings.fwr_base_url)
 
                register_info = await register_app(service, app["id"], app_password, token)
                if register_info:
                    register_dict[app["id"]] = register_info
                app_name = register_info.get("name")
                app_id = register_info.get("id")
                app_email = register_info.get("email")
                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()
            if not user_bs_app:
                service = BishengService(settings.sgb_base_url)
 
                register_info = await register_app(service, app["id"], app_password, token)
                if register_info:
                    register_dict[app["id"]] = register_info
                app_name = register_info.get("name")
                app_id = register_info.get("id")
                app_email = register_info.get("email")
                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()
            if not user_df_app:
                admin_user = db.query(UserModel).filter(UserModel.permission == "admin").first()
                token = await get_new_token(db, admin_user.id, DIFY)
                if not token:
                    print("用户注册获取dftoken失败!")
                service = DifyService(settings.dify_base_url)
                register_info = await register_app(service, app["id"], app_password, token)
                if register_info:
                    register_dict[app["id"]] = register_info
                app_name = register_info.get("name")
                app_id = register_info.get("id")
                app_email = register_info.get("email")
                await save_db(db, app_name, crypt_password, app_email, user.id, app_id, "dify_app")
        else:
            raise Exception("未知注册应用---")
 
 
async def register_app(service, app_id, app_password, token):
    name = app_id + str(int(time.time()))
    try:
        register_info = await service.register(name, app_password, token)
        return {"id": register_info.get("id"), "name": name, "email": register_info.get("email")}
    except Exception as e:
        print(f"Failed to register with {app_id}: {str(e)}")
        return None
 
 
async def save_db(db, username, password, email, user_id, app_id, app_type):
    user_app_dao = UserAppDao(db)
    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})
 
 
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()