issue-ai/src/app/api/monitor/test-wechat/route.ts

32 lines
1.6 KiB
TypeScript

// src/app/api/monitor/test-wechat/route.ts
import { NextRequest, NextResponse } from 'next/server'
import { initDatabase } from '@/lib/db-schema'
import { getCurrentUser } from '@/lib/auth'
import { hasPermission } from '@/lib/permissions'
import { getMonitorConfig } from '@/lib/monitor/settings-manager'
import { writeAuditLog, getClientIP } from '@/lib/audit'
export async function POST(request: NextRequest) {
initDatabase()
const user = await getCurrentUser()
if (!user || !hasPermission(user, 'monitor:write')) return NextResponse.json({ error: '权限不足' }, { status: 403 })
const config = getMonitorConfig()
if (!config.wechat.webhook_url) return NextResponse.json({ success: false, error: 'Webhook URL 未配置' })
try {
const response = await fetch(config.wechat.webhook_url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ msgtype: 'text', text: { content: '✅ issue-ai 邮件监控测试消息' } }),
signal: AbortSignal.timeout(5_000),
})
const result = await response.json() as { errcode?: number; errmsg?: string }
if (result.errcode === 0) {
writeAuditLog({ userId: user.id, action: 'test', entityType: 'monitor_wechat', details: { message: '测试微信推送成功' }, ipAddress: getClientIP(request) })
return NextResponse.json({ success: true, message: '测试消息发送成功' })
}
return NextResponse.json({ success: false, error: result.errmsg || '发送失败' })
} catch (e) {
return NextResponse.json({ success: false, error: e instanceof Error ? e.message : '发送失败' })
}
}