From 4567ad57287ffaa5c037d6af76c7567bdfbaa931 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=8C=E5=85=88=E8=88=9C?= <2499071264@qq.com> Date: Fri, 13 Nov 2026 18:45:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B5=8B=E8=AF=95=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/mock-data-center.js | 121 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 tools/mock-data-center.js diff --git a/tools/mock-data-center.js b/tools/mock-data-center.js new file mode 100644 index 0000000..65dd61c --- /dev/null +++ b/tools/mock-data-center.js @@ -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 };