feat(安全培训): 新增员工安全培训相关功能

This commit is contained in:
runny committed 2026-08-12 11:56:35 +08:00
1 parent cbc3cb959f
commit 8e5c57790c
50 files changed
+3769 -87

No files matched your search

+109 -5
View File
@@ -6,11 +6,26 @@
*/
import { requestJson } from '@/shared/api/client';
import { getAuthHeaders } from '@/shared/utils/auth';
import type { TrainingAnswerRecord, TrainingProgressRecord, TrainingRecord, TrainingStage, TrainingStats } from '../training/training.types';
import type {
SafetyProgressState,
TrainingAnswerRecord,
TrainingExamMode,
TrainingProgressRecord,
TrainingPublishedConfig,
TrainingRecord,
TrainingStage,
TrainingStats,
TrainingType,
} from '../training/training.types';
type TrainingRecordApi = {
id: number;
user_id?: number;
training_type?: TrainingType;
training_version?: string | null;
exam_mode?: TrainingExamMode;
safety_stage?: string | null;
safety_progress?: Partial<SafetyProgressState> | null;
employee_id?: string | null;
name?: string | null;
department?: string | null;
@@ -31,6 +46,32 @@ type TrainingRecordApi = {
status?: TrainingRecord['status'];
};
type TrainingConfigApi = {
id: number;
training_type?: TrainingType;
version?: string | null;
pass_score?: number | null;
settings?: Record<string, unknown> | null;
resources?: Array<{
id?: number | null;
resource_type?: string | null;
training_level?: 'company' | 'department' | 'team' | null;
agent_guide_id?: number | null;
inline_questions?: unknown[];
title?: string | null;
sort_order?: number;
required_duration?: number;
resource?: {
id?: number;
file_name?: string;
file_type?: string;
file_url?: string;
guide_usage?: string;
source_file_name?: string;
} | null;
}>;
};
type TrainingListApi = {
items: TrainingRecordApi[];
total: number;
@@ -52,13 +93,19 @@ type TrainingStatsApi = {
export interface TrainingRecordQuery {
keyword?: string;
department?: string;
status?: 'all' | 'passed' | 'failed' | 'in_progress';
status?: string;
type?: string;
page?: number;
page_size?: number;
}
export interface SaveTrainingProgressPayload {
stage: TrainingStage;
agentId?: number | null;
trainingVersion?: string | null;
examMode?: TrainingExamMode;
safetyStage?: string | null;
safetyProgress?: Partial<SafetyProgressState> | null;
currentQuestionIndex: number;
score: number;
passed: boolean;
@@ -80,9 +127,20 @@ function mapAnswerDetails(items?: TrainingRecordApi['answer_details']): Training
: [];
}
function mapSafetyProgressState(items?: TrainingRecordApi['safety_progress']): SafetyProgressState {
return {
company: String(items?.company || 'pending'),
department: String(items?.department || 'pending'),
team: String(items?.team || 'pending'),
resource_progress: (items as any)?.resource_progress || {},
};
}
function mapTrainingProgressRecord(data: TrainingRecordApi): TrainingProgressRecord {
return {
id: data.id,
trainingType: data.training_type || 'onboarding',
trainingVersion: data.training_version || undefined,
name: String(data.name || ''),
employeeId: String(data.employee_id || ''),
department: String(data.department || ''),
@@ -92,6 +150,9 @@ function mapTrainingProgressRecord(data: TrainingRecordApi): TrainingProgressRec
attempts: Number(data.attempt_count || 0),
status: data.status || 'in_progress',
stage: (data.stage || 'video') as TrainingStage,
examMode: (data.exam_mode || 'unified') as TrainingExamMode,
safetyStage: data.safety_stage ? String(data.safety_stage) as TrainingProgressRecord['safetyStage'] : null,
safetyProgress: mapSafetyProgressState(data.safety_progress),
currentQuestionIndex: Number(data.current_question_index || 0),
passed: Boolean(data.passed),
answerDetails: mapAnswerDetails(data.answer_details),
@@ -101,10 +162,41 @@ function mapTrainingProgressRecord(data: TrainingRecordApi): TrainingProgressRec
};
}
function mapTrainingConfig(data: TrainingConfigApi): TrainingPublishedConfig {
return {
id: data.id,
trainingType: data.training_type || 'onboarding',
version: String(data.version || ''),
passScore: Number(data.pass_score || 0),
settings: data.settings || {},
resources: Array.isArray(data.resources)
? data.resources.map((resource) => ({
id: resource.id,
resourceType: resource.resource_type,
trainingLevel: resource.training_level,
agentGuideId: resource.agent_guide_id,
inlineQuestions: resource.inline_questions,
title: resource.title,
sortOrder: Number(resource.sort_order || 0),
requiredDuration: Number(resource.required_duration || 0),
resource: resource.resource ? {
id: resource.resource.id,
fileName: resource.resource.file_name || resource.resource.source_file_name,
fileType: resource.resource.file_type,
fileUrl: resource.resource.file_url,
guideUsage: resource.resource.guide_usage,
} : null,
}))
: [],
};
}
function mapTrainingRecord(data: TrainingRecordApi): TrainingRecord {
const progress = mapTrainingProgressRecord(data);
return {
id: progress.id,
trainingType: progress.trainingType,
trainingVersion: progress.trainingVersion,
name: progress.name,
employeeId: progress.employeeId,
department: progress.department,
@@ -121,26 +213,38 @@ function buildQueryString(query: TrainingRecordQuery = {}) {
if (query.keyword) params.set('keyword', query.keyword);
if (query.department) params.set('department', query.department);
if (query.status) params.set('status', query.status);
if (query.type) params.set('type', query.type);
if (query.page) params.set('page', String(query.page));
if (query.page_size) params.set('page_size', String(query.page_size));
return params.toString();
}
export async function getMyTrainingProgress(): Promise<TrainingProgressRecord | null> {
const data = await requestJson<TrainingRecordApi | null>('/api/user/training-record', authHeaders());
export async function getMyTrainingProgress(type: string = 'onboarding'): Promise<TrainingProgressRecord | null> {
const data = await requestJson<TrainingRecordApi | null>(`/api/user/training-record?type=${type}`, authHeaders());
return data ? mapTrainingProgressRecord(data) : null;
}
export async function saveMyTrainingProgress(payload: SaveTrainingProgressPayload): Promise<TrainingProgressRecord> {
export async function getMyTrainingConfig(type: string = 'onboarding'): Promise<TrainingPublishedConfig | null> {
const data = await requestJson<TrainingConfigApi | null>(`/api/user/training-config?type=${encodeURIComponent(type)}`, authHeaders());
return data ? mapTrainingConfig(data) : null;
}
export async function saveMyTrainingProgress(payload: SaveTrainingProgressPayload, type: string = 'onboarding'): Promise<TrainingProgressRecord> {
const data = await requestJson<TrainingRecordApi>('/api/user/training-record/progress', {
method: 'PUT',
...jsonAuthHeaders(),
body: JSON.stringify({
agent_id: payload.agentId || undefined,
stage: payload.stage,
training_version: payload.trainingVersion || undefined,
exam_mode: payload.examMode || undefined,
safety_stage: payload.safetyStage || undefined,
safety_progress: payload.safetyProgress || undefined,
current_question_index: payload.currentQuestionIndex,
score: payload.score,
passed: payload.passed,
attempt_count: payload.attemptCount,
type: type,
answer_details: payload.answerDetails.map((item) => ({
question_id: item.questionId,
selected_option_id: item.selectedOptionId,