hookehuyr

✨ feat: 身份证控件新增录入提示和性别显示

1 <!-- 1 <!--
2 * @Date: 2022-09-14 14:44:30 2 * @Date: 2022-09-14 14:44:30
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2022-12-29 11:32:10 4 + * @LastEditTime: 2023-02-01 12:05:15
5 * @FilePath: /data-table/src/components/IdentityField/index.vue 5 * @FilePath: /data-table/src/components/IdentityField/index.vue
6 * @Description: 身份证输入控件 6 * @Description: 身份证输入控件
7 --> 7 -->
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
25 :border="false" 25 :border="false"
26 > 26 >
27 </van-field> 27 </van-field>
28 + <div v-if="gender" class="gender"><span>性别:</span>{{ gender }}</div>
28 <van-number-keyboard 29 <van-number-keyboard
29 v-model="item.value" 30 v-model="item.value"
30 :show="show" 31 :show="show"
...@@ -41,6 +42,7 @@ ...@@ -41,6 +42,7 @@
41 <script setup> 42 <script setup>
42 import $ from "jquery"; 43 import $ from "jquery";
43 import { storeToRefs, mainStore } from "@/utils/generatePackage"; 44 import { storeToRefs, mainStore } from "@/utils/generatePackage";
45 +import { showSuccessToast, showFailToast } from "vant";
44 46
45 const props = defineProps({ 47 const props = defineProps({
46 item: Object, 48 item: Object,
...@@ -108,6 +110,15 @@ const blurKeyboard = () => { ...@@ -108,6 +110,15 @@ const blurKeyboard = () => {
108 document.getElementById("app").style.paddingBottom = "0"; 110 document.getElementById("app").style.paddingBottom = "0";
109 // 还原border颜色 111 // 还原border颜色
110 content.css("border-color", "#eaeaea"); 112 content.css("border-color", "#eaeaea");
113 + // 键盘失焦检查输入和添加性别显示
114 + const input_val = props.item.value;
115 + if (required && !input_val) {
116 + showFailToast("身份证号码不能为空");
117 + } else if (input_val && !/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(input_val)) {
118 + showFailToast("请输入正确身份证号码");
119 + } else {
120 + gender.value = getGenderByIdNumber(input_val)
121 + }
111 }; 122 };
112 123
113 // 校验函数返回 true 表示校验通过,false 表示不通过 124 // 校验函数返回 true 表示校验通过,false 表示不通过
...@@ -134,6 +145,31 @@ const rules = [{ validator, message: validatorMessage }]; ...@@ -134,6 +145,31 @@ const rules = [{ validator, message: validatorMessage }];
134 145
135 const onInput = (value) => {}; 146 const onInput = (value) => {};
136 const onDelete = () => {}; 147 const onDelete = () => {};
148 +
149 +const gender = ref('');
150 +
151 +/**
152 + * 按身份证号码获取性别
153 + * @idNumber 身份证号码
154 + * @return 男:male;女:female;异常(身份证号码为空或长度、格式错误):undefined
155 + */
156 +const getGenderByIdNumber = (idNumber) => {
157 + if (idNumber) {
158 + let genderCode; // 性别代码
159 + if (idNumber.length == 18) { // 二代身份证号码长度为18位(第17位为性别代码)
160 + genderCode = idNumber.charAt(16);
161 + } else if (idNumber.length == 15) { // 一代身份证号码长度为15位(第15位为性别代码)
162 + genderCode = idNumber.charAt(14);
163 + }
164 + if (genderCode && !isNaN(genderCode)) {
165 + // 两代身份证号码的性别代码都为男奇女偶
166 + if (parseInt(genderCode) % 2 == 0) {
167 + return '女';
168 + }
169 + return '男';
170 + }
171 + }
172 +}
137 </script> 173 </script>
138 174
139 <style lang="less" scoped> 175 <style lang="less" scoped>
...@@ -146,6 +182,13 @@ const onDelete = () => {}; ...@@ -146,6 +182,13 @@ const onDelete = () => {};
146 color: red; 182 color: red;
147 } 183 }
148 } 184 }
185 + .gender {
186 + padding: 0 1rem 0 1rem;
187 + font-size: 0.9rem;
188 + span {
189 + font-weight: bold;
190 + }
191 + }
149 } 192 }
150 193
151 :deep(.van-field__body) { 194 :deep(.van-field__body) {
......