diff --git a/scripts/load_path.py b/scripts/load_path.py index dfe341c..07dec68 100644 --- a/scripts/load_path.py +++ b/scripts/load_path.py @@ -156,6 +156,7 @@ PATHS_LOGINED = [ f"/{MOD}/api/exchange_rate_create.dspy", f"/{MOD}/api/exchange_rate_update.dspy", f"/{MOD}/api/exchange_rate_delete.dspy", + f"/{MOD}/api/fetch_forex_rates.dspy", # proxy_recharge/ f"/{MOD}/proxy_recharge.ui", diff --git a/wwwroot/api/fetch_forex_rates.dspy b/wwwroot/api/fetch_forex_rates.dspy new file mode 100644 index 0000000..65587b9 --- /dev/null +++ b/wwwroot/api/fetch_forex_rates.dspy @@ -0,0 +1,63 @@ +# 获取中国银行外汇牌价并插入 exchange_rate 表 +# GET /accounting/api/fetch_forex_rates.dspy +import re +import urllib.request +from datetime import date + +BOC_URL = 'https://www.boc.cn/sourcedb/whpj/index.html' +CURRENCIES = {'美元': 'USD', '日元': 'JPY', '英镑': 'GBP'} + +async with get_sor_context(request._run_ns, 'accounting') as sor: + # 1. 抓取 BOC 页面 + try: + req = urllib.request.Request(BOC_URL, headers={'User-Agent': 'Mozilla/5.0'}) + with urllib.request.urlopen(req, timeout=15) as resp: + html = resp.read().decode('utf-8', errors='replace') + except Exception as e: + return json.dumps({'success': False, 'message': f'获取BOC页面失败: {e}'}, ensure_ascii=False) + + today = date.today().isoformat() + inserted = 0 + + for cn_name, code in CURRENCIES.items(): + pos = html.find(cn_name) + if pos < 0: + continue + chunk = html[pos:pos+500] + tds = re.findall(r'