refactor(api): migrate expression-api to ApiResponse pattern
- Migrated all 11 functions in expression-api.ts to return Promise<ApiResponse<T>> - Implemented manual response handling following person-api pattern - Properly unwrap nested API responses and re-wrap in ApiResponse - Updated all 16 call sites across 4 files with proper error handling - Fixed type annotations (ChatInfo) in expression.tsx - Build passes successfully with no TypeScript errors - Follows AGENTS.md import conventions and Wave 2 constraints - All HTTP and API-level errors handled consistently via ApiResponse
This commit is contained in:
@@ -161,9 +161,9 @@ function IndexPageContent() {
|
||||
// 获取审核统计
|
||||
const fetchReviewStats = useCallback(async () => {
|
||||
try {
|
||||
const data = await getReviewStats()
|
||||
if (isMountedRef.current) {
|
||||
setUncheckedCount(data.unchecked)
|
||||
const result = await getReviewStats()
|
||||
if (result.success && isMountedRef.current) {
|
||||
setUncheckedCount(result.data.unchecked)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取审核统计失败:', error)
|
||||
|
||||
@@ -16,16 +16,16 @@ export function useChatNameMap() {
|
||||
const loadChatNameMap = useCallback(async () => {
|
||||
try {
|
||||
setLoading(true)
|
||||
const response = await getChatList()
|
||||
if (response?.data) {
|
||||
const result = await getChatList()
|
||||
if (result.success) {
|
||||
const nameMap = new Map<string, string>()
|
||||
response.data.forEach((chat: ChatInfo) => {
|
||||
result.data.forEach((chat: ChatInfo) => {
|
||||
nameMap.set(chat.chat_id, chat.chat_name)
|
||||
})
|
||||
setChatNameMap(nameMap)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载聊天列表失败:', error)
|
||||
console.error('加载聚天列表失败:', error)
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
|
||||
@@ -68,15 +68,18 @@ export function PersonManagementPage() {
|
||||
const loadPersons = async () => {
|
||||
try {
|
||||
setLoading(true)
|
||||
const response = await getPersonList({
|
||||
const result = await getPersonList({
|
||||
page,
|
||||
page_size: pageSize,
|
||||
search: search || undefined,
|
||||
is_known: filterKnown,
|
||||
platform: filterPlatform,
|
||||
})
|
||||
setPersons(response.data)
|
||||
setTotal(response.total)
|
||||
if (!result.success) {
|
||||
throw new Error(result.error)
|
||||
}
|
||||
setPersons(result.data.data)
|
||||
setTotal(result.data.total)
|
||||
} catch (error) {
|
||||
toast({
|
||||
title: '加载失败',
|
||||
@@ -91,9 +94,9 @@ export function PersonManagementPage() {
|
||||
// 加载统计数据
|
||||
const loadStats = async () => {
|
||||
try {
|
||||
const response = await getPersonStats()
|
||||
if (response?.data) {
|
||||
setStats(response.data)
|
||||
const result = await getPersonStats()
|
||||
if (result.success) {
|
||||
setStats(result.data)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载统计数据失败:', error)
|
||||
@@ -110,8 +113,11 @@ export function PersonManagementPage() {
|
||||
// 查看详情
|
||||
const handleViewDetail = async (person: PersonInfo) => {
|
||||
try {
|
||||
const response = await getPersonDetail(person.person_id)
|
||||
setSelectedPerson(response.data)
|
||||
const result = await getPersonDetail(person.person_id)
|
||||
if (!result.success) {
|
||||
throw new Error(result.error)
|
||||
}
|
||||
setSelectedPerson(result.data)
|
||||
setIsDetailDialogOpen(true)
|
||||
} catch (error) {
|
||||
toast({
|
||||
@@ -131,7 +137,10 @@ export function PersonManagementPage() {
|
||||
// 删除人物
|
||||
const handleDelete = async (person: PersonInfo) => {
|
||||
try {
|
||||
await deletePerson(person.person_id)
|
||||
const result = await deletePerson(person.person_id)
|
||||
if (!result.success) {
|
||||
throw new Error(result.error)
|
||||
}
|
||||
toast({
|
||||
title: '删除成功',
|
||||
description: `已删除人物信息: ${person.person_name || person.nickname || person.user_id}`,
|
||||
@@ -190,9 +199,12 @@ export function PersonManagementPage() {
|
||||
const handleBatchDelete = async () => {
|
||||
try {
|
||||
const result = await batchDeletePersons(Array.from(selectedPersons))
|
||||
if (!result.success) {
|
||||
throw new Error(result.error)
|
||||
}
|
||||
toast({
|
||||
title: '批量删除完成',
|
||||
description: result.message,
|
||||
description: result.data.message,
|
||||
})
|
||||
setSelectedPersons(new Set())
|
||||
setBatchDeleteDialogOpen(false)
|
||||
@@ -858,7 +870,10 @@ function PersonEditDialog({
|
||||
|
||||
try {
|
||||
setSaving(true)
|
||||
await updatePerson(person.person_id, formData)
|
||||
const result = await updatePerson(person.person_id, formData)
|
||||
if (!result.success) {
|
||||
throw new Error(result.error)
|
||||
}
|
||||
toast({
|
||||
title: '保存成功',
|
||||
description: '人物信息已更新',
|
||||
|
||||
@@ -72,13 +72,21 @@ export function ExpressionManagementPage() {
|
||||
const loadExpressions = async () => {
|
||||
try {
|
||||
setLoading(true)
|
||||
const response = await getExpressionList({
|
||||
const result = await getExpressionList({
|
||||
page,
|
||||
page_size: pageSize,
|
||||
search: search || undefined,
|
||||
})
|
||||
setExpressions(response.data)
|
||||
setTotal(response.total)
|
||||
if (result.success) {
|
||||
setExpressions(result.data.data)
|
||||
setTotal(result.data.total)
|
||||
} else {
|
||||
toast({
|
||||
title: '加载失败',
|
||||
description: result.error,
|
||||
variant: 'destructive',
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
toast({
|
||||
title: '加载失败',
|
||||
@@ -93,9 +101,11 @@ export function ExpressionManagementPage() {
|
||||
// 加载统计数据
|
||||
const loadStats = async () => {
|
||||
try {
|
||||
const response = await getExpressionStats()
|
||||
if (response?.data) {
|
||||
setStats(response.data)
|
||||
const result = await getExpressionStats()
|
||||
if (result.success) {
|
||||
setStats(result.data)
|
||||
} else {
|
||||
console.error('加载统计数据失败:', result.error)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载统计数据失败:', error)
|
||||
@@ -105,28 +115,30 @@ export function ExpressionManagementPage() {
|
||||
// 加载审核统计
|
||||
const loadReviewStats = async () => {
|
||||
try {
|
||||
const data = await getReviewStats()
|
||||
setUncheckedCount(data.unchecked)
|
||||
const result = await getReviewStats()
|
||||
if (result.success) {
|
||||
setUncheckedCount(result.data.unchecked)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载审核统计失败:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// 加载聊天列表
|
||||
// 加载聚天列表
|
||||
const loadChatList = async () => {
|
||||
try {
|
||||
const response = await getChatList()
|
||||
if (response?.data) {
|
||||
setChatList(response.data)
|
||||
// 构建聊天ID到名称的映射
|
||||
const result = await getChatList()
|
||||
if (result.success) {
|
||||
setChatList(result.data)
|
||||
// 构建聚天ID到名称的映射
|
||||
const nameMap = new Map<string, string>()
|
||||
response.data.forEach((chat) => {
|
||||
result.data.forEach((chat: ChatInfo) => {
|
||||
nameMap.set(chat.chat_id, chat.chat_name)
|
||||
})
|
||||
setChatNameMap(nameMap)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载聊天列表失败:', error)
|
||||
console.error('加载聚天列表失败:', error)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,9 +159,17 @@ export function ExpressionManagementPage() {
|
||||
// 查看详情
|
||||
const handleViewDetail = async (expression: Expression) => {
|
||||
try {
|
||||
const response = await getExpressionDetail(expression.id)
|
||||
setSelectedExpression(response.data)
|
||||
setIsDetailDialogOpen(true)
|
||||
const result = await getExpressionDetail(expression.id)
|
||||
if (result.success) {
|
||||
setSelectedExpression(result.data)
|
||||
setIsDetailDialogOpen(true)
|
||||
} else {
|
||||
toast({
|
||||
title: '加载详情失败',
|
||||
description: result.error,
|
||||
variant: 'destructive',
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
toast({
|
||||
title: '加载详情失败',
|
||||
@@ -168,14 +188,22 @@ export function ExpressionManagementPage() {
|
||||
// 删除表达方式
|
||||
const handleDelete = async (expression: Expression) => {
|
||||
try {
|
||||
await deleteExpression(expression.id)
|
||||
toast({
|
||||
title: '删除成功',
|
||||
description: `已删除表达方式: ${expression.situation}`,
|
||||
})
|
||||
setDeleteConfirmExpression(null)
|
||||
loadExpressions()
|
||||
loadStats()
|
||||
const result = await deleteExpression(expression.id)
|
||||
if (result.success) {
|
||||
toast({
|
||||
title: '删除成功',
|
||||
description: `已删除表达方式: ${expression.situation}`,
|
||||
})
|
||||
setDeleteConfirmExpression(null)
|
||||
loadExpressions()
|
||||
loadStats()
|
||||
} else {
|
||||
toast({
|
||||
title: '删除失败',
|
||||
description: result.error,
|
||||
variant: 'destructive',
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
toast({
|
||||
title: '删除失败',
|
||||
@@ -208,15 +236,23 @@ export function ExpressionManagementPage() {
|
||||
// 批量删除
|
||||
const handleBatchDelete = async () => {
|
||||
try {
|
||||
await batchDeleteExpressions(Array.from(selectedIds))
|
||||
toast({
|
||||
title: '批量删除成功',
|
||||
description: `已删除 ${selectedIds.size} 个表达方式`,
|
||||
})
|
||||
setSelectedIds(new Set())
|
||||
setIsBatchDeleteDialogOpen(false)
|
||||
loadExpressions()
|
||||
loadStats()
|
||||
const result = await batchDeleteExpressions(Array.from(selectedIds))
|
||||
if (result.success) {
|
||||
toast({
|
||||
title: '批量删除成功',
|
||||
description: `已删除 ${selectedIds.size} 个表达方式`,
|
||||
})
|
||||
setSelectedIds(new Set())
|
||||
setIsBatchDeleteDialogOpen(false)
|
||||
loadExpressions()
|
||||
loadStats()
|
||||
} else {
|
||||
toast({
|
||||
title: '批量删除失败',
|
||||
description: result.error,
|
||||
variant: 'destructive',
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
toast({
|
||||
title: '批量删除失败',
|
||||
@@ -848,7 +884,7 @@ function ExpressionCreateDialog({
|
||||
if (!formData.situation || !formData.style || !formData.chat_id) {
|
||||
toast({
|
||||
title: '验证失败',
|
||||
description: '请填写必填字段:情境、风格和聊天',
|
||||
description: '请填写必填字段:情境、风格和聚天',
|
||||
variant: 'destructive',
|
||||
})
|
||||
return
|
||||
@@ -856,18 +892,26 @@ function ExpressionCreateDialog({
|
||||
|
||||
try {
|
||||
setSaving(true)
|
||||
await createExpression(formData)
|
||||
toast({
|
||||
title: '创建成功',
|
||||
description: '表达方式已创建',
|
||||
})
|
||||
// 重置表单
|
||||
setFormData({
|
||||
situation: '',
|
||||
style: '',
|
||||
chat_id: '',
|
||||
})
|
||||
onSuccess()
|
||||
const result = await createExpression(formData)
|
||||
if (result.success) {
|
||||
toast({
|
||||
title: '创建成功',
|
||||
description: '表达方式已创建',
|
||||
})
|
||||
// 重置表单
|
||||
setFormData({
|
||||
situation: '',
|
||||
style: '',
|
||||
chat_id: '',
|
||||
})
|
||||
onSuccess()
|
||||
} else {
|
||||
toast({
|
||||
title: '创建失败',
|
||||
description: result.error,
|
||||
variant: 'destructive',
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
toast({
|
||||
title: '创建失败',
|
||||
@@ -988,12 +1032,20 @@ function ExpressionEditDialog({
|
||||
|
||||
try {
|
||||
setSaving(true)
|
||||
await updateExpression(expression.id, formData)
|
||||
toast({
|
||||
title: '保存成功',
|
||||
description: '表达方式已更新',
|
||||
})
|
||||
onSuccess()
|
||||
const result = await updateExpression(expression.id, formData)
|
||||
if (result.success) {
|
||||
toast({
|
||||
title: '保存成功',
|
||||
description: '表达方式已更新',
|
||||
})
|
||||
onSuccess()
|
||||
} else {
|
||||
toast({
|
||||
title: '保存失败',
|
||||
description: result.error,
|
||||
variant: 'destructive',
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
toast({
|
||||
title: '保存失败',
|
||||
|
||||
Reference in New Issue
Block a user