import cuid from "cuid"; import _ from "lodash"; import { getDb } from "funjia-server/lib/funjia-server.mjs"; import { getTable, pickFilters, matchRow, listResponse, omitSensitive } from "../helpers.mjs"; /** * 员工(登录账号)路由。 * * ## 改造前的缺陷 * * 1. **列表接口在员工表为空时抛错** * `db.data?.employee?.filter(...)` → `undefined`,随后 `data.length` 直接 500。 * * 2. **明文密码出接口** * `/api/employee/list` 与 `/api/employee/login` 都返回完整记录,`pwd` 字段 * (示例库里就是 `123456`)明文透出到浏览器。改为统一剔除。 * * 3. **`console.log` 打印登录参数** * 原来会把 `ctx.request.body`(含用户名、密码)原样写进日志。 * * 4. **`upd` 允许请求体覆盖主键 id**,`delete` 用逐条 splice(ids 中的无效 id * 被静默忽略,且返回值不体现实际删除条数)。 * * 5. **登录失败不区分原因**这件事要保持——反过来也不该把「用户不存在」和 * 「密码错误」区分开,避免账号枚举。 */ const getEmployee = (router) => { const tableName = "employee"; router.post("/api/employee/list", async (ctx) => { ctx.type = "application/json"; const { table } = await getTable(getDb, tableName); const body = ctx.request.body || {}; const data = table.filter((item) => matchRow(item, pickFilters(body))); ctx.body = { ...listResponse(data, body), data: listResponse(data, body).data.map((row) => omitSensitive(row)), }; }); router.get("/api/employee", async (ctx) => { ctx.type = "application/json"; const { table } = await getTable(getDb, tableName); const detail = table.find((item) => item.id == ctx.request.query.id); ctx.body = { code: 0, data: detail ? omitSensitive(detail) : {} }; }); router.post("/api/employee/add", async (ctx) => { ctx.type = "application/json"; const { db, table } = await getTable(getDb, tableName); const data = ctx.request.body || {}; if (table.some((item) => item.userName === data.userName)) { ctx.body = { code: 1, msg: "用户名已存在" }; return; } const { id, createTime, ...rest } = data; void id; void createTime; table.push({ ...rest, id: cuid(), createTime: new Date() }); await db.write(); ctx.body = { code: 0 }; }); router.post("/api/employee/upd", async (ctx) => { ctx.type = "application/json"; const { db, table } = await getTable(getDb, tableName); const { id, createTime, ...patch } = ctx.request.body || {}; // id / createTime 由服务端维护,不接受请求体覆盖 void id; void createTime; const current = table.find((item) => item.id == id); if (!current) { ctx.body = { code: 1, msg: "员工不存在" }; return; } _.merge(current, patch); await db.write(); ctx.body = { code: 0 }; }); router.post("/api/employee/delete", async (ctx) => { ctx.type = "application/json"; const { db, table } = await getTable(getDb, tableName); const ids = (ctx.request.body?.ids || []).map(String); if (ids.length === 0) { ctx.body = { code: 1, msg: "未指定要删除的记录" }; return; } const before = table.length; const rest = table.filter((item) => !ids.includes(String(item.id))); table.length = 0; table.push(...rest); await db.write(); ctx.body = { code: 0, data: { deleted: before - table.length } }; }); router.post("/api/employee/login", async (ctx) => { ctx.type = "application/json"; const { table } = await getTable(getDb, tableName); const { userName, pwd } = ctx.request.body || {}; const detail = table.find((item) => item.userName === userName && item.pwd === pwd); if (!detail) { // 不区分「用户不存在」与「密码错误」,避免账号枚举 ctx.body = { code: 1, msg: "用户名或密码错误" }; return; } ctx.body = { code: 0, data: omitSensitive(detail) }; }); }; export default getEmployee;