4、年龄:增加单位,比如29岁,当年龄字段赋值29岁时系统自动换算出生年月日的值。
This commit is contained in:
@@ -80,6 +80,19 @@
|
||||
<el-input v-model="form.countryCode" clearable :disabled="isViewMode" />
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
<el-col :span="8">
|
||||
<el-form-item label="出生日期" prop="birthDate">
|
||||
<el-date-picker
|
||||
v-model="form.birthDate"
|
||||
type="date"
|
||||
placeholder="请选择出生日期"
|
||||
format="YYYY年MM月DD日"
|
||||
:disabled="isViewMode"
|
||||
value-format="YYYY-MM-DD"
|
||||
@change="handleBirthDateChange"
|
||||
/>
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
</el-row>
|
||||
<!-- <el-col :span="6">
|
||||
<el-form-item label="年龄" prop="age">
|
||||
@@ -109,7 +122,8 @@
|
||||
<el-input
|
||||
v-model="form.age"
|
||||
:disabled="isViewMode"
|
||||
@input="(value) => (form.age = value.replace(/[^0-9]/g, ''))"
|
||||
@input="handleAgeInput"
|
||||
placeholder="请输入年龄"
|
||||
/>
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
@@ -320,6 +334,8 @@ const data = reactive({
|
||||
isViewMode: false,
|
||||
form: {
|
||||
typeCode: '08',
|
||||
birthDate: undefined,
|
||||
age: undefined,
|
||||
},
|
||||
rules: {
|
||||
name: [{ required: true, message: '姓名不能为空', trigger: 'change' }],
|
||||
@@ -330,6 +346,7 @@ const data = reactive({
|
||||
{ required: true, message: '证件号码不能为空', trigger: 'change' },
|
||||
{ validator: validateIdCard, trigger: 'blur' }
|
||||
],
|
||||
birthDate: [{ required: false, message: '请选择出生日期', trigger: 'change' }],
|
||||
},
|
||||
});
|
||||
|
||||
@@ -347,6 +364,46 @@ const props = defineProps({
|
||||
},
|
||||
});
|
||||
|
||||
// 处理出生日期变化,自动计算年龄
|
||||
function handleBirthDateChange() {
|
||||
if (form.value.birthDate) {
|
||||
const birthDate = new Date(form.value.birthDate);
|
||||
const today = new Date();
|
||||
|
||||
let age = today.getFullYear() - birthDate.getFullYear();
|
||||
const monthDiff = today.getMonth() - birthDate.getMonth();
|
||||
|
||||
// 计算精确年龄
|
||||
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
|
||||
age--;
|
||||
}
|
||||
|
||||
form.value.age = age + '岁';
|
||||
}
|
||||
}
|
||||
|
||||
// 处理年龄输入,自动计算出生日期
|
||||
function handleAgeInput() {
|
||||
// 提取数字部分
|
||||
const ageMatch = form.value.age.match(/\d+/);
|
||||
if (ageMatch) {
|
||||
const age = parseInt(ageMatch[0]);
|
||||
// 移除非数字字符,保留数字和可能的单位
|
||||
form.value.age = age + '岁';
|
||||
|
||||
// 计算出生日期
|
||||
const today = new Date();
|
||||
const birthYear = today.getFullYear() - age;
|
||||
const birthMonth = today.getMonth();
|
||||
const birthDay = today.getDate();
|
||||
|
||||
const birthDate = new Date(birthYear, birthMonth, birthDay);
|
||||
|
||||
// 格式化为YYYY-MM-DD
|
||||
const formattedBirthDate = birthDate.toISOString().split('T')[0];
|
||||
form.value.birthDate = formattedBirthDate;
|
||||
}
|
||||
}
|
||||
watch(
|
||||
() => form.value.idCard,
|
||||
(newIdCard) => {
|
||||
@@ -354,7 +411,8 @@ watch(
|
||||
const birthYear = parseInt(newIdCard.substring(6, 10));
|
||||
const birthMonth = parseInt(newIdCard.substring(10, 12));
|
||||
const birthDay = parseInt(newIdCard.substring(12, 14));
|
||||
|
||||
// 设置出生日期
|
||||
form.value.birthDate = `${birthYear}-${birthMonth.toString().padStart(2, '0')}-${birthDay.toString().padStart(2, '0')}`;
|
||||
const today = new Date();
|
||||
const currentYear = today.getFullYear();
|
||||
const currentMonth = today.getMonth() + 1;
|
||||
@@ -370,7 +428,7 @@ watch(
|
||||
age--;
|
||||
}
|
||||
|
||||
form.value.age = age;
|
||||
form.value.age = age + '岁';
|
||||
}
|
||||
}
|
||||
);
|
||||
@@ -463,6 +521,7 @@ function reset() {
|
||||
maritalStatusEnum: undefined,
|
||||
busNo: undefined,
|
||||
organizationId: undefined,
|
||||
birthDate: undefined,
|
||||
};
|
||||
proxy.resetForm('patientRef');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user