Files
sweet-hut-agent/tools/bill.py
2026-07-02 22:17:33 +08:00

68 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from langchain_core.tools import tool
import requests
BASE_URL = "https://cxx0822.s.3q.hair/home-api/"
USERNAME = "Cxx0822"
PASSWORD = "19940822Cxx"
def get_token() -> str:
"""
调用登录接口,返回 saToken.tokenValue
"""
params = {
"name": USERNAME,
"password": PASSWORD
}
try:
resp = requests.post(f"{BASE_URL}session", params=params, timeout=10)
resp.raise_for_status()
data = resp.json()
# 按你给的返回结构取值
token_value = data["saToken"]["tokenValue"]
return token_value
except requests.exceptions.RequestException as e:
raise RuntimeError(f"登录失败,无法获取 token: {e}")
except KeyError as e:
raise RuntimeError(f"登录响应结构异常,未找到 tokenValue: {e}")
@tool
def bill_search(start_date: str, end_date: str) -> str:
"""
查询账单列表。
参数说明:
- start_date (必填):
- 开始日期格式YYYY-MM-DD
- 示例2026-06-01
- end_date (必填):
- 结束日期格式YYYY-MM-DD
- 示例2026-07-01
"""
print(f"start_date: {start_date}, end_date: {end_date}")
params = {
"bookName": "",
"type": "",
"startDate": start_date,
"endDate": end_date
}
token = get_token()
headers = {
"satoken": token
}
try:
resp = requests.get(f"{BASE_URL}bill/summary", params=params, headers=headers, timeout=10)
resp.raise_for_status()
return resp.text
except Exception as e:
return f"查询账单失败: {e}"