9f90e16661
- 新增「查询」菜单组:监控配置、系统状态、今日工作日、最新公告 - 新增「系统管理」子菜单:暂停/恢复定时任务处理器 - 修复立即爬取被企微重试机制触发多次的问题(60秒防重入锁) - docker-compose 补充 image 名称和 container_name Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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": "today_stats",
|
|
},
|
|
{
|
|
"name": "查询",
|
|
"sub_button": [
|
|
{
|
|
"name": "监控配置",
|
|
"type": "click",
|
|
"key": "monitor_config",
|
|
},
|
|
{
|
|
"name": "系统状态",
|
|
"type": "click",
|
|
"key": "system_status",
|
|
},
|
|
{
|
|
"name": "今日工作日",
|
|
"type": "click",
|
|
"key": "workday_status",
|
|
},
|
|
{
|
|
"name": "最新公告",
|
|
"type": "click",
|
|
"key": "latest_announcements",
|
|
},
|
|
],
|
|
},
|
|
{
|
|
"name": "系统管理",
|
|
"sub_button": [
|
|
{
|
|
"name": "立即爬取",
|
|
"type": "click",
|
|
"key": "trigger_crawl",
|
|
},
|
|
{
|
|
"name": "同步节假日",
|
|
"type": "click",
|
|
"key": "sync_holidays",
|
|
},
|
|
],
|
|
},
|
|
]
|
|
}
|
|
|
|
|
|
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
|