diff --git a/jcloud/api/account.py b/jcloud/api/account.py index 42edd1b..a9a91fb 100644 --- a/jcloud/api/account.py +++ b/jcloud/api/account.py @@ -1539,103 +1539,230 @@ def update_profile_email(email): } @jingrow.whitelist() -def verify_api_credentials_and_balance(api_key: str, api_secret: str, amount: float = 0): - """验证API密钥和检查余额 +def verify_api_credentials_and_balance(api_key: str, api_secret: str, api_name: str): + """ + 验证API密钥和团队余额 + + 参数: + api_key: API密钥 + api_secret: API密钥 + api_name: API名称 + + 返回: + { + "success": bool, + "message": str + } + """ + try: + # 获取当前用户(管理员)的API信息 + admin_user = jingrow.session.user + + # 验证管理员权限 + if admin_user != "Administrator": + return { + "success": False, + "message": "只有管理员用户才能访问此API" + } + + # 验证用户的API密钥 + user = jingrow.db.get_value( + "User", + {"api_key": api_key}, + ["name", "enabled", "api_secret"] + ) + + if not user: + return { + "success": False, + "message": "无效的API密钥" + } + + # 验证用户的API密钥 + stored_secret = get_decrypted_password("User", user[0], "api_secret") + if stored_secret != api_secret: + return { + "success": False, + "message": "无效的API密钥" + } + + if not user[1]: # 检查用户是否启用 + return { + "success": False, + "message": "用户账户已禁用" + } + + # 获取用户团队 + team = jingrow.db.get_value( + "Team", + {"user": user[0]}, + ["name", "enabled"] + ) + + if not team: + return { + "success": False, + "message": "未找到用户团队" + } + + if not team[1]: # 检查团队是否启用 + return { + "success": False, + "message": "团队账户已禁用" + } + + # 获取团队余额 + team_pg = jingrow.get_pg("Team", team[0]) + balance = team_pg.get_balance() + + # 获取API价格 + price = jingrow.db.get_value("Api Pricing", {"api_name": api_name}, "price") or 0 + if price <= 0: + return { + "success": False, + "message": "API价格未设置" + } + + # 检查余额是否足够 + has_sufficient_balance = balance >= price + + if not has_sufficient_balance: + return { + "success": False, + "message": "余额不足" + } + + return { + "success": True, + "message": "验证成功" + } + + except Exception as e: + return { + "success": False, + "message": f"验证过程发生错误: {str(e)}" + } + +@jingrow.whitelist() +def deduct_api_usage_fee(api_key: str, api_secret: str, api_name: str, usage_count: int = 1): + """ + 从API用户的团队扣除API调用费用 + + 参数: + api_key: API密钥 + api_secret: API密钥 + api_name: API名称 + usage_count: API使用次数,默认为1 + + 返回: + { + "success": bool, + "message": str + } + """ + try: + # 获取当前用户(管理员) + admin_user = jingrow.session.user + + # 验证管理员权限 + if admin_user != "Administrator": + jingrow.log_error("API扣费", f"非管理员用户尝试访问API: {admin_user}") + return { + "success": False, + "message": "只有管理员用户才能访问此API" + } + + # 验证用户的API密钥 + user = jingrow.db.get_value( + "User", + {"api_key": api_key}, + ["name", "enabled", "api_secret"] + ) + + if not user: + jingrow.log_error("API扣费", f"无效的API密钥: {api_key}") + return { + "success": False, + "message": "无效的API密钥" + } + + # 验证用户的API密钥 + stored_secret = get_decrypted_password("User", user[0], "api_secret") + if stored_secret != api_secret: + return { + "success": False, + "message": "无效的API密钥" + } + + if not user[1]: # 检查用户是否启用 + return { + "success": False, + "message": "用户账户已禁用" + } + + # 获取用户团队 + team = jingrow.db.get_value( + "Team", + {"user": user[0]}, + ["name", "enabled"] + ) + + if not team: + return { + "success": False, + "message": "未找到用户团队" + } + + if not team[1]: # 检查团队是否启用 + return { + "success": False, + "message": "团队账户已禁用" + } + + # 获取团队对象 + team_pg = jingrow.get_pg("Team", team[0]) + + # 获取团队余额 + current_balance = team_pg.get_balance() + + # 获取API价格 + price = jingrow.db.get_value("Api Pricing", {"api_name": api_name}, "price") or 0 + + if price <= 0: + return { + "success": False, + "message": "API价格未设置" + } + + # 计算总费用 + total_price = price * usage_count + + # 创建余额交易记录(扣款) + balance_transaction = jingrow.get_pg({ + "pagetype": "Balance Transaction", + "team": team[0], # 使用API的团队 + "type": "Adjustment", + "source": "Prepaid Credits", + "amount": -1 * float(total_price), # 使用负数表示扣减 + "description": f"API使用费: {api_name} x {usage_count}次", + }) + + # 保存交易记录 + balance_transaction.insert(ignore_permissions=True) + balance_transaction.submit() + + jingrow.db.commit() + return { + "success": True, + "message": "扣费成功" + } + + except Exception as e: + error_msg = f"扣费过程发生错误: {str(e)}" + jingrow.log_error("API扣费错误", error_msg) + return { + "success": False, + "message": error_msg + } - 参数: - api_key: 用户的API密钥 - api_secret: 用户的API密钥对应的密钥 - amount: 需要检查的余额金额,默认为0 - - 返回: - { - "success": bool, # 验证是否成功 - "message": str # 错误信息 - } - """ - try: - # 记录验证请求信息 - jingrow.log_error("API验证请求参数", f"api_key={api_key}, api_secret={api_secret}, amount={amount}") - - # 获取当前用户(管理员)的API信息 - admin_user = jingrow.session.user - jingrow.log_error("API验证管理员信息", f"当前用户: {admin_user}") - - # 验证管理员权限 - if admin_user != "Administrator": - return { - "success": False, - "message": "只有管理员用户才能访问此API" - } - - # 验证用户的API密钥 - user = jingrow.db.get_value( - "User", - {"api_key": api_key}, - ["name", "enabled", "api_secret"] - ) - - jingrow.log_error("API验证用户查询", f"用户查询结果: {user}") - - if not user: - return { - "success": False, - "message": "无效的API密钥" - } - - # 验证用户的API密钥 - stored_secret = get_decrypted_password("User", user[0], "api_secret") - if stored_secret != api_secret: - jingrow.log_error("API验证密钥不匹配", f"API密钥不匹配。数据库中的密钥: {stored_secret}") - return { - "success": False, - "message": "无效的API密钥" - } - - if not user[1]: # 检查用户是否启用 - return { - "success": False, - "message": "用户账户已禁用" - } - - # 获取用户团队 - team = jingrow.db.get_value( - "Team", - {"user": user[0]}, - ["name", "enabled"] - ) - - jingrow.log_error("API验证团队信息", f"团队信息: {team}") - - if not team: - return { - "success": False, - "message": "未找到用户团队" - } - - if not team[1]: # 检查团队是否启用 - return { - "success": False, - "message": "团队账户已禁用" - } - - # 获取团队余额 - team_pg = jingrow.get_pg("Team", team[0]) - balance = team_pg.get_balance() - - jingrow.log_error("API验证团队余额", f"团队余额: {balance}") - - # 检查余额是否足够 - has_sufficient_balance = balance >= amount - - return { - "success": True, - "message": "验证成功" if has_sufficient_balance else "余额不足" - } - - except Exception as e: - jingrow.log_error("API验证系统错误", f"API验证失败: {str(e)}") - return { - "success": False, - "message": f"验证过程发生错误: {str(e)}" - } + \ No newline at end of file