const { ObjectId } = require("mongodb"); const validator = require('../../utils/validator.js'); const Sm4Util = require('../../utils/sm4-util.js'); let db = ""; module.exports = async (item, mongodb) => { db = mongodb; switch (item.type) { case "addFamilyProfile": return await addFamilyProfile(item); case "getFamilyProfile": return await getFamilyProfile(item) case "updateFamilyProfile": return await updateFamilyProfile(item); case "sm4Encrypt": return sm4Encrypt(item); } }; function sm4Encrypt(data) { if (typeof data.text == 'string' && data.text.length > 0) { return { success: true, text: Sm4Util.encryptDataForSm3(data.text) }; } if (Array.isArray(data.textGroup) && data.textGroup.length) { return { success: true, textGroup: data.textGroup.map(txt => { if (typeof txt === 'string' && txt.trim().length > 0) { return Sm4Util.encryptDataForSm3(txt.trim()); } return ''; }) }; } return { success: false, message: '加密参数无效' }; } function getValidData({ name, socialno, tel, address, userId, psnToken }) { const list = [ { value: name, label: '姓名', min: 2, max: 20, prop: 'name' }, { value: socialno, label: '证件号', min: 15, max: 20, prop: 'socialno' }, { value: tel, label: '手机号', min: 11, max: 12, prop: 'tel' }, { value: address, label: '地址', min: 1, max: 100, prop: 'address' }, { value: userId, label: '用户ID', min: 10, max: 24, prop: 'userId' }, { value: psnToken, label: '医保授权码', min: 1, max: 100, prop: 'psnToken' }, ] const data = {}; return new Promise((resolve, reject) => { for (let item of list) { const { value, label, min, max, prop } = item; const [valid, valOrMsg] = validator.verifyString(value, label, min, max); if (valid) { data[prop] = valOrMsg; } else { reject({ message: valOrMsg }); return; } } if (!validator.isChinaId(data.socialno)) { reject({ message: '证件号无效' }); return } if (!validator.isMobile(data.tel)) { reject({ message: '手机号无效' }); return } resolve(data) }) } async function addFamilyProfile(ctx) { const zytHis = require("../zyt-his"); try { const data = await getValidData(ctx); const total = await db.collection("family-profile").countDocuments({ userId: data.userId }); if (total > 10) return { success: false, message: '亲情档案已达上限' }; const anotherIdNo = Sm4Util.encryptDataForSm3(data.socialno); const sameCount = await db.collection("family-profile").countDocuments({ anotherIdNo, userId: data.userId }); if (sameCount > 0) { return { success: false, message: '当前账户已经绑定该亲情档案' } } const queryRes = await zytHis({ type: 'getHisCustomer', idCard: data.socialno }); if (!queryRes.success) return { success: false, message: queryRes.message || '查询亲情档案失败' } let list = Array.isArray(queryRes.list) ? queryRes.list : []; let patient = list.find(item => item.socialno === data.socialno); // 如果已经建过档案 比较档案姓名 if (patient && patient.name !== data.name) { return { success: false, message: '亲情档案姓名与his系统不一致,请核对后再试' } } // 如果没有建过档案 则新增档案 if (!patient || patient.isyb !== "1") { const params = { name: data.name, idCard: data.socialno, mobile: data.tel, address: data.address, psnToken: data.psnToken, } const addRes = await zytHis({ type: 'addHisCustomer', ...params }); if (!addRes.success) return { success: false, message: addRes.message || '新增亲情档案失败(his医保建档失败)' } list = Array.isArray(addRes.list) ? addRes.list : []; patient = list.find(item => item.socialno === data.socialno); } if (!patient || patient.isyb !== "1") { return { success: false, message: '新增亲情档案失败,未查询到档案信息或者医保建档失败' } } const res = await db.collection("family-profile").insertOne({ userId: data.userId, name: patient.name, tel: data.tel, patientid: patient.patientid, blhno: patient.blhno, address: data.address, anotherIdNo, anotherName: Sm4Util.encryptDataForSm3(patient.name), createTime: Date.now() }) if (res.insertedId) { return { success: true, message: '新增亲情档案成功', id: res.insertedId } } return { success: false, message: '新增亲情档案失败' } } catch (e) { return { success: false, message: e.message || '新增亲情档案失败' } } } async function getFamilyProfile(ctx) { const { userId } = ctx; if (typeof userId !== 'string' || userId.length === '') { return { success: false, message: '用户ID无效' } } try { const list = await db.collection("family-profile").find({ userId: userId.trim() }).toArray(); list.forEach(item => { item.socialno = Sm4Util.decryptDataForSm3(item.anotherIdNo); }) return { success: true, list, message: '查询亲情档案成功' } } catch (e) { return { success: false, message: e.message || '查询亲情档案失败' } } } async function updateFamilyProfile(ctx) { const { id, userId, tel, address } = ctx; if (typeof id !== 'string' || !ObjectId.isValid(id)) { return { success: false, message: '亲情档案ID无效' } } if (typeof userId !== 'string' || userId.length === '') { return { success: false, message: '用户ID无效' } } if (typeof tel !== 'string' || !validator.isMobile(tel)) { return { success: false, message: '手机号无效' } } if (typeof address !== 'string' || address.trim().length == 0) { return { success: false, message: '地址无效' } } if (address.length > 100) { return { success: false, message: '地址长度不能超过100个字符' } } try { const count = await db.collection("family-profile").countDocuments({ _id: new ObjectId(id), userId: userId.trim() }); if (count === 0) { return { success: false, message: '亲情档案不存在或无权限修改' } } const res = await db.collection("family-profile").updateOne( { _id: new ObjectId(id), userId: userId.trim() }, { $set: { tel, address, updateTime: Date.now() } } ); if (res.modifiedCount > 0) { return { success: true, message: '亲情档案更新成功' } } return { success: false, message: res || '亲情档案更新失败' } } catch (e) { return { success: false, message: e.message || '亲情档案更新失败' } } }