From ef088510b2528fb2f399134a58e97b61230bbc22 Mon Sep 17 00:00:00 2001 From: leiyun Date: Sat, 28 Mar 2026 16:02:33 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=91=E5=AE=9A=E7=94=A8=E6=88=B7=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller/admin/patient.js | 21 ++++++++++++++ src/controller/mp.js | 50 ++++++++++++++++++++------------- 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/src/controller/admin/patient.js b/src/controller/admin/patient.js index 249ce83..e404b44 100644 --- a/src/controller/admin/patient.js +++ b/src/controller/admin/patient.js @@ -147,6 +147,21 @@ module.exports = class extends Base { } const model = this.model('patient'); + + // 唯一性校验 + const existByIdCardAndPhone = await model.where({ id_card, phone, is_deleted: 0 }).find(); + if (!think.isEmpty(existByIdCardAndPhone)) { + return this.fail('该患者已存在(身份证+手机号匹配)'); + } + const existByPhone = await model.where({ phone, is_deleted: 0 }).find(); + if (!think.isEmpty(existByPhone)) { + return this.fail('该手机号已被其他患者使用'); + } + const existByIdCard = await model.where({ id_card, is_deleted: 0 }).find(); + if (!think.isEmpty(existByIdCard)) { + return this.fail('该身份证号已被其他患者使用'); + } + const patientNo = model.generatePatientNo(); const id = await model.add({ @@ -315,6 +330,12 @@ module.exports = class extends Base { const patient = await this.model('patient').where({ id, is_deleted: 0 }).find(); if (think.isEmpty(patient)) return this.fail('患者不存在'); + // 唯一性校验(排除自身) + const existByPhone = await this.model('patient').where({ phone, id: ['!=', id], is_deleted: 0 }).find(); + if (!think.isEmpty(existByPhone)) return this.fail('该手机号已被其他患者使用'); + const existByIdCard = await this.model('patient').where({ id_card, id: ['!=', id], is_deleted: 0 }).find(); + if (!think.isEmpty(existByIdCard)) return this.fail('该身份证号已被其他患者使用'); + await this.model('patient').where({ id }).update({ name, phone, id_card, gender, birth_date, province_code, city_code, district_code, diff --git a/src/controller/mp.js b/src/controller/mp.js index 56cd4f9..4c7f0b9 100644 --- a/src/controller/mp.js +++ b/src/controller/mp.js @@ -242,29 +242,36 @@ module.exports = class extends Base { const userModel = this.model('wechat_user'); const currentUser = await userModel.where({ id: mpUser.id }).find(); - // 查找已存在的患者(排除当前用户已绑定的) - const existCondition = { id_card: idCard, is_deleted: 0 }; - if (currentUser.patient_id) existCondition.id = ['!=', currentUser.patient_id]; - const existPatient = await patientModel.where(existCondition).find(); - - if (!think.isEmpty(existPatient)) { - // 手机号+身份证都匹配 → 可绑定已有患者 - if (existPatient.phone === mobile) { + // 排除当前用户已绑定的 patient + const excludeId = currentUser.patient_id || 0; + + // 查找身份证是否已存在 + const idCardCondition = { id_card: idCard, is_deleted: 0 }; + if (excludeId) idCardCondition.id = ['!=', excludeId]; + const existByIdCard = await patientModel.where(idCardCondition).find(); + + // 查找手机号是否已存在 + const phoneCondition = { phone: mobile, is_deleted: 0 }; + if (excludeId) phoneCondition.id = ['!=', excludeId]; + const existByPhone = await patientModel.where(phoneCondition).find(); + + if (!think.isEmpty(existByIdCard)) { + if (existByIdCard.phone === mobile) { + // 手机号+身份证都匹配 → 可绑定已有患者 if (!confirmBind) { - // 首次提交,返回 1010 让前端确认 - const maskedName = existPatient.name.length > 1 - ? existPatient.name[0] + '*'.repeat(existPatient.name.length - 1) - : existPatient.name; - const maskedPhone = '****' + existPatient.phone.slice(-4); + const maskedName = existByIdCard.name.length > 1 + ? existByIdCard.name[0] + '*'.repeat(existByIdCard.name.length - 1) + : existByIdCard.name; + const maskedPhone = '****' + existByIdCard.phone.slice(-4); return this.json({ code: 1010, data: { patientName: maskedName, patientPhone: maskedPhone }, msg: '该用户信息已存在' }); } - // 用户确认绑定:检查该 patient 是否已被其他真实微信用户绑定(排除 H5 登录渠道) + // 用户确认绑定 const boundUser = await userModel.where({ - patient_id: existPatient.id, + patient_id: existByIdCard.id, id: ['!=', mpUser.id], open_id: ['NOT LIKE', 'h5_%'], status: 1 @@ -272,11 +279,9 @@ module.exports = class extends Base { if (!think.isEmpty(boundUser)) { return this.json({ code: 1, msg: '该患者信息已被其他微信账号绑定' }); } - // 绑定已有 patient 到当前微信用户 const now = think.datetime(new Date()); - await userModel.where({ id: mpUser.id }).update({ patient_id: existPatient.id, update_time: now }); - // 更新认证信息 - await patientModel.where({ id: existPatient.id }).update({ + await userModel.where({ id: mpUser.id }).update({ patient_id: existByIdCard.id, update_time: now }); + await patientModel.where({ id: existByIdCard.id }).update({ name: realName, phone: mobile, id_card_type: cardTypeInt, id_card_front: idCardFront || '', id_card_back: idCardBack || '', photo: photo || '', gender: gender || '', birth_date: birthday || null, @@ -285,10 +290,15 @@ module.exports = class extends Base { }); return this.json({ code: 0, data: {}, msg: '实名认证成功' }); } - // 身份证匹配但手机号不同 → 真正的冲突 + // 身份证存在但手机号不同 return this.json({ code: 1, msg: '该证件号已被其他用户认证' }); } + // 身份证不存在,但手机号已被其他患者使用 + if (!think.isEmpty(existByPhone)) { + return this.json({ code: 1, msg: '该手机号已被其他患者使用' }); + } + if (cardTypeInt === 1 || cardTypeInt === 3) { const faceidConfig = require('../config/faceid.js'); if (faceidConfig.enabled) {