@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* 企业微信群机器人 Webhook 通知
|
||||
* 在 .env 中配置 WECOM_WEBHOOK_KEY 即可启用
|
||||
* 不配置则静默跳过
|
||||
*
|
||||
* 文档:https://developer.work.weixin.qq.com/document/path/91770
|
||||
*/
|
||||
|
||||
const WECOM_API = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send'
|
||||
|
||||
/**
|
||||
* 发送企业微信群机器人 markdown 消息
|
||||
* @param {string} title - 消息标题(用作 markdown 一级标题)
|
||||
* @param {Record<string, string>} fields - { 字段名: 字段值 }
|
||||
*/
|
||||
export async function notifyWecom(title, fields) {
|
||||
const key = process.env.WECOM_WEBHOOK_KEY
|
||||
if (!key) return
|
||||
|
||||
const lines = Object.entries(fields)
|
||||
.map(([k, v]) => `> **${k}**:${v}`)
|
||||
.join('\n')
|
||||
|
||||
const now = new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' })
|
||||
|
||||
const content = `## ${title}\n${lines}\n> **提交时间**:${now}`
|
||||
|
||||
try {
|
||||
await $fetch(`${WECOM_API}?key=${key}`, {
|
||||
method: 'POST',
|
||||
body: {
|
||||
msgtype: 'markdown',
|
||||
markdown: { content },
|
||||
},
|
||||
})
|
||||
} catch (err) {
|
||||
// 通知失败不影响主流程,静默忽略
|
||||
console.error('[notify] 企业微信通知失败:', err?.message)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* 简单内存速率限制
|
||||
* 每个 IP 每小时最多允许 MAX_REQUESTS 次提交
|
||||
*/
|
||||
|
||||
const WINDOW_MS = 60 * 60 * 1000 // 1 小时
|
||||
const MAX_REQUESTS = 5
|
||||
|
||||
// Map<ip, { count: number, resetAt: number }>
|
||||
const store = new Map()
|
||||
|
||||
// 每 2 小时清理一次过期记录,防止内存泄漏
|
||||
setInterval(() => {
|
||||
const now = Date.now()
|
||||
for (const [ip, record] of store.entries()) {
|
||||
if (now > record.resetAt) {
|
||||
store.delete(ip)
|
||||
}
|
||||
}
|
||||
}, 2 * 60 * 60 * 1000)
|
||||
|
||||
/**
|
||||
* 检查并记录一次请求
|
||||
* @param {string} ip
|
||||
* @returns {{ allowed: boolean, remaining: number, resetAt: number }}
|
||||
*/
|
||||
export function checkRateLimit(ip) {
|
||||
const now = Date.now()
|
||||
const record = store.get(ip)
|
||||
|
||||
if (!record || now > record.resetAt) {
|
||||
store.set(ip, { count: 1, resetAt: now + WINDOW_MS })
|
||||
return { allowed: true, remaining: MAX_REQUESTS - 1, resetAt: now + WINDOW_MS }
|
||||
}
|
||||
|
||||
if (record.count >= MAX_REQUESTS) {
|
||||
return { allowed: false, remaining: 0, resetAt: record.resetAt }
|
||||
}
|
||||
|
||||
record.count++
|
||||
return { allowed: true, remaining: MAX_REQUESTS - record.count, resetAt: record.resetAt }
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* 文件存储工具
|
||||
* 将表单数据追加写入 server/storage/ 目录下的 JSON 文件
|
||||
*/
|
||||
|
||||
import { promises as fs } from 'fs'
|
||||
import { resolve } from 'path'
|
||||
|
||||
const STORAGE_DIR = resolve(process.cwd(), 'server/storage')
|
||||
|
||||
/**
|
||||
* 确保存储目录存在
|
||||
*/
|
||||
async function ensureDir() {
|
||||
try {
|
||||
await fs.mkdir(STORAGE_DIR, { recursive: true })
|
||||
} catch {
|
||||
// 目录已存在,忽略
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将记录追加到指定文件
|
||||
* @param {string} filename - 不含路径,如 'customize.json'
|
||||
* @param {object} record
|
||||
*/
|
||||
export async function appendRecord(filename, record) {
|
||||
await ensureDir()
|
||||
const filepath = resolve(STORAGE_DIR, filename)
|
||||
|
||||
let records = []
|
||||
try {
|
||||
const raw = await fs.readFile(filepath, 'utf-8')
|
||||
records = JSON.parse(raw)
|
||||
} catch {
|
||||
// 文件不存在或解析失败,从空数组开始
|
||||
}
|
||||
|
||||
records.push(record)
|
||||
await fs.writeFile(filepath, JSON.stringify(records, null, 2), 'utf-8')
|
||||
return record
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取所有记录(管理员用)
|
||||
* @param {string} filename
|
||||
*/
|
||||
export async function readRecords(filename) {
|
||||
const filepath = resolve(STORAGE_DIR, filename)
|
||||
try {
|
||||
const raw = await fs.readFile(filepath, 'utf-8')
|
||||
return JSON.parse(raw)
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户