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:
caoxiaozhu
2026-05-14 02:25:15 +00:00
parent 3965c1ec42
commit c0401dbd0d
4 changed files with 200 additions and 44 deletions

7
create_employee.sh Normal file
View File

@@ -0,0 +1,7 @@
#!/bin/bash
export PYTHONIOENCODING=utf-8
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
cd /app/server && .venv/bin/python3 /tmp/create_employee_input.py

View File

@@ -98,6 +98,66 @@ function buildUrl(path) {
return `${runtimeApiBaseUrl}${path}` return `${runtimeApiBaseUrl}${path}`
} }
function formatValidationLocation(loc) {
if (!Array.isArray(loc)) {
return ''
}
return loc
.filter((item) => item !== 'body')
.map((item) => String(item || '').trim())
.filter(Boolean)
.join('.')
}
function resolveErrorMessage(payload, fallback = '接口请求失败,请稍后重试。') {
const detail = payload?.detail
if (typeof detail === 'string' && detail.trim()) {
return detail.trim()
}
if (Array.isArray(detail) && detail.length) {
const messages = detail
.map((item) => {
if (typeof item === 'string' && item.trim()) {
return item.trim()
}
if (!item || typeof item !== 'object') {
return ''
}
const message = String(item.msg || item.message || '').trim()
const location = formatValidationLocation(item.loc)
if (location && message) {
return `${location}: ${message}`
}
return message
})
.filter(Boolean)
if (messages.length) {
return messages.join('')
}
}
if (detail && typeof detail === 'object') {
const message = String(detail.message || detail.msg || '').trim()
if (message) {
return message
}
}
if (typeof payload?.message === 'string' && payload.message.trim()) {
return payload.message.trim()
}
return fallback
}
export async function apiRequest(path, options = {}) { export async function apiRequest(path, options = {}) {
const { const {
contentType = 'application/json', contentType = 'application/json',
@@ -136,7 +196,7 @@ export async function apiRequest(path, options = {}) {
payload = null payload = null
} }
throw new Error(payload?.detail || '接口请求失败,请稍后重试。') throw new Error(resolveErrorMessage(payload))
} }
return response.blob() return response.blob()
@@ -150,7 +210,7 @@ export async function apiRequest(path, options = {}) {
} }
if (!response.ok) { if (!response.ok) {
throw new Error(payload?.detail || '接口请求失败,请稍后重试。') throw new Error(resolveErrorMessage(payload))
} }
return payload return payload

View File

@@ -100,6 +100,46 @@ function normalizeNullableText(value) {
return text || null 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) { function sameValues(left, right) {
if (left.length !== right.length) { if (left.length !== right.length) {
return false return false
@@ -547,6 +587,11 @@ export default {
return return
} }
if (!isValidEmail(employeeForm.value.email)) {
toast('请输入有效的邮箱地址。')
return
}
if (!normalizeText(employeeForm.value.position)) { if (!normalizeText(employeeForm.value.position)) {
toast('岗位不能为空。') toast('岗位不能为空。')
return return
@@ -557,6 +602,18 @@ export default {
return 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) { if (normalizeText(employeeForm.value.password) && normalizeText(employeeForm.value.password).length < 5) {
toast('员工密码至少需要 5 位。') toast('员工密码至少需要 5 位。')
return return

View File

@@ -86,10 +86,42 @@ async function testInjectsAuthenticatedUserHeaders() {
assert.equal(capturedOptions.headers['x-auth-is-admin'], 'true') assert.equal(capturedOptions.headers['x-auth-is-admin'], 'true')
} }
async function testFormatsValidationErrors() {
global.fetch = async () => ({
ok: false,
async json() {
return {
detail: [
{
loc: ['body', 'email'],
msg: 'value is not a valid email address'
},
{
loc: ['body', 'password'],
msg: 'String should have at least 5 characters'
}
]
}
}
})
await assert.rejects(
() => apiRequest('/employees/demo', { method: 'PATCH', body: '{}' }),
(error) => {
assert.equal(
error.message,
'email: value is not a valid email addresspassword: String should have at least 5 characters'
)
return true
}
)
}
async function run() { async function run() {
await testUsesCustomContentTypeHeader() await testUsesCustomContentTypeHeader()
await testSupportsBlobResponses() await testSupportsBlobResponses()
await testInjectsAuthenticatedUserHeaders() await testInjectsAuthenticatedUserHeaders()
await testFormatsValidationErrors()
console.log('api-request tests passed') console.log('api-request tests passed')
} }