fix bugs
This commit is contained in:
@@ -45,6 +45,31 @@
|
||||
/>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="form-item">
|
||||
<view class="form-label">所属单位</view>
|
||||
<view class="form-value">
|
||||
<input
|
||||
class="form-input"
|
||||
v-model="formData.organization"
|
||||
placeholder="请输入所属单位"
|
||||
placeholder-class="placeholder"
|
||||
/>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="form-item">
|
||||
<view class="form-label">联系电话</view>
|
||||
<view class="form-value">
|
||||
<input
|
||||
class="form-input"
|
||||
v-model="formData.phone"
|
||||
type="number"
|
||||
placeholder="请输入联系电话"
|
||||
placeholder-class="placeholder"
|
||||
/>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 错误提示 -->
|
||||
@@ -88,6 +113,8 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import athleteAPI from '@/api/athlete.js'
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
@@ -95,7 +122,9 @@ export default {
|
||||
idType: '身份证',
|
||||
name: '',
|
||||
idCard: '',
|
||||
team: ''
|
||||
team: '',
|
||||
organization: '',
|
||||
phone: ''
|
||||
},
|
||||
errors: [],
|
||||
showHint: false,
|
||||
@@ -110,7 +139,10 @@ export default {
|
||||
this.formData.name &&
|
||||
this.formData.idCard &&
|
||||
this.formData.team &&
|
||||
this.validateIdCard(this.formData.idCard)
|
||||
this.formData.organization &&
|
||||
this.formData.phone &&
|
||||
this.validateIdCard(this.formData.idCard) &&
|
||||
this.validatePhone(this.formData.phone)
|
||||
);
|
||||
}
|
||||
},
|
||||
@@ -123,20 +155,30 @@ export default {
|
||||
},
|
||||
'formData.team'(val) {
|
||||
this.validateForm();
|
||||
},
|
||||
'formData.organization'(val) {
|
||||
this.validateForm();
|
||||
},
|
||||
'formData.phone'(val) {
|
||||
this.validateForm();
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
validateIdCard(idCard) {
|
||||
// 简单的身份证号验证
|
||||
return /^\d{18}$/.test(idCard);
|
||||
// 身份证号验证:18位,最后一位可以是数字或字母X
|
||||
return /^\d{17}[\dXx]$/.test(idCard);
|
||||
},
|
||||
validatePhone(phone) {
|
||||
// 手机号验证:11位数字
|
||||
return /^1[3-9]\d{9}$/.test(phone);
|
||||
},
|
||||
validateForm() {
|
||||
this.errors = [];
|
||||
this.showHint = false;
|
||||
|
||||
if (!this.formData.name || !this.formData.idCard || !this.formData.team) {
|
||||
if (!this.formData.name || !this.formData.idCard || !this.formData.team || !this.formData.organization || !this.formData.phone) {
|
||||
this.showHint = true;
|
||||
if (!this.formData.name || !this.formData.idCard || !this.formData.team) {
|
||||
if (!this.formData.name || !this.formData.idCard || !this.formData.team || !this.formData.organization || !this.formData.phone) {
|
||||
this.errors.push({
|
||||
label: '有空文本时弹出:',
|
||||
message: '按钮置灰'
|
||||
@@ -154,25 +196,93 @@ export default {
|
||||
message: '按钮置灰'
|
||||
});
|
||||
}
|
||||
|
||||
if (this.formData.phone && !this.validatePhone(this.formData.phone)) {
|
||||
this.errors.push({
|
||||
label: '手机号格式不正确:',
|
||||
message: '按钮置灰'
|
||||
});
|
||||
}
|
||||
},
|
||||
handleIdTypeChange(e) {
|
||||
this.formData.idType = '身份证';
|
||||
this.showIdTypePicker = false;
|
||||
},
|
||||
handleSave() {
|
||||
/**
|
||||
* 从身份证号中提取信息
|
||||
*/
|
||||
extractInfoFromIdCard(idCard) {
|
||||
if (!idCard || idCard.length !== 18) {
|
||||
return {
|
||||
gender: null,
|
||||
age: null,
|
||||
birthDate: null
|
||||
}
|
||||
}
|
||||
|
||||
// 提取出生日期
|
||||
const year = idCard.substring(6, 10)
|
||||
const month = idCard.substring(10, 12)
|
||||
const day = idCard.substring(12, 14)
|
||||
const birthDate = `${year}-${month}-${day}`
|
||||
|
||||
// 计算年龄
|
||||
const birthYear = parseInt(year)
|
||||
const currentYear = new Date().getFullYear()
|
||||
const age = currentYear - birthYear
|
||||
|
||||
// 提取性别(倒数第二位,奇数为男,偶数为女)
|
||||
const genderCode = parseInt(idCard.substring(16, 17))
|
||||
const gender = genderCode % 2 === 1 ? 1 : 2
|
||||
|
||||
return {
|
||||
gender,
|
||||
age,
|
||||
birthDate
|
||||
}
|
||||
},
|
||||
|
||||
async handleSave() {
|
||||
if (!this.isFormValid) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 显示身份证号码格式错误提示(模拟)
|
||||
this.toastMessage = '身份证号码格式不正确';
|
||||
this.showToast = true;
|
||||
setTimeout(() => {
|
||||
this.showToast = false;
|
||||
}, 2000);
|
||||
try {
|
||||
// 从身份证号中提取信息
|
||||
const info = this.extractInfoFromIdCard(this.formData.idCard)
|
||||
|
||||
// 实际保存逻辑
|
||||
// uni.navigateBack();
|
||||
// 调用API保存选手信息(使用后端实体类的字段名)
|
||||
await athleteAPI.submitAthlete({
|
||||
playerName: this.formData.name,
|
||||
idCard: this.formData.idCard,
|
||||
teamName: this.formData.team,
|
||||
organization: this.formData.organization,
|
||||
contactPhone: this.formData.phone,
|
||||
idCardType: 1, // 身份证类型固定为1
|
||||
gender: info.gender,
|
||||
age: info.age,
|
||||
birthDate: info.birthDate
|
||||
})
|
||||
|
||||
// 保存成功
|
||||
uni.showToast({
|
||||
title: '保存成功',
|
||||
icon: 'success'
|
||||
})
|
||||
|
||||
// 延迟返回上一页
|
||||
setTimeout(() => {
|
||||
uni.navigateBack()
|
||||
}, 1500)
|
||||
} catch (err) {
|
||||
console.error('保存选手失败:', err)
|
||||
// 显示错误提示
|
||||
this.toastMessage = '保存失败,请重试'
|
||||
this.showToast = true
|
||||
setTimeout(() => {
|
||||
this.showToast = false
|
||||
}, 2000)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user