181 lines
5.4 KiB
TypeScript
181 lines
5.4 KiB
TypeScript
// 删除试卷API使用示例
|
||
import { ExamApi } from '../modules/exam'
|
||
|
||
/**
|
||
* 使用示例:删除单个试卷
|
||
*/
|
||
export async function deletePaperExample() {
|
||
try {
|
||
const paperId = '1962379646322384897' // 试卷ID
|
||
|
||
console.log('🚀 准备删除试卷:', paperId)
|
||
|
||
// 调用删除API
|
||
const response = await ExamApi.deleteExamPaper(paperId)
|
||
|
||
console.log('✅ 删除试卷成功:', response)
|
||
|
||
if (response.data) {
|
||
console.log('删除结果:', response.data)
|
||
return true
|
||
} else {
|
||
console.warn('删除操作可能失败')
|
||
return false
|
||
}
|
||
|
||
} catch (error) {
|
||
console.error('删除试卷失败:', error)
|
||
throw error
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 使用示例:批量删除试卷
|
||
*/
|
||
export async function batchDeletePapersExample() {
|
||
try {
|
||
const paperIds = [
|
||
'1962379646322384897',
|
||
'1966450638717292545',
|
||
'1966458655621877761'
|
||
] // 试卷ID数组
|
||
|
||
console.log('🚀 准备批量删除试卷:', paperIds)
|
||
|
||
// 调用批量删除API
|
||
const response = await ExamApi.batchDeleteExamPapers(paperIds)
|
||
|
||
console.log('✅ 批量删除试卷完成:', response)
|
||
|
||
if (response.data) {
|
||
const { success, failed, total, errors } = response.data
|
||
console.log('批量删除结果:', {
|
||
总数: total,
|
||
成功: success,
|
||
失败: failed,
|
||
错误: errors
|
||
})
|
||
|
||
if (failed === 0) {
|
||
console.log('✅ 所有试卷删除成功')
|
||
return { success: true, message: `成功删除 ${success} 个试卷` }
|
||
} else if (success > 0) {
|
||
console.warn('⚠️ 部分试卷删除成功')
|
||
return { success: false, message: `删除完成:成功 ${success} 个,失败 ${failed} 个` }
|
||
} else {
|
||
console.error('❌ 所有试卷删除失败')
|
||
return { success: false, message: `删除失败:${failed} 个试卷删除失败` }
|
||
}
|
||
} else {
|
||
console.warn('批量删除操作可能失败')
|
||
return { success: false, message: '批量删除操作失败' }
|
||
}
|
||
|
||
} catch (error) {
|
||
console.error('批量删除试卷失败:', error)
|
||
throw error
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 在Vue组件中使用示例(使用 Naive UI 对话框组件)
|
||
*/
|
||
export function useDeletePaperInComponent() {
|
||
// 注意:在实际使用中需要从 'naive-ui' 导入 useDialog 和 useMessage
|
||
// import { useDialog, useMessage } from 'naive-ui'
|
||
|
||
const deletePaper = async (paperId: string, paperName: string, dialog: any, message: any) => {
|
||
try {
|
||
// 使用 Naive UI 对话框组件
|
||
dialog.warning({
|
||
title: '确认删除',
|
||
content: `确定要删除试卷"${paperName}"吗?此操作不可撤销。`,
|
||
positiveText: '确定删除',
|
||
negativeText: '取消',
|
||
onPositiveClick: async () => {
|
||
try {
|
||
const response = await ExamApi.deleteExamPaper(paperId)
|
||
console.log('删除试卷成功:', response)
|
||
message.success('试卷删除成功!')
|
||
return true
|
||
} catch (error) {
|
||
console.error('删除试卷失败:', error)
|
||
message.error('删除试卷失败,请重试')
|
||
throw error
|
||
}
|
||
}
|
||
})
|
||
} catch (error) {
|
||
console.error('删除试卷失败:', error)
|
||
throw error
|
||
}
|
||
}
|
||
|
||
const batchDeletePapers = async (paperIds: string[], dialog: any, message: any) => {
|
||
try {
|
||
// 使用 Naive UI 对话框组件
|
||
dialog.error({
|
||
title: '确认批量删除',
|
||
content: `确定要删除选中的 ${paperIds.length} 个试卷吗?此操作不可撤销。`,
|
||
positiveText: '确定删除',
|
||
negativeText: '取消',
|
||
onPositiveClick: async () => {
|
||
try {
|
||
const response = await ExamApi.batchDeleteExamPapers(paperIds)
|
||
console.log('批量删除试卷成功:', response)
|
||
message.success(`成功删除 ${paperIds.length} 个试卷!`)
|
||
return true
|
||
} catch (error) {
|
||
console.error('批量删除试卷失败:', error)
|
||
message.error('批量删除试卷失败,请重试')
|
||
throw error
|
||
}
|
||
}
|
||
})
|
||
} catch (error) {
|
||
console.error('批量删除试卷失败:', error)
|
||
throw error
|
||
}
|
||
}
|
||
|
||
return {
|
||
deletePaper,
|
||
batchDeletePapers
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 错误处理示例
|
||
*/
|
||
export async function deletePaperWithErrorHandling(paperId: string) {
|
||
try {
|
||
const response = await ExamApi.deleteExamPaper(paperId)
|
||
|
||
// 检查响应状态
|
||
if (response.data && response.data === 'success') {
|
||
console.log('试卷删除成功')
|
||
return { success: true, message: '删除成功' }
|
||
} else {
|
||
console.warn('删除操作可能失败:', response)
|
||
return { success: false, message: '删除失败,请重试' }
|
||
}
|
||
|
||
} catch (error: any) {
|
||
console.error('删除试卷时发生错误:', error)
|
||
|
||
// 根据错误类型返回不同的错误信息
|
||
if (error.response) {
|
||
const status = error.response.status
|
||
if (status === 404) {
|
||
return { success: false, message: '试卷不存在' }
|
||
} else if (status === 403) {
|
||
return { success: false, message: '没有权限删除此试卷' }
|
||
} else if (status === 500) {
|
||
return { success: false, message: '服务器错误,请稍后重试' }
|
||
}
|
||
}
|
||
|
||
return { success: false, message: '删除失败,请检查网络连接' }
|
||
}
|
||
}
|