Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4567ad5728 |
@@ -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 };
|
||||
Reference in New Issue
Block a user