refactor(web): update API service, view script and add tests
Frontend: - services/api.js: update API service client - views/scripts/EmployeeManagementView.js: update employee management view script - tests/api-request.test.mjs: update API request tests Scripts: - create_employee.sh: add employee creation script
This commit is contained in:
@@ -100,6 +100,46 @@ function normalizeNullableText(value) {
|
||||
return text || null
|
||||
}
|
||||
|
||||
function isValidEmail(value) {
|
||||
const normalized = normalizeText(value)
|
||||
if (!normalized) {
|
||||
return false
|
||||
}
|
||||
|
||||
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/u.test(normalized)
|
||||
}
|
||||
|
||||
function isValidIsoDate(value) {
|
||||
const normalized = normalizeText(value)
|
||||
if (!normalized) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (!/^\d{4}-\d{2}-\d{2}$/u.test(normalized)) {
|
||||
return false
|
||||
}
|
||||
|
||||
const [yearText, monthText, dayText] = normalized.split('-')
|
||||
const year = Number.parseInt(yearText, 10)
|
||||
const month = Number.parseInt(monthText, 10)
|
||||
const day = Number.parseInt(dayText, 10)
|
||||
|
||||
if ([year, month, day].some((item) => Number.isNaN(item))) {
|
||||
return false
|
||||
}
|
||||
|
||||
const parsed = new Date(year, month - 1, day)
|
||||
if (Number.isNaN(parsed.getTime())) {
|
||||
return false
|
||||
}
|
||||
|
||||
return (
|
||||
parsed.getFullYear() === year &&
|
||||
parsed.getMonth() === month - 1 &&
|
||||
parsed.getDate() === day
|
||||
)
|
||||
}
|
||||
|
||||
function sameValues(left, right) {
|
||||
if (left.length !== right.length) {
|
||||
return false
|
||||
@@ -547,6 +587,11 @@ export default {
|
||||
return
|
||||
}
|
||||
|
||||
if (!isValidEmail(employeeForm.value.email)) {
|
||||
toast('请输入有效的邮箱地址。')
|
||||
return
|
||||
}
|
||||
|
||||
if (!normalizeText(employeeForm.value.position)) {
|
||||
toast('岗位不能为空。')
|
||||
return
|
||||
@@ -557,6 +602,18 @@ export default {
|
||||
return
|
||||
}
|
||||
|
||||
const birthDate = normalizeNullableText(employeeForm.value.birthDate)
|
||||
if (birthDate && !isValidIsoDate(birthDate)) {
|
||||
toast('出生日期格式不正确,请使用 YYYY-MM-DD。')
|
||||
return
|
||||
}
|
||||
|
||||
const joinDate = normalizeNullableText(employeeForm.value.joinDate)
|
||||
if (joinDate && !isValidIsoDate(joinDate)) {
|
||||
toast('入职日期格式不正确,请使用 YYYY-MM-DD。')
|
||||
return
|
||||
}
|
||||
|
||||
if (normalizeText(employeeForm.value.password) && normalizeText(employeeForm.value.password).length < 5) {
|
||||
toast('员工密码至少需要 5 位。')
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user