فهرست منبع

feat: 家庭成员编辑页面人员类型根据身份证自动设置

Developer 2 هفته پیش
والد
کامیت
07d4214611
1فایلهای تغییر یافته به همراه28 افزوده شده و 13 حذف شده
  1. 28 13
      pagesMy/familyMemberVindicate.vue

+ 28 - 13
pagesMy/familyMemberVindicate.vue

@@ -28,17 +28,8 @@
 			<view class="title">成员信息</view>
 			<view class="pre adfac" style="margin-top: 40rpx;">
 				<view class="pre-title"><span>*</span>人员类型</view>
-				<view class="pre-content adfac">
-					<view class="pre-content-box adfac" @click="changeMember(1,'personnelType')">
-						<image v-if="memberInfo.personnelType==1" src="https://oss.familydaf.cn/sxsnfile/20251218/ef2d43944c7e4cea8c6ebe0b4179f0fc.png"></image>
-						<image v-else src="https://oss.familydaf.cn/sxsnfile/20251218/a27c9b23939d4d5085ff790ed4bd2d6a.png"></image>
-						<text>成人</text>
-					</view>
-					<view class="pre-content-box adfac" @click="changeMember(2,'personnelType')">
-						<image v-if="memberInfo.personnelType==2" src="https://oss.familydaf.cn/sxsnfile/20251218/ef2d43944c7e4cea8c6ebe0b4179f0fc.png"></image>
-						<image v-else src="https://oss.familydaf.cn/sxsnfile/20251218/a27c9b23939d4d5085ff790ed4bd2d6a.png"></image>
-						<text>学生</text>
-					</view>
+				<view class="pre-content">
+					<view style="font-size: 30rpx;color: #252525;text-align: right;">{{memberInfo.personnelType===2?'学生':'成人'}}</view>
 				</view>
 			</view>
 			<view class="pre adfac">
@@ -100,9 +91,25 @@
 <script setup name="">
 	import { BaseApi2 } from '../common/api/baseApi';
 	import CusHeader from '@/components/CusHeader/index.vue'
-	import { onLoad } from '@dcloudio/uni-app' 
-	import { ref, getCurrentInstance, nextTick } from 'vue'
+	import { onLoad } from '@dcloudio/uni-app'
+	import { ref, getCurrentInstance, nextTick, watch } from 'vue'
 	const { proxy } = getCurrentInstance()
+
+	const isValidIdCard = (idCard) => idCard && proxy.$reg.idCard(idCard)
+	const calcAge = (idCard) => {
+		if (!isValidIdCard(idCard)) return -1
+		const birthStr = idCard.substring(6, 14)
+		const birth = new Date(
+			parseInt(birthStr.substring(0, 4)),
+			parseInt(birthStr.substring(4, 6)) - 1,
+			parseInt(birthStr.substring(6, 8))
+		)
+		const today = new Date()
+		let age = today.getFullYear() - birth.getFullYear()
+		const m = today.getMonth() - birth.getMonth()
+		if (m < 0 || (m === 0 && today.getDate() < birth.getDate())) age--
+		return age
+	}
 	
 	const title = ref('添加家庭成员')
 	const defaultAvatar = ref('https://oss.familydaf.cn/sxsnfile/20251218/3821654e080945998d464f3c3aa64122.png')
@@ -160,6 +167,14 @@
 	const changeMember = (value,key) => {
 		memberInfo.value[key] = value;
 	}
+
+	// 监听身份证变化,自动设置人员类型(>=18 为成人,<18 为学生)
+	watch(() => memberInfo.value.idCard, (val) => {
+		const age = calcAge(val)
+		if (age >= 0) {
+			memberInfo.value.personnelType = age >= 18 ? 1 : 2
+		}
+	})
 	
 	const handleSave = async () => {
 		if(!memberInfo.value.avatarPath) return proxy.$showToast('请上传头像')