feat: add message template

This commit is contained in:
engigu
2025-12-06 00:28:18 +08:00
parent ff194daef2
commit a511512793
21 changed files with 2608 additions and 155 deletions
+151 -86
View File
@@ -1,11 +1,10 @@
const gethttpOrigin = () => {
return window.location.origin
}
class ApiStrGenerate {
// ==================== 公共加密工具 ====================
class TokenEncryption {
// 根据字符串内容生成确定性 salt(范围 0~255)
static getDeterministicSalt(text) {
let sum = 0;
@@ -17,7 +16,7 @@ class ApiStrGenerate {
// 加密:首字节为salt,后续为按位异或后的数据
static encryptHex(text, key) {
const salt = ApiStrGenerate.getDeterministicSalt(text);
const salt = TokenEncryption.getDeterministicSalt(text);
let result = salt.toString(16).padStart(2, '0');
for (let i = 0; i < text.length; i++) {
const code = text.charCodeAt(i) ^ (key & 0xFF) ^ ((salt + i) & 0xFF);
@@ -25,46 +24,19 @@ class ApiStrGenerate {
}
return result;
}
}
static getDataString(task_id, options) {
// 新版仅展示 token;兼容旧版 task_id(后端依然支持)
let data = { token: ApiStrGenerate.encryptHex(task_id, 71) };
data.title = 'message title';
data.text = 'Hello World!';
if (options.html) {
data.html = '<h1> Hello World! </h1>';
}
if (options.markdown) {
data.markdown = '**Hello World!**';
}
if (options.url) {
data.url = 'https://github.com';
}
// @提及功能参数(可选)
if (options.at_mobiles) {
data.at_mobiles = ['13800138000', '13900139000'];
}
if (options.at_user_ids) {
data.at_user_ids = ['zhangsan', 'lisi'];
}
if (options.at_all) {
data.at_all = true;
}
let dataStr = JSON.stringify(data, null, 4);
return dataStr
// ==================== 公共代码模板生成器 ====================
class CodeTemplates {
static getCurl(url, dataStr) {
return `curl -X POST --location '${url}' \\
--header 'Content-Type: application/json' \\
--data '${dataStr}'`;
}
static getCurlString(task_id, options) {
let dataStr = ApiStrGenerate.getDataString(task_id, options);
let example = `curl -X POST --location '${gethttpOrigin()}/api/v1/message/send' \\
--header 'Content-Type: application/json' \\
--data '${dataStr}'`;
return example;
}
static getGolangString(task_id, options) {
let dataStr = ApiStrGenerate.getDataString(task_id, options);
let example = `package main
static getGolang(url, dataStr) {
return `package main
import (
"fmt"
@@ -77,7 +49,7 @@ import (
func main() {
client := &http.Client{}
var data = strings.NewReader(\`${dataStr}\`)
req, err := http.NewRequest("POST", "${gethttpOrigin()}/api/v1/message/send", data)
req, err := http.NewRequest("POST", "${url}", data)
if err != nil {
log.Fatal(err)
}
@@ -92,29 +64,23 @@ func main() {
log.Fatal(err)
}
fmt.Printf("%s\\n", bodyText)
}
`;
return example;
}`;
}
static getPythonString(task_id, options) {
let dataStr = ApiStrGenerate.getDataString(task_id, options);
let example = `import requests
static getPython(url, dataStr) {
return `import requests
headers = {
'Content-Type': 'application/json',
}
json_data = ${dataStr}
response = requests.post('${gethttpOrigin()}/api/v1/message/send', headers=headers, json=json_data)
response = requests.post('${url}', headers=headers, json=json_data)
print("response:", response.json())
`;
return example;
print("response:", response.json())`;
}
static getJaveString(task_id, options) {
let dataStr = ApiStrGenerate.getDataString(task_id, options);
let example = `import java.io.IOException;
static getJava(url, dataStr) {
return `import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
@@ -126,19 +92,16 @@ HttpClient client = HttpClient.newBuilder()
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("${gethttpOrigin()}/api/v1/message/send"))
.POST(BodyPublishers.ofString(${JSON.stringify(dataStr).trim('\"')}))
.uri(URI.create("${url}"))
.POST(BodyPublishers.ofString(${JSON.stringify(dataStr).trim('"')}))
.setHeader("Content-Type", "application/json")
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
`;
return example;
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());`;
}
static getRustString(task_id, options) {
let dataStr = ApiStrGenerate.getDataString(task_id, options);
let example = `extern crate reqwest;
static getRust(url, dataStr) {
return `extern crate reqwest;
use reqwest::header;
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -146,7 +109,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
headers.insert("Content-Type", "application/json".parse().unwrap());
let client = reqwest::blocking::Client::new();
let res = client.post("${gethttpOrigin()}/api/v1/message/send")
let res = client.post("${url}")
.headers(headers)
.body(r#"
${dataStr}
@@ -157,43 +120,37 @@ ${dataStr}
println!("{}", res);
Ok(())
}
`;
return example;
}`;
}
static getPHPString(task_id, options) {
let dataStr = ApiStrGenerate.getDataString(task_id, options);
let example = `<?php
static getPHP(url, dataStr) {
return `<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '${gethttpOrigin()}/api/v1/message/send');
curl_setopt($ch, CURLOPT_URL, '${url}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, ${JSON.stringify(dataStr).trim('\"')});
curl_setopt($ch, CURLOPT_POSTFIELDS, ${JSON.stringify(dataStr).trim('"')});
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
curl_close($ch);
`;
return example;
curl_close($ch);`;
}
static getNodeString(task_id, options) {
let dataStr = ApiStrGenerate.getDataString(task_id, options);
let example = `var request = require('request');
static getNode(url, dataStr) {
return `var request = require('request');
var headers = {
'Content-Type': 'application/json'
};
var dataString = ${JSON.stringify(dataStr).trim('\"')};
var dataString = ${JSON.stringify(dataStr).trim('"')};
var options = {
url: '${gethttpOrigin()}/api/v1/message/send',
url: '${url}',
method: 'POST',
headers: headers,
body: dataString
@@ -201,15 +158,123 @@ var options = {
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);
`;
return example;
request(options, callback);`;
}
}
export { ApiStrGenerate };
// ==================== 发信任务 API (V1) ====================
class ApiStrGenerate {
static getDataString(task_id, options) {
let data = { token: TokenEncryption.encryptHex(task_id, 71) };
data.title = 'message title';
data.text = 'Hello World!';
if (options.html) data.html = '<h1> Hello World! </h1>';
if (options.markdown) data.markdown = '**Hello World!**';
if (options.url) data.url = 'https://github.com';
if (options.at_mobiles) data.at_mobiles = ['13800138000', '13900139000'];
if (options.at_user_ids) data.at_user_ids = ['zhangsan', 'lisi'];
if (options.at_all) data.at_all = true;
return JSON.stringify(data, null, 4);
}
static getApiUrl() {
return `${gethttpOrigin()}/api/v1/message/send`;
}
static getCurlString(task_id, options) {
return CodeTemplates.getCurl(this.getApiUrl(), this.getDataString(task_id, options));
}
static getGolangString(task_id, options) {
return CodeTemplates.getGolang(this.getApiUrl(), this.getDataString(task_id, options));
}
static getPythonString(task_id, options) {
return CodeTemplates.getPython(this.getApiUrl(), this.getDataString(task_id, options));
}
static getJaveString(task_id, options) {
return CodeTemplates.getJava(this.getApiUrl(), this.getDataString(task_id, options));
}
static getRustString(task_id, options) {
return CodeTemplates.getRust(this.getApiUrl(), this.getDataString(task_id, options));
}
static getPHPString(task_id, options) {
return CodeTemplates.getPHP(this.getApiUrl(), this.getDataString(task_id, options));
}
static getNodeString(task_id, options) {
return CodeTemplates.getNode(this.getApiUrl(), this.getDataString(task_id, options));
}
}
// ==================== 模板 API (V2) ====================
class TemplateApiStrGenerate {
static getTemplateDataString(template_id, placeholders_json) {
// 解析占位符配置
let placeholders = {};
try {
const placeholdersList = JSON.parse(placeholders_json || '[]');
// 根据占位符配置生成示例值
placeholdersList.forEach(p => {
placeholders[p.key] = p.default || `mock_${p.key}`;
});
} catch (e) {
// 如果解析失败,使用默认示例
placeholders = {
'username': 'John Doe',
'email': 'john@example.com',
'phone': '13800138000'
};
}
let data = {
token: TokenEncryption.encryptHex(template_id, 71),
title: 'message title',
placeholders: placeholders
};
return JSON.stringify(data, null, 4);
}
static getApiUrl() {
return `${gethttpOrigin()}/api/v2/message/send`;
}
static getCurlString(template_id, placeholders_json) {
return CodeTemplates.getCurl(this.getApiUrl(), this.getTemplateDataString(template_id, placeholders_json));
}
static getGolangString(template_id, placeholders_json) {
return CodeTemplates.getGolang(this.getApiUrl(), this.getTemplateDataString(template_id, placeholders_json));
}
static getPythonString(template_id, placeholders_json) {
return CodeTemplates.getPython(this.getApiUrl(), this.getTemplateDataString(template_id, placeholders_json));
}
static getJavaString(template_id, placeholders_json) {
return CodeTemplates.getJava(this.getApiUrl(), this.getTemplateDataString(template_id, placeholders_json));
}
static getRustString(template_id, placeholders_json) {
return CodeTemplates.getRust(this.getApiUrl(), this.getTemplateDataString(template_id, placeholders_json));
}
static getPHPString(template_id, placeholders_json) {
return CodeTemplates.getPHP(this.getApiUrl(), this.getTemplateDataString(template_id, placeholders_json));
}
static getNodeString(template_id, placeholders_json) {
return CodeTemplates.getNode(this.getApiUrl(), this.getTemplateDataString(template_id, placeholders_json));
}
}
export { ApiStrGenerate, TemplateApiStrGenerate };