- 新增 attendant_application 表与 5 个接口:提交/我的申请/审核列表/ 材料大图/审批,/mine 注册在 /:id 之前 - 提交侧按员工档案岗位分支:在职陪诊师与管理员及其他岗位 409 拒绝, 转岗由管理员后台显式操作;停用陪诊师可重新申请 - 审批两步走不依赖事务:条件更新抢占 pending,ensureEmployee 单条 条件 upsert 幂等建档(只放行无档案或已有 attendant 档案), 失败靠 approvedEmployeeId 为空续跑 - 岗位冲突时申请单条件回滚为 pending,不卡死在"已通过但未建档" - employee.userId 加 partial unique 索引并移除 employeeNo 默认值, 附迁移与存量校验脚本;员工管理端绑定账号查重与 userIdSource 标记 - 证件照复用 parseImage(单张 3MB),入驻前缀放宽请求体上限; 索引创建失败改为记日志,避免启动即崩
165 lines
6.1 KiB
JavaScript
165 lines
6.1 KiB
JavaScript
import ResponseUtil from "../utils/responseUtil.js";
|
||
import { DBModel } from "../models/index.js";
|
||
|
||
const EDUCATION_OPTIONS = ["初中及以下", "高中/中专", "大专", "本科", "硕士及以上"];
|
||
const MOBILE_REGEX = /^1[3-9]\d{9}$/;
|
||
const ID_NUMBER_REGEX = /^\d{17}[\dXx]$/;
|
||
const EXAM_MONTH_REGEX = /^2026-(0[1-9]|1[0-2])$/;
|
||
const MAX_IMAGE_BYTES = 5 * 1024 * 1024;
|
||
const DUPLICATE_WINDOW_MS = 24 * 60 * 60 * 1000;
|
||
const STATUS_VALUES = ["pending", "contacted", "enrolled", "cancelled"];
|
||
const MAX_PAGE_SIZE = 200;
|
||
|
||
const COURSE = {
|
||
name: "医疗陪诊顾问(高级)",
|
||
issuer: "国家开放大学培训中心",
|
||
};
|
||
|
||
class HandlerTrainingApplication {
|
||
// 报名回执不携带身份证号与图片,避免在客户端重复暴露敏感信息
|
||
static toPublic(doc) {
|
||
if (!doc) return null;
|
||
const year = Number(doc.exam?.year) || 2026;
|
||
const month = Number(doc.exam?.month) || 0;
|
||
return {
|
||
id: doc._id,
|
||
applicant: {
|
||
name: doc.applicant?.name || "",
|
||
mobile: doc.applicant?.mobile || "",
|
||
education: doc.applicant?.education || "",
|
||
},
|
||
examMonth: month ? `${year}-${String(month).padStart(2, "0")}` : "",
|
||
course: {
|
||
name: doc.course?.name || COURSE.name,
|
||
issuer: doc.course?.issuer || COURSE.issuer,
|
||
},
|
||
status: doc.status || "pending",
|
||
createtime: doc.meta?.createtime || null,
|
||
};
|
||
}
|
||
|
||
// 只接受 jpeg/png 的 data URL;同时检查文件签名和原始大小,
|
||
// 防止把任意 base64 文本当作报名材料写入数据库
|
||
static parseImage(input, label, maxBytes = MAX_IMAGE_BYTES) {
|
||
const raw = String(input || "").trim();
|
||
const match = raw.match(/^data:(image\/(?:jpeg|png));base64,([A-Za-z0-9+/]+={0,2})$/);
|
||
if (!match) return { error: `请上传${label}(JPG/PNG)` };
|
||
|
||
const mime = match[1];
|
||
const buffer = Buffer.from(match[2], "base64");
|
||
if (!buffer.length || buffer.length > maxBytes) {
|
||
return { error: `${label}不能超过 ${Math.round(maxBytes / 1024 / 1024)}MB` };
|
||
}
|
||
|
||
const isJpeg = buffer.length > 2 && buffer[0] === 0xff && buffer[1] === 0xd8;
|
||
const isPng = buffer.length > 8 && buffer.subarray(0, 8).equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]));
|
||
if ((mime === "image/jpeg" && !isJpeg) || (mime === "image/png" && !isPng)) {
|
||
return { error: `${label}格式不正确` };
|
||
}
|
||
|
||
return { value: { data: raw, mime } };
|
||
}
|
||
|
||
static parsePayload(body = {}) {
|
||
const applicant = body.applicant || {};
|
||
const name = String(applicant.name || "").trim();
|
||
const idNumber = String(applicant.idNumber || "").trim().toUpperCase();
|
||
const mobile = String(applicant.mobile || "").trim();
|
||
const education = String(applicant.education || "").trim();
|
||
const examMonth = String(body.examMonth || "").trim();
|
||
|
||
if (!name) return { error: "请填写姓名" };
|
||
if (name.length > 20) return { error: "姓名不能超过 20 个字" };
|
||
if (!ID_NUMBER_REGEX.test(idNumber)) return { error: "请填写 18 位身份证号,末位可为 X" };
|
||
if (!MOBILE_REGEX.test(mobile)) return { error: "请填写正确的 11 位手机号" };
|
||
if (!EDUCATION_OPTIONS.includes(education)) return { error: "请选择学历" };
|
||
if (!EXAM_MONTH_REGEX.test(examMonth)) return { error: "请选择 2026 年的考试月份" };
|
||
|
||
const photo = HandlerTrainingApplication.parseImage(body.materials?.photo, "2寸蓝底照片");
|
||
if (photo.error) return photo;
|
||
const idCard = HandlerTrainingApplication.parseImage(body.materials?.idCard, "身份证电子版");
|
||
if (idCard.error) return idCard;
|
||
|
||
return {
|
||
value: {
|
||
applicant: { name, idNumber, mobile, education },
|
||
exam: { year: 2026, month: Number(examMonth.slice(5)) },
|
||
materials: { photo: photo.value, idCard: idCard.value },
|
||
course: { ...COURSE },
|
||
},
|
||
};
|
||
}
|
||
|
||
async createApplication(ctx) {
|
||
try {
|
||
const userId = ctx.state.user?._id;
|
||
if (!userId) return ResponseUtil.unauthorized(ctx, "登录已失效");
|
||
|
||
const result = HandlerTrainingApplication.parsePayload(ctx.request.body);
|
||
if (result.error) return ResponseUtil.badRequest(ctx, result.error);
|
||
|
||
const existing = await DBModel.TrainingApplication.findRecentActiveByUser(
|
||
userId,
|
||
Date.now() - DUPLICATE_WINDOW_MS
|
||
);
|
||
if (existing) {
|
||
return ResponseUtil.success(
|
||
ctx,
|
||
{ application: HandlerTrainingApplication.toPublic(existing), duplicated: true },
|
||
"你已提交过报名,客服会尽快联系你"
|
||
);
|
||
}
|
||
|
||
const application = await DBModel.TrainingApplication.createApplication({
|
||
userId,
|
||
...result.value,
|
||
});
|
||
|
||
return ResponseUtil.success(
|
||
ctx,
|
||
{ application: HandlerTrainingApplication.toPublic(application) },
|
||
"报名提交成功"
|
||
);
|
||
} catch (err) {
|
||
return ResponseUtil.internalError(ctx, err);
|
||
}
|
||
}
|
||
|
||
async getMyApplication(ctx) {
|
||
try {
|
||
const userId = ctx.state.user?._id;
|
||
if (!userId) return ResponseUtil.unauthorized(ctx, "登录已失效");
|
||
|
||
const application = await DBModel.TrainingApplication.findLatestByUser(userId);
|
||
return ResponseUtil.success(
|
||
ctx,
|
||
{ application: HandlerTrainingApplication.toPublic(application) },
|
||
"查询成功"
|
||
);
|
||
} catch (err) {
|
||
return ResponseUtil.internalError(ctx, err);
|
||
}
|
||
}
|
||
|
||
async getApplications(ctx) {
|
||
try {
|
||
const { status } = ctx.request.query;
|
||
if (status && !STATUS_VALUES.includes(status)) {
|
||
return ResponseUtil.badRequest(ctx, "报名状态筛选值不正确");
|
||
}
|
||
|
||
const page = Math.max(parseInt(ctx.request.query.page, 10) || 1, 1);
|
||
const pageSize = Math.min(
|
||
Math.max(parseInt(ctx.request.query.pageSize, 10) || 20, 1),
|
||
MAX_PAGE_SIZE
|
||
);
|
||
const result = await DBModel.TrainingApplication.findApplicationPage({ page, pageSize, status });
|
||
return ResponseUtil.success(ctx, { ...result, page, pageSize }, "查询成功");
|
||
} catch (err) {
|
||
return ResponseUtil.internalError(ctx, err);
|
||
}
|
||
}
|
||
}
|
||
|
||
export { HandlerTrainingApplication };
|