feat(admin): 添加管理员日志查看接口
新增/api/admin/logs接口,允许管理员通过X-Admin-Token验证后查看应用日志 同时调整飞书推送日志的响应体截取长度从200增加到500字符
This commit is contained in:
parent
64baba32b4
commit
7aae6f9e52
@ -495,6 +495,23 @@ def admin_import():
|
||||
imported = import_csv_text(raw, actor='admin')
|
||||
return jsonify({"ok": True, "imported": imported})
|
||||
|
||||
@app.route('/api/admin/logs', methods=['GET'])
|
||||
def admin_logs():
|
||||
token = os.getenv('ADMIN_TOKEN')
|
||||
if token and request.headers.get('X-Admin-Token') != token:
|
||||
return jsonify({"error": "unauthorized"}), 401
|
||||
n = request.args.get('lines', default=200, type=int)
|
||||
p = os.path.join(os.path.dirname(__file__), "..", "app.log")
|
||||
if not os.path.exists(p):
|
||||
return jsonify({"lines": []})
|
||||
try:
|
||||
with open(p, 'r', encoding='utf-8') as f:
|
||||
lines = f.readlines()
|
||||
tail = lines[-n:] if n > 0 else lines
|
||||
return jsonify({"lines": [line.rstrip('\n') for line in tail]})
|
||||
except Exception:
|
||||
return jsonify({"lines": []})
|
||||
|
||||
def auto_import_csv_on_start():
|
||||
with app.app_context():
|
||||
flag = os.getenv('AUTO_IMPORT_ON_START', '1')
|
||||
@ -628,7 +645,7 @@ def push_feishu(date_str: str, amount: float, reason: str):
|
||||
if is_feishu:
|
||||
resp = _post_json(url, payload)
|
||||
ok = (200 <= resp.status_code < 300)
|
||||
_log(f"飞书推送卡片{'成功' if ok else '失败'}: status={resp.status_code} {resp.text[:200]}")
|
||||
_log(f"飞书推送卡片{'成功' if ok else '失败'}: status={resp.status_code} body={resp.text[:500]}")
|
||||
if not ok:
|
||||
post_payload = {
|
||||
"msg_type": "post",
|
||||
@ -649,13 +666,13 @@ def push_feishu(date_str: str, amount: float, reason: str):
|
||||
}
|
||||
resp_post = _post_json(url, post_payload)
|
||||
ok = (200 <= resp_post.status_code < 300)
|
||||
_log(f"飞书推送POST{'成功' if ok else '失败'}: status={resp_post.status_code} {resp_post.text[:200]}")
|
||||
_log(f"飞书推送POST{'成功' if ok else '失败'}: status={resp_post.status_code} body={resp_post.text[:500]}")
|
||||
if not ok:
|
||||
text = f"{shop}\n日期:{date_str}\n今日:¥{amount:.2f}"
|
||||
if isinstance(y_amt, (int, float)):
|
||||
text += f" {arrow} {diff_str} {pct_str}".strip()
|
||||
text += f"\n原因:{reason}"
|
||||
resp2 = _post_json(url, {"msg_type":"text","content":{"text": text}})
|
||||
_log(f"飞书推送文本{'成功' if (200 <= resp2.status_code < 300) else '失败'}: status={resp2.status_code} {resp2.text[:200]}")
|
||||
_log(f"飞书推送文本{'成功' if (200 <= resp2.status_code < 300) else '失败'}: status={resp2.status_code} body={resp2.text[:500]}")
|
||||
except Exception as e:
|
||||
_log(f"飞书推送异常: {str(e)[:200]}")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user