a51161b5f3
- 新增 ai_whitelist 配置项,仅白名单用户可操作 AI 开关 - 新增 app/services/ai_state.py 运行时开关模块 - 系统管理菜单新增「AI 分析」按钮 (toggle_ai) - 监控配置、系统状态显示 AI 状态和 AI 标记统计 - pipeline 改用运行时开关 is_ai_enabled()
113 lines
3.3 KiB
Python
113 lines
3.3 KiB
Python
import logging
|
|
|
|
import httpx
|
|
|
|
from app.config import settings
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
MENU = {
|
|
"button": [
|
|
{
|
|
"name": "最新公告",
|
|
"type": "click",
|
|
"key": "latest_announcements",
|
|
},
|
|
{
|
|
"name": "查询",
|
|
"sub_button": [
|
|
{
|
|
"name": "监控配置",
|
|
"type": "click",
|
|
"key": "monitor_config",
|
|
},
|
|
{
|
|
"name": "系统状态",
|
|
"type": "click",
|
|
"key": "system_status",
|
|
},
|
|
{
|
|
"name": "今日工作日",
|
|
"type": "click",
|
|
"key": "workday_status",
|
|
},
|
|
],
|
|
},
|
|
{
|
|
"name": "系统管理",
|
|
"sub_button": [
|
|
{
|
|
"name": "立即爬取",
|
|
"type": "click",
|
|
"key": "trigger_crawl",
|
|
},
|
|
{
|
|
"name": "同步节假日",
|
|
"type": "click",
|
|
"key": "sync_holidays",
|
|
},
|
|
{
|
|
"name": "AI 分析",
|
|
"type": "click",
|
|
"key": "toggle_ai",
|
|
},
|
|
],
|
|
},
|
|
]
|
|
}
|
|
|
|
|
|
class MenuManager:
|
|
def __init__(self, client=None):
|
|
self.client = client
|
|
|
|
async def _get_token(self) -> str | None:
|
|
from app.wechat.client import WeChatClient
|
|
c = self.client or WeChatClient()
|
|
return await c._get_access_token()
|
|
|
|
async def create(self) -> bool:
|
|
token = await self._get_token()
|
|
if not token:
|
|
return False
|
|
|
|
url = "https://qyapi.weixin.qq.com/cgi-bin/menu/create"
|
|
params = {"access_token": token, "agentid": int(settings.wechat_agent_id)}
|
|
|
|
async with httpx.AsyncClient(timeout=15) as client:
|
|
response = await client.post(url, params=params, json=MENU)
|
|
data = response.json()
|
|
if data.get("errcode") == 0:
|
|
logger.info("企微菜单创建成功")
|
|
return True
|
|
logger.error(f"企微菜单创建失败: {data}")
|
|
return False
|
|
|
|
async def delete(self) -> bool:
|
|
token = await self._get_token()
|
|
if not token:
|
|
return False
|
|
|
|
url = "https://qyapi.weixin.qq.com/cgi-bin/menu/delete"
|
|
params = {"access_token": token, "agentid": int(settings.wechat_agent_id)}
|
|
|
|
async with httpx.AsyncClient(timeout=15) as client:
|
|
response = await client.get(url, params=params)
|
|
data = response.json()
|
|
return data.get("errcode") == 0
|
|
|
|
async def get(self) -> dict | None:
|
|
token = await self._get_token()
|
|
if not token:
|
|
return None
|
|
|
|
url = "https://qyapi.weixin.qq.com/cgi-bin/menu/get"
|
|
params = {"access_token": token, "agentid": int(settings.wechat_agent_id)}
|
|
|
|
async with httpx.AsyncClient(timeout=15) as client:
|
|
response = await client.get(url, params=params)
|
|
data = response.json()
|
|
if data.get("errcode") == 0:
|
|
return data
|
|
return None
|