feat: 发送消息优化
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
'use strict';
|
||||
|
||||
const http = require('node:http');
|
||||
|
||||
const host = '127.0.0.1';
|
||||
const port = 3001;
|
||||
const tokenPath = '/msg/getThirdAPIToken';
|
||||
const sendPath = '/message/sendMessageApi';
|
||||
const maxBodySize = 1024 * 1024;
|
||||
|
||||
function sendJson(response, status, body) {
|
||||
response.writeHead(status, { 'Content-Type': 'application/json; charset=utf-8' });
|
||||
response.end(JSON.stringify(body));
|
||||
}
|
||||
|
||||
function logRequest(method, path, params) {
|
||||
for (const field of ['token', 'appPassword']) {
|
||||
if (Object.hasOwn(params, field)) {
|
||||
params[field] = '[已脱敏]';
|
||||
}
|
||||
}
|
||||
|
||||
console.log(JSON.stringify({
|
||||
请求接口: path,
|
||||
请求方式: method,
|
||||
请求参数: params
|
||||
}, null, 2));
|
||||
}
|
||||
|
||||
async function handleRequest(request, response) {
|
||||
const { pathname, searchParams } = new URL(request.url, 'http://127.0.0.1');
|
||||
if (pathname === tokenPath && request.method === 'GET') {
|
||||
logRequest(request.method, pathname, Object.fromEntries(searchParams));
|
||||
sendJson(response, 200, {
|
||||
rspCode: 0,
|
||||
rspMsg: 'success',
|
||||
resultData: { token: 'mock-zjnu-msg-token' }
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (pathname !== sendPath || request.method !== 'POST') {
|
||||
sendJson(response, 404, { rspCode: 404, rspMsg: '接口不存在' });
|
||||
return;
|
||||
}
|
||||
|
||||
if (request.headers['content-type']?.split(';')[0].trim() !== 'application/x-www-form-urlencoded') {
|
||||
sendJson(response, 415, { rspCode: 415, rspMsg: '请求体必须为 URL 编码表单' });
|
||||
return;
|
||||
}
|
||||
|
||||
const chunks = [];
|
||||
let bodySize = 0;
|
||||
try {
|
||||
for await (const chunk of request) {
|
||||
bodySize += chunk.length;
|
||||
if (bodySize <= maxBodySize) {
|
||||
chunks.push(chunk);
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
sendJson(response, 400, { rspCode: 400, rspMsg: '无法读取请求体' });
|
||||
return;
|
||||
}
|
||||
|
||||
if (bodySize > maxBodySize) {
|
||||
sendJson(response, 413, { rspCode: 413, rspMsg: '请求体超过 1 MB' });
|
||||
return;
|
||||
}
|
||||
|
||||
const form = new URLSearchParams(Buffer.concat(chunks).toString('utf8'));
|
||||
let receivers;
|
||||
try {
|
||||
receivers = JSON.parse(form.get('receivers'));
|
||||
} catch {
|
||||
logRequest(request.method, pathname, Object.fromEntries(form));
|
||||
sendJson(response, 400, { rspCode: 400, rspMsg: 'receivers 必须为 JSON 数组' });
|
||||
return;
|
||||
}
|
||||
|
||||
logRequest(request.method, pathname, {
|
||||
...Object.fromEntries(form),
|
||||
...(form.has('receivers') ? { receivers } : {})
|
||||
});
|
||||
|
||||
const requiredFields = ['token', 'msgContent', 'sendChannel', 'sendMode'];
|
||||
if (requiredFields.some(field => !form.get(field)?.trim())
|
||||
|| !Array.isArray(receivers) || receivers.length === 0) {
|
||||
sendJson(response, 400, { rspCode: 400, rspMsg: '缺少发送参数或接收人' });
|
||||
return;
|
||||
}
|
||||
|
||||
sendJson(response, 200, { rspCode: 0, rspMsg: 'success' });
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
http.createServer(handleRequest).listen(port, host, () => {
|
||||
console.log('模拟浙师大消息平台 http://' + host + ':' + port);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { handleRequest };
|
||||
Reference in New Issue
Block a user