请年假、育儿假校验优化

This commit is contained in:
runny committed 2026-05-20 10:26:54 +08:00
1 parent 46d7fff01e
commit 72642b981e
2 files changed
+136 -2

No files matched your search

+78 -2
View File
@@ -946,7 +946,81 @@ const saveLocalMessageToBackend = async (msg: any, userQuery: string = '') => {
}
};
const NEED_BALANCE_CHECK_TYPES = new Set(['010', '408']);
const formatLeaveBalanceNumber = (value: any) => {
if (value === null || value === undefined) return '暂无数据';
const num = Number(value);
if (Number.isNaN(num)) return String(value);
return Number.isInteger(num) ? String(num) : num.toFixed(2);
};
const checkLeaveBalance = async (leaveTypeCode: string): Promise<{
has_balance: boolean;
leave_type_label: string;
year: number;
total_hours: number | null;
used_hours: number | null;
remaining_hours: number | null;
} | null> => {
try {
const res = await fetch(`/api/user/leave-balance?leave_type=${encodeURIComponent(leaveTypeCode)}`, {
headers: getAuthHeaders(),
});
if (!res.ok) return null;
return await res.json();
} catch (e) {
console.error('Failed to check leave balance', e);
return null;
}
};
const startLocalLeaveFlow = async (content: string) => {
const initialLeaveType = extractLeaveTypeFromIntent(content);
if (initialLeaveType && NEED_BALANCE_CHECK_TYPES.has(initialLeaveType)) {
messages.value.push({ role: 'user', content, type: 'text' });
const thinkingMsg = {
role: 'assistant' as const,
type: 'text' as const,
content: '正在查询您的假期余额,请稍候...',
};
messages.value.push(thinkingMsg);
const balance = await checkLeaveBalance(initialLeaveType);
const thinkingIndex = messages.value.indexOf(thinkingMsg);
if (thinkingIndex >= 0) {
messages.value.splice(thinkingIndex, 1);
}
if (balance && !balance.has_balance) {
const unit = initialLeaveType === '010' ? '天' : '小时';
const detailLines = [
`**${balance.leave_type_label}余额不足,暂无可用假期。**`,
'',
`| 假别 | 年度 | 可休 | 已休 | 当前剩余 |`,
`| :---: | :---: | :---: | :---: | :---: |`,
`| ${balance.leave_type_label} | ${balance.year} | ${formatLeaveBalanceNumber(balance.total_hours)}${unit} | ${formatLeaveBalanceNumber(balance.used_hours)}${unit} | ${formatLeaveBalanceNumber(balance.remaining_hours)}${unit} |`,
'',
'如有疑问,请联系人事部门确认假期数据。',
];
const noBalanceMsg = {
role: 'assistant' as const,
type: 'text' as const,
content: detailLines.join('\n'),
};
messages.value.push(noBalanceMsg);
await saveLocalMessageToBackend(noBalanceMsg, content);
if (history.value.length === 0 || !history.value.some(h => h.conversation_id === conversationId.value)) {
await loadHistory();
}
return;
}
}
const formId = newLocalMessageId('leave-form');
const payload: LeaveFormPayload = {
formId,
@@ -954,8 +1028,10 @@ const startLocalLeaveFlow = async (content: string) => {
submitting: false,
confirmationGenerated: false,
};
messages.value.push({ role: 'user', content, type: 'text' });
if (!initialLeaveType || !NEED_BALANCE_CHECK_TYPES.has(initialLeaveType)) {
messages.value.push({ role: 'user', content, type: 'text' });
}
const formMsg = {
role: 'assistant' as const,
type: 'leave-form' as const,