feat: 发送消息优化
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
'use strict';
|
||||
|
||||
const http = require('node:http');
|
||||
|
||||
const host = '127.0.0.1';
|
||||
const port = 3000;
|
||||
const teacherPath = '/do/api/call/lxyxt_jzgjbxx';
|
||||
const unitPath = '/do/api/call/lxyxt_dwxx';
|
||||
const defaultPageSize = 20;
|
||||
const pad = (number, length) => String(number).padStart(length, '0');
|
||||
|
||||
const colleges = ['文学院', '数学学院', '计算机学院', '外国语学院', '教育学院'].map((name, index) => ({
|
||||
DWDM: 'MOCK-U' + pad(index + 1, 2),
|
||||
DWMC: '模拟' + name,
|
||||
DWJC: name,
|
||||
DWCC: 1,
|
||||
LSDWDM: '0',
|
||||
DWLBDM: '01'
|
||||
}));
|
||||
|
||||
const departments = colleges.map((college, index) => ({
|
||||
DWDM: 'MOCK-U' + pad(index + 6, 2),
|
||||
DWMC: college.DWMC + '教学系',
|
||||
DWJC: college.DWJC + '教学系',
|
||||
DWCC: 2,
|
||||
LSDWDM: college.DWDM,
|
||||
DWLBDM: '01'
|
||||
}));
|
||||
|
||||
const units = [...colleges, ...departments];
|
||||
const teachers = Array.from({ length: 100 }, (_, index) => {
|
||||
const sequence = pad(index + 1, 3);
|
||||
const unit = units[index % units.length];
|
||||
const doctorate = index % 3 === 0;
|
||||
return {
|
||||
ZGH: 'MOCK-T' + sequence,
|
||||
XM: '模拟教师' + sequence,
|
||||
XBMC: Math.floor(index / units.length) % 2 === 0 ? '女' : '男',
|
||||
CSRQ: (1975 + index % 23) + '-' + pad(index % 12 + 1, 2) + '-' + pad(index % 28 + 1, 2),
|
||||
LXRQ: (2010 + index % 15) + '-' + pad(index % 12 + 1, 2) + '-01',
|
||||
DQZTMC: '在岗',
|
||||
YRFSDM: '教职工',
|
||||
SZDWDM: unit.DWDM,
|
||||
SZDW: unit.DWMC,
|
||||
MZDM: '01',
|
||||
ZGXLMC: doctorate ? '博士研究生' : '硕士研究生',
|
||||
ZGXWMC: doctorate ? '博士' : '硕士',
|
||||
ZYJSGWLBDMMC: '专业技术岗',
|
||||
ZYJSZWDMMC: ['讲师', '副教授', '教授'][index % 3],
|
||||
SFSJT: false
|
||||
};
|
||||
});
|
||||
|
||||
function sendJson(response, status, body) {
|
||||
response.writeHead(status, { 'Content-Type': 'application/json; charset=utf-8' });
|
||||
response.end(JSON.stringify(body));
|
||||
}
|
||||
|
||||
function positiveInteger(value) {
|
||||
const number = Number(value);
|
||||
return (typeof value === 'number' || typeof value === 'string') && Number.isSafeInteger(number) && number > 0;
|
||||
}
|
||||
|
||||
async function handleRequest(request, response) {
|
||||
const path = request.url.split('?')[0];
|
||||
if (path !== teacherPath && path !== unitPath) {
|
||||
sendJson(response, 404, { returnStatus: 0, errorMsg: '接口不存在' });
|
||||
return;
|
||||
}
|
||||
if (request.method !== 'POST') {
|
||||
response.setHeader('Allow', 'POST');
|
||||
sendJson(response, 405, { returnStatus: 0, errorMsg: '请使用 POST' });
|
||||
return;
|
||||
}
|
||||
|
||||
let params;
|
||||
try {
|
||||
let body = '';
|
||||
request.setEncoding('utf8');
|
||||
for await (const chunk of request) {
|
||||
body += chunk;
|
||||
}
|
||||
params = body.trim() ? JSON.parse(body) : {};
|
||||
if (!params || typeof params !== 'object' || Array.isArray(params)) {
|
||||
throw new TypeError('请求体必须是 JSON 对象');
|
||||
}
|
||||
} catch {
|
||||
sendJson(response, 400, { returnStatus: 0, errorMsg: '请求体必须是 JSON 对象' });
|
||||
return;
|
||||
}
|
||||
|
||||
const pageNum = params.pageNum ?? 1;
|
||||
const pageSize = params.pageSize ?? defaultPageSize;
|
||||
if (!positiveInteger(pageNum) || !positiveInteger(pageSize)) {
|
||||
sendJson(response, 400, { returnStatus: 0, errorMsg: 'pageNum 和 pageSize 必须是正整数' });
|
||||
return;
|
||||
}
|
||||
|
||||
const jobNumber = String(params.zgh ?? '').trim();
|
||||
const records = path === unitPath ? units
|
||||
: jobNumber ? teachers.filter(teacher => teacher.ZGH === jobNumber) : teachers;
|
||||
const start = (Number(pageNum) - 1) * Number(pageSize);
|
||||
sendJson(response, 200, {
|
||||
returnStatus: 1,
|
||||
pageInfo: {
|
||||
pageNum: Number(pageNum),
|
||||
pageSize: Number(pageSize),
|
||||
pages: Math.ceil(records.length / Number(pageSize)),
|
||||
total: records.length
|
||||
},
|
||||
dataSet: records.slice(start, start + Number(pageSize))
|
||||
});
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
http.createServer(handleRequest).listen(port, host, () => {
|
||||
console.log('模拟数据接口 http://' + host + ':' + port);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { handleRequest };
|
||||
@@ -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