chenyc
2024-10-21 63945718fe73672fb1cb30e0b7956636b53eb1cb
app.js
@@ -63,10 +63,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: 'ccc'
                        remark: ''
                    },
                    dbinfo: {}, // 如果没有需要更新的数据库信息,可以保持空
                    msg: "success"
@@ -296,4 +296,60 @@
    }catch(err){
        console.log('数据写入异常')
    }
}
}
const getAge=(idCard)=>{
       // 检查身份证号码长度是否正确
       if (idCard.length !== 18) {
        throw new Error('身份证号码长度必须为18位');
    }
    // 获取出生年月日
    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;
}
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;
}