102 lines
3.1 KiB
Plaintext
102 lines
3.1 KiB
Plaintext
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 |