Przeglądaj źródła

绑定用户逻辑

master
leiyun 2 miesięcy temu
rodzic
commit
ef088510b2
2 zmienionych plików z 51 dodań i 20 usunięć
  1. +21
    -0
      src/controller/admin/patient.js
  2. +30
    -20
      src/controller/mp.js

+ 21
- 0
src/controller/admin/patient.js Wyświetl plik

@@ -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,


+ 30
- 20
src/controller/mp.js Wyświetl plik

@@ -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) {


Ładowanie…
Anuluj
Zapisz