From d8968afe83bca750fc1e8faf32fc687a9e64e657 Mon Sep 17 00:00:00 2001 From: ping <1017253325@qq.com> Date: Mon, 18 May 2026 18:01:33 +0800 Subject: [PATCH] update --- b/cntoai/sync_cn_ai_user.dspy | 102 ++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 b/cntoai/sync_cn_ai_user.dspy diff --git a/b/cntoai/sync_cn_ai_user.dspy b/b/cntoai/sync_cn_ai_user.dspy new file mode 100644 index 0000000..48383ed --- /dev/null +++ b/b/cntoai/sync_cn_ai_user.dspy @@ -0,0 +1,102 @@ +async def sync_cn_ai_user(ns={}): + import aiohttp + + user_info = None + if ns.get('userid'): + userid = ns.get('userid') + db = DBPools() + async with db.sqlorContext('kboss') as sor: + user_info = await sor.R('users', {'id': userid}) + if not user_info: + return { + 'status': False, + 'msg': '未找到匹配的用户' + } + userid = user_info[0]['id'] + orgid = user_info[0]['orgid'] + username = user_info[0]['username'] + name = user_info[0]['name'] + email = user_info[0]['email'] + + already_sync_user_key = '2i68AZ81di_q5f8AySDrJ' + already_sync_user_dappid = 'cndemo' + + + # 目标URL + url = "https://ai.atvoe.com/rbac/usersync" + # url = 'https://ai.atvoe.com/tmp/env.dspy' + + # 请求头 + headers = { + "Content-Type": "application/json", + "Authorization": "Bearer %s" % already_sync_user_key + } + + # 请求体数据 + payload = { + "action": "single", + "dappid": already_sync_user_dappid, + "user": { + "id": userid, + "orgid": orgid, + "username": username, + "name": name, + "email": email + } + } + + try: + # 创建一个异步会话 + result_sysnc = None + async with aiohttp.ClientSession() as session: + # 发送POST请求 + async with session.post(url, headers=headers, data=json.dumps(payload)) as response: + # 打印响应状态码 + print(f"状态码: {response.status}") + result_sysnc = await response.json() + + if not result_sysnc.get('status') == 'success': + print(f"同步用户失败: {result_sysnc}") + return { + 'status': False + } + + db = DBPools() + async with db.sqlorContext('kboss') as sor: + # user_api_keys表格 userid/opc_apikey + # 首先判断apikey是否存在 + apikey = result_sysnc['data'][0].get('apikey') + appid = result_sysnc['data'][0].get('appid') + secretkey = result_sysnc['data'][0].get('secretkey') + + records = await sor.R('user_api_keys', {'opc_apikey': apikey}) + if records: + print(f"用户{payload['user']['id']}已存在") + return { + 'status': False, + 'msg': f'用户opc_apikey已存在, {result_sysnc}' + } + print(f"{result_sysnc}") + await sor.C('user_api_keys', { + 'userid': userid, + 'opc_apikey': apikey, + 'appid': appid, + 'secretkey': secretkey, + 'expire_time': None, + }) + + return { + 'status': True, + 'msg': '用户同步成功' + } + + except Exception as e: + print(f"同步用户失败: {e}") + return { + 'status': False, + 'msg': f"同步用户失败: {e}" + } + + +ret = await sync_cn_ai_user(params_kw) +return ret \ No newline at end of file