diff --git a/backend/__pycache__/main.cpython-310.pyc b/backend/__pycache__/main.cpython-310.pyc index 5749167..512d33e 100644 Binary files a/backend/__pycache__/main.cpython-310.pyc and b/backend/__pycache__/main.cpython-310.pyc differ diff --git a/backend/__pycache__/notify_service.cpython-310.pyc b/backend/__pycache__/notify_service.cpython-310.pyc index 52cb2fb..ec6ecf7 100644 Binary files a/backend/__pycache__/notify_service.cpython-310.pyc and b/backend/__pycache__/notify_service.cpython-310.pyc differ diff --git a/backend/main.py b/backend/main.py index 048f88a..fd9ddec 100644 --- a/backend/main.py +++ b/backend/main.py @@ -3,6 +3,10 @@ from fastapi import FastAPI from fastapi.staticfiles import StaticFiles from fastapi.middleware.cors import CORSMiddleware import os +import sys + +# 添加项目根目录到路径 +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from backend.database import init_db from backend.routers import channels, notify, history @@ -35,15 +39,15 @@ app.include_router(channels.router) app.include_router(notify.router) app.include_router(history.router) -# 静态文件(前端) -frontend_path = os.path.join(os.path.dirname(__file__), "..", "frontend") -if os.path.exists(frontend_path): - app.mount("/", StaticFiles(directory=frontend_path, html=True), name="frontend") - @app.get("/api/health") async def health_check(): return {"status": "ok"} +# 静态文件(前端)- 放在最后 +frontend_path = os.path.join(os.path.dirname(__file__), "..", "frontend") +if os.path.exists(frontend_path): + app.mount("/", StaticFiles(directory=frontend_path, html=True), name="frontend") + if __name__ == "__main__": import uvicorn uvicorn.run("backend.main:app", host="0.0.0.0", port=8000, reload=True) diff --git a/backend/notify_service.py b/backend/notify_service.py index e096ca7..1331513 100644 --- a/backend/notify_service.py +++ b/backend/notify_service.py @@ -48,6 +48,14 @@ class NotifyService: elif channel.type == "apprise": return config.get("url", "") + # Bark iOS 推送 - 在 send_notification 中直接处理 + elif channel.type == "bark": + base_url = config.get("base_url", "") + device_key = config.get("device_key", "") + if base_url and device_key: + # 返回特殊标记,实际发送在 send_notification 中处理 + return "__bark__" + return None async def send_notification( @@ -101,17 +109,60 @@ class NotifyService: } try: - # 发送通知 - apobj = apprise.Apprise() - apobj.add(apprise_url) - - # 构建消息 - message = body - if title: - message = f"**{title}**\n\n{body}" - - # 发送 - result = apobj.notify(body=message) + # Bark 特殊处理 - 使用 requests + if channel.type == "bark": + import requests + import urllib.parse + + base_url = channel.config.get("base_url", "").rstrip("/") + device_key = channel.config.get("device_key", "") + + # 强制使用 HTTP(因为你的 Bark 服务器 SSL 有问题) + if base_url.startswith("https://"): + base_url = base_url.replace("https://", "http://") + elif not base_url.startswith("http://"): + base_url = "http://" + base_url + + # 构建 URL + encoded_body = urllib.parse.quote(str(body), safe='') + encoded_title = urllib.parse.quote(str(title), safe='') if title else "" + + if encoded_title: + bark_url = f"{base_url}/{device_key}/{encoded_title}/{encoded_body}" + else: + bark_url = f"{base_url}/{device_key}/{encoded_body}" + + # 添加参数 + params = {} + if priority == "high": + params["level"] = "active" + elif priority == "urgent": + params["level"] = "critical" + + try: + headers = { + 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X)', + 'Accept': 'application/json, text/plain, */*', + 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8', + } + response = requests.get(bark_url, params=params, timeout=10, headers=headers) + result = response.status_code == 200 + print(f"Bark response: {response.status_code}, {response.text[:100]}") + except Exception as e: + result = False + print(f"Bark error: {e}") + else: + # 使用 apprise 发送通知 + apobj = apprise.Apprise() + apobj.add(apprise_url) + + # 构建消息 + message = body + if title: + message = f"**{title}**\n\n{body}" + + # 发送 + result = apobj.notify(body=message) if result: await crud.update_notification_status(