chenyc
2024-10-22 582298836cc8ff78ad2f726106babc9c36264456
app.js
@@ -3,16 +3,22 @@
const mysql = require('mysql2');
const fs = require('fs').promises;
const path = require('path');
const moment = require('moment')
// 转成能识别的url
const ViapiUtil = require('@alicloud/viapi-utils');
const facebody20191230 = require('@alicloud/facebody20191230');
const OpenApi = require('@alicloud/openapi-client');
const Util = require('@alicloud/tea-util');
const Tea = require('@alicloud/tea-typescript');
const app = express();
const port = 3000;
// 设置 JSON 请求体的最大大小为 5MB
app.use(express.json({ limit: '5mb' }));
// 设置 URL 编码请求体的最大大小为 5MB
app.use(express.urlencoded({ extended: true, limit: '5mb' }));
// 使用 body-parser 中间件解析 JSON 请求体
app.use(bodyParser.json());
@@ -39,7 +45,9 @@
    config.endpoint = `facebody.cn-shanghai.aliyuncs.com`;
    return new facebody20191230.default(config);
}
app.get('/',async(req,res)=>{
    res.send('设备交换患者信息');
})
// 定义 /webuser 路由处理器  用患者code 换信息
app.post('/webuser', async (req, res) => {
    const mode=req.body
@@ -63,7 +71,7 @@
                        userNum: user.patient_no,
                        name: user.patient_name,
                        sex: user.patient_gender === 0 ? '男' : '女',
                        age: user.patient_age_2,
                        age: getAgeFromIdCard(user.patient_identity_no),
                        headimgurl: user.patient_avatar_icon,
                        imgBaseData: '',
                        remark: ''
@@ -102,7 +110,7 @@
        const ossurl = await base64toFile(content, deviceID)
        console.log('得到oss图片路径', ossurl)
        let searchFaceRequest = new facebody20191230.SearchFaceRequest({
            dbName: 'Face_emeishan',
            dbName: 'Face_systemtest',
            imageUrl: ossurl,
            limit: 1,
        });
@@ -136,10 +144,10 @@
                                userNum: user.patient_no,
                                name: user.patient_name,
                                sex: user.patient_gender === 0 ? '男' : '女',
                                age: user.patient_age_2,
                                age: getAgeFromIdCard(user.patient_identity_no),
                                headimgurl: user.patient_avatar_icon,
                                imgBaseData: '',
                                remark: ''
                                remark: '患者人脸识别111'
                            },
                            dbinfo: {}, // 如果没有需要更新的数据库信息,可以保持空
                            msg: "success"
@@ -178,6 +186,82 @@
})
// 定义设备数据回传接口
// 定义数据回传接口
app.post('/webResults',async (req, res) =>{
    const action = req.body.action;
    const deviceID = req.body.deviceID;
    const mode=req.body
    logHttps(mode)
    try {
        const datas=mode.datas
        datas.forEach(async element => {
            await installData(element)
        });
        response = {
            retCode: "1",
            msg: "成功"
        };
        res.json(response)
    }catch(err){
        res.status(400).json({ retCode: "-1", msg: "Invalid request parameters" });
    }
})
// 定义北京悦琦创通科技有限公司VBP-10系列HTTP通信协议
/**
 * patientId={patientId}
 */
app.post('/getpatientInfo',async(req,res)=>{
    // 使用req.query访问查询字符串参数
  const patientId = req.query.patientid;
  const mode={"action":'getpatientInfo',"patientId":patientId}
  logHttps(mode)
  if(patientId){
    // 输出传入的patientid值
    console.log('Patient ID:',patientId)
    let query = `SELECT * FROM patient_info WHERE code = '${patientId}'`
    try {
        const [rows] = await pool.promise().query(query);
        if (rows.length > 0) {
            const user = rows[0];
            const response = {
                retCode: "1",
                result: {
                    patientId: user.code,
                    fullName: user.patient_name,
                    gender: user.patient_gender,
                    age: getAgeFromIdCard(user.patient_identity_no),
                    // phoneNum:user.
                    headimgurl: user.patient_avatar_icon,
                    imgBaseData: '',
                    remark: ''
                },
                dbinfo: {}, // 如果没有需要更新的数据库信息,可以保持空
                msg: "success"
            };
            res.json(response);
        } else {
            const response = {
                retCode: "0",
                msg: "没有找到患者,请先检查患者code"
            };
            res.json(response);
        }
    } catch (err) {
        console.error(err);
        res.status(500).json({ retCode: "-1", msg: "Database error" });
    }
  }else{
    // 如果请求参数不符合预期,则返回错误信息
    res.status(400).json({ retCode: "-1", msg: "Invalid request parameters" });
  }
  console
})
// 启动服务器
app.listen(port, async () => {
@@ -235,8 +319,76 @@
// 访问日志记录
const logHttps=(mode)=>{
    try{
    console.log(mode)
    console.log(JSON.stringify(mode))
    let sqlStr=`INSERT INTO system_log (id, code, url, parameter) VALUES (0, '', '${mode.action}', '${JSON.stringify(mode)}')`
    pool.promise().query(sqlStr)
}
    }catch (err) {
        console.error('Error creating directory:', err);
        throw err;
    }
}
// 插入数据
const installData=async (mode)=>{
    try{
     let sqlStr=`INSERT INTO  patient_rtcf_result (id, code, patient_code,`+
            `comm_time,rtcf_json_bmi,`+
            `rtcf_json_bp,`+
            `rtcf_json_bf,`+
            `rtcf_json_temp,`+
            `rtcf_json_bo,`+
            `rtcf_json_bs,`+
            `rtcf_json_whr,`+
            `rtcf_json_ncg,`+
            `rtcf_json_zytz,`+
            `rtcf_json_ecg,`+
            `rtcf_json_xzsx,`+
            `rtcf_json_eye,`+
            `rtcf_json_sds,`+
            `rtcf_json_txhxdb,`+
            `rtcf_json_fei,`+
            `rtcf_jiu,`+
            `rtcf_gmd)`+
            `VALUES (0, '', '${mode.UID}', '${moment(new Date()).format('YYYY-MM-DD HH:mm:ss')}','${JSON.stringify(mode.BMI)}',`+
            `'${JSON.stringify(mode.BP)}','${JSON.stringify(mode.BF)}','${JSON.stringify(mode.temp)}','${JSON.stringify(mode.bo)}',`+
            `'${JSON.stringify(mode.bs)}','${JSON.stringify(mode.whr)}','${JSON.stringify(mode.ncg)}','${JSON.stringify(mode.zytz)}',`+
            `'${JSON.stringify(mode.ecg)}','${JSON.stringify(mode.xzsx)}','${JSON.stringify(mode.eye)}',`+
            `'${JSON.stringify(mode.sds)}','${JSON.stringify(mode.thxhdb)}','${JSON.stringify(mode.fei)}','${JSON.stringify(mode.jiu)}','${JSON.stringify(mode.gmd)}')`
        console.log('-------------------')
        console.log(sqlStr)
       await pool.promise().query(sqlStr)
    }catch(err){
        console.log('数据写入异常')
    }
}
function getAgeFromIdCard(idCard) {
    // 检查身份证号码长度是否正确
    if (idCard.length !== 18) {
        // throw new Error('身份证号码长度必须为18位');
        return ''
    }
    // 获取出生年月日
    const birthYear = parseInt(idCard.substring(6, 10), 10);
    const birthMonth = parseInt(idCard.substring(10, 12), 10);
    const birthDay = parseInt(idCard.substring(12, 14), 10);
    // 获取当前日期
    const now = new Date();
    const currentYear = now.getFullYear();
    const currentMonth = now.getMonth() + 1; // 注意:getMonth() 返回的月份是从0开始的
    const currentDay = now.getDate();
    // 计算年龄
    let age = currentYear - birthYear;
    // 如果当前日期在生日之前,则年龄减一
    if (currentMonth < birthMonth || (currentMonth === birthMonth && currentDay < birthDay)) {
        age--;
    }
    return age;
}