diff --git a/web/src/views/notify/components/ApiUsage.vue b/web/src/views/notify/components/ApiUsage.vue index 257b051..6c1a579 100644 --- a/web/src/views/notify/components/ApiUsage.vue +++ b/web/src/views/notify/components/ApiUsage.vue @@ -4,6 +4,7 @@ import { Input } from '@/components/ui/input' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Badge } from '@/components/ui/badge' import { Copy, Terminal, Key, FileJson, RefreshCw, Check, Hash, Info, AlertTriangle, Code2 } from 'lucide-vue-next' +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs' import { AlertDialog, @@ -68,99 +69,106 @@ notify-token: <你的API Token> "text": "内容" }` -const shellExample = computed(() => `send_notification() { - curl -s -X POST "http://${host.value}/api/v1/notify/send" \\ - -H "Content-Type: application/json" \\ - -H "notify-token: \${1:-${props.apiToken || 'YOUR_TOKEN'}}" \\ - -d "{\\"channel_id\\":\\"\$2\\",\\"title\\":\\"\$3\\",\\"text\\":\\"\$4\\"}" -} +const activeLang = ref('shell') +const testTitle = ref('系统通知') +const testText = ref('这是一条测试消息内容') +const testChannel = ref(props.channels[0]?.id || 'ID') -# 使用示例: send_notification <渠道ID> <标题> <内容> -send_notification "${props.apiToken || 'YOUR_TOKEN'}" "ID" "任务完成" "脚本执行完毕"`) +const shellExample = computed(() => `# 1. 设置环境变量 (可选) +# export BH_NOTIFY_TOKEN="${props.apiToken || 'YOUR_TOKEN'}" + +# 2. 调用 (优先使用环境变量) +TOKEN="\${BH_NOTIFY_TOKEN:-${props.apiToken || 'YOUR_TOKEN'}}" +curl -s -X POST "http://${host.value}/api/v1/notify/send" \\ + -H "Content-Type: application/json" \\ + -H "notify-token: $TOKEN" \\ + -d '{"channel_id":"${testChannel.value}","title":"${testTitle.value}","text":"${testText.value}"}'`) const pythonExample = computed(() => `import requests +import os -def send_notification(token, channel_id, text, title="通知"): - url = "http://${host.value}/api/v1/notify/send" - headers = { - "Content-Type": "application/json", - "notify-token": token - } - payload = { - "channel_id": channel_id, - "title": title, - "text": text - } - return requests.post(url, json=payload, headers=headers).json() +def send_notify(text, title="${testTitle.value}", channel="${testChannel.value}"): + token = os.getenv("BH_NOTIFY_TOKEN", "${props.apiToken || 'YOUR_TOKEN'}") + url = f"http://${host.value}/api/v1/notify/send" + data = {"channel_id": channel, "title": title, "text": text} + return requests.post(url, json=data, headers={"notify-token": token}).json() -# 使用示例 -send_notification("${props.apiToken || 'YOUR_TOKEN'}", "ID", "脚本执行完毕", "任务完成")`) +# 调用示例 +print(send_notify("${testText.value}"))`) -const javascriptExample = computed(() => `async function sendNotification(token, channelId, text, title = "通知") { - const url = "http://${host.value}/api/v1/notify/send"; - const res = await fetch(url, { - method: "POST", - headers: { - "Content-Type": "application/json", - "notify-token": token - }, +const javascriptExample = computed(() => `/** + * 发送通知 (优先使用环境变量 BH_NOTIFY_TOKEN) + * @param {string} text 消息内容 + */ +const sendNotify = async (text) => { + const token = (typeof process !== 'undefined' ? process.env.BH_NOTIFY_TOKEN : null) || "${props.apiToken || 'YOUR_TOKEN'}"; + const res = await fetch("http://${host.value}/api/v1/notify/send", { body: JSON.stringify({ - channel_id: channelId, - title: title, + channel_id: "${testChannel.value}", + title: "${testTitle.value}", text: text }) }); - return res.json(); -} + return await res.json(); +}; -// 使用示例 -sendNotification("${props.apiToken || 'YOUR_TOKEN'}", "ID", "脚本执行完毕", "任务完成");`) +sendNotify("${testText.value}");`) const goExample = computed(() => `package main - import ( "bytes" "encoding/json" - "fmt" "net/http" + "os" ) -func sendNotification(token, channelID, title, text string) error { - url := "http://${host.value}/api/v1/notify/send" - payload := map[string]string{ - "channel_id": channelID, +func sendNotify(title, text string) { + token := os.Getenv("BH_NOTIFY_TOKEN") + if token == "" { + token = "${props.apiToken || 'YOUR_TOKEN'}" + } + payload, _ := json.Marshal(map[string]string{ + "channel_id": "${testChannel.value}", "title": title, "text": text, - } - body, _ := json.Marshal(payload) - - req, _ := http.NewRequest("POST", url, bytes.NewBuffer(body)) + }) + req, _ := http.NewRequest("POST", "http://${host.value}/api/v1/notify/send", bytes.NewBuffer(payload)) req.Header.Set("Content-Type", "application/json") req.Header.Set("notify-token", token) - - client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - return err - } - defer resp.Body.Close() - return nil + http.DefaultClient.Do(req) } func main() { - // 使用示例 - sendNotification("${props.apiToken || 'YOUR_TOKEN'}", "ID", "任务完成", "脚本执行完毕") + sendNotify("${testTitle.value}", "${testText.value}") }`) +const phpExample = computed(() => ` $channel, + "title" => $title, + "text" => $text + ])); + curl_exec($curl); +} +sendNotify("${testText.value}"); +?>`) + const examples = [ { id: 'shell', name: 'Shell', icon: Terminal, code: shellExample }, { id: 'python', name: 'Python', icon: Code2, code: pythonExample }, { id: 'javascript', name: 'JavaScript', icon: Code2, code: javascriptExample }, { id: 'go', name: 'Go', icon: Code2, code: goExample }, + { id: 'php', name: 'PHP', icon: Code2, code: phpExample } ] -const activeLang = ref('shell') - const highlightCode = (code: string, lang: string) => { if (!code) return '' @@ -181,7 +189,7 @@ const highlightCode = (code: string, lang: string) => { .replace(/>/g, '>') // 1. 处理字符串 (优先处理,防止内部匹配) - html = html.replace(/("(?:\\.|[^"])*")|('(?:\\.|[^'])*')/g, `$1`) + html = html.replace(/("(?:\\.|[^"])*"|'(?:\\.|[^'])*')/g, `$&`) // 2. 处理注释 (注意排除 http:// 或 https:// 中的双斜杠) html = html.replace(/(^|[^\:])(\/\/.+)$|(#.+)$/gm, `$1$2$3`) @@ -189,24 +197,29 @@ const highlightCode = (code: string, lang: string) => { // 3. 语言配置 const langConfig: Record = { shell: { - keywords: ['curl'], + keywords: ['curl', 'export'], types: [], functions: ['send_notification'] }, python: { - keywords: ['import', 'def', 'return', 'as', 'from'], + keywords: ['import', 'def', 'return', 'as', 'from', 'print'], types: ['dict', 'list', 'str', 'int', 'float'], - functions: ['send_notification', 'post', 'json'] + functions: ['send_notify', 'post', 'json', 'getenv'] }, javascript: { - keywords: ['async', 'await', 'function', 'const', 'return', 'let', 'var', 'if', 'else', 'try', 'catch'], + keywords: ['async', 'await', 'function', 'const', 'return', 'let', 'var', 'if', 'else'], types: ['JSON', 'Promise', 'fetch'], - functions: ['sendNotification', 'stringify', 'json', 'post'] + functions: ['sendNotify', 'stringify', 'json', 'post'] }, go: { - keywords: ['package', 'import', 'func', 'return', 'map', 'defer', 'if', 'nil', 'go', 'main'], + keywords: ['package', 'import', 'func', 'return', 'map', 'defer', 'main'], types: ['string', 'error', 'byte', 'int'], - functions: ['Marshal', 'NewRequest', 'Set', 'Do', 'Close', 'Sprintf'] + functions: ['Marshal', 'NewRequest', 'Set', 'Do', 'Close', 'sendNotify'] + }, + php: { + keywords: ['function', 'return', 'true'], + types: [], + functions: ['curl_init', 'curl_setopt', 'curl_exec', 'json_encode', 'sendNotify'] } } @@ -240,7 +253,6 @@ const currentExample = computed(() => {