Skip to content

Commit 5e0d2a0

Browse files
committed
refactor(api): 统一响应格式并优化错误处理
- 替换手动构造 ApiResponse 为统一的 success 和 error 方法调用 - 使用 ApiResponse::success_with_count 和 ApiResponse::success 替代重复代码 - 错误响应统一使用 ApiResponse::error_with_code 明确错误码 - 移除对 ApiError 的直接依赖,保持响应结构一致性 - 增强前端 JS 中对 API 响应的解析和错误提示逻辑 - 更新分页查询接口注释以准确描述功能 - 简化 DTO 删除接口为基于条件的批量删除方式 - 修正 JS 中事件监听器重复绑定的问题 - 调整部分字段命名以避免关键字冲突(如 Type -> Type_) - 完善 JS 表单交互逻辑提升用户体验和健壮性
1 parent 3fde81c commit 5e0d2a0

14 files changed

Lines changed: 696 additions & 957 deletions

File tree

file_classification_webapi/src/bin/handlers/file_groups.rs

Lines changed: 21 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,11 @@ async fn api_list_file_groups_by_filter(
3232
Ok(file_groups) => {
3333
let count = file_groups.len();
3434
// 构造成功响应
35-
Ok(HttpResponse::Ok().json(ApiResponse {
36-
success: true,
37-
data: Some(file_groups),
38-
message: None,
39-
count: Some(count),
40-
}))
35+
Ok(HttpResponse::Ok().json(ApiResponse::success_with_count(file_groups, count)))
4136
}
4237
// 错误处理
4338
Err(e) => Ok(
44-
HttpResponse::InternalServerError().json(ApiError { success: false, message: e.to_string() }),
39+
HttpResponse::InternalServerError().json(ApiResponse::<()>::error_with_code("INTERNAL_ERROR", &e.to_string())),
4540
),
4641
}
4742
}
@@ -66,17 +61,12 @@ async fn api_list_file_groups_by_conditions(
6661
let count = file_groups.len();
6762

6863
// 构造成功的响应对象并返回
69-
Ok(HttpResponse::Ok().json(ApiResponse {
70-
success: true,
71-
data: Some(file_groups),
72-
message: None,
73-
count: Some(count),
74-
}))
64+
Ok(HttpResponse::Ok().json(ApiResponse::success_with_count(file_groups, count)))
7565
}
7666

7767
// 如果出现错误,则构造失败的响应对象并返回
7868
Err(e) => Ok(
79-
HttpResponse::InternalServerError().json(ApiError { success: false, message: e.to_string() }),
69+
HttpResponse::InternalServerError().json(ApiResponse::<()>::error_with_code("INTERNAL_ERROR", &e.to_string())),
8070
),
8171
}
8272
}
@@ -106,17 +96,12 @@ async fn api_list_file_groups_by_conditions_with_options(
10696
let count = file_groups.len();
10797

10898
// 返回成功响应
109-
Ok(HttpResponse::Ok().json(ApiResponse {
110-
success: true,
111-
data: Some(file_groups),
112-
message: None,
113-
count: Some(count),
114-
}))
99+
Ok(HttpResponse::Ok().json(ApiResponse::success_with_count(file_groups, count)))
115100
}
116101

117102
// 处理错误情况
118103
Err(e) => Ok(
119-
HttpResponse::InternalServerError().json(ApiError { success: false, message: e.to_string() }),
104+
HttpResponse::InternalServerError().json(ApiResponse::<()>::error_with_code("INTERNAL_ERROR", &e.to_string())),
120105
),
121106
}
122107
}
@@ -143,17 +128,12 @@ async fn api_list_file_groups_by_filter_with_options(
143128
let count = file_groups.len();
144129

145130
// 返回成功响应
146-
Ok(HttpResponse::Ok().json(ApiResponse {
147-
success: true,
148-
data: Some(file_groups),
149-
message: None,
150-
count: Some(count),
151-
}))
131+
Ok(HttpResponse::Ok().json(ApiResponse::success_with_count(file_groups, count)))
152132
}
153133

154134
// 处理错误情况
155135
Err(e) => Ok(
156-
HttpResponse::InternalServerError().json(ApiError { success: false, message: e.to_string() }),
136+
HttpResponse::InternalServerError().json(ApiResponse::<()>::error_with_code("INTERNAL_ERROR", &e.to_string())),
157137
),
158138
}
159139
}
@@ -187,24 +167,19 @@ async fn api_list_file_groups_by_filter_with_pagination(
187167
match select_file_groups_by_filter_with_pagination(&mut conn, filter, options) {
188168
Ok(result) => {
189169
// 返回成功响应
190-
Ok(HttpResponse::Ok().json(ApiResponse {
191-
success: true,
192-
data: Some(result.clone()),
193-
message: None,
194-
count: Some(result.data.len()),
195-
}))
170+
Ok(HttpResponse::Ok().json(ApiResponse::success(result.clone())))
196171
}
197172

198173
// 处理错误情况
199174
Err(e) => Ok(
200-
HttpResponse::InternalServerError().json(ApiError { success: false, message: e.to_string() }),
175+
HttpResponse::InternalServerError().json(ApiResponse::<()>::error_with_code("INTERNAL_ERROR", &e.to_string())),
201176
),
202177
}
203178
}
204179

205180
/// 根据条件分页搜索文件组
206181
///
207-
/// 此接口允许客户端传递条件和分页参数,以分页方式检索数据
182+
/// 此接口允许客户端传递分页参数,以分页方式检索数据
208183
///
209184
/// 请求路径: GET /api/file-groups/search/by-conditions-with-pagination
210185
#[get("/api/file-groups/search/by-conditions-with-pagination")]
@@ -227,17 +202,12 @@ async fn api_list_file_groups_by_conditions_with_pagination(
227202
match select_file_groups_by_conditions_with_pagination(&mut conn, conditions, options) {
228203
Ok(result) => {
229204
// 返回成功响应
230-
Ok(HttpResponse::Ok().json(ApiResponse {
231-
success: true,
232-
data: Some(result.clone()),
233-
message: None,
234-
count: Some(result.data.len()),
235-
}))
205+
Ok(HttpResponse::Ok().json(ApiResponse::success(result.clone())))
236206
}
237207

238208
// 处理错误情况
239209
Err(e) => Ok(
240-
HttpResponse::InternalServerError().json(ApiError { success: false, message: e.to_string() }),
210+
HttpResponse::InternalServerError().json(ApiResponse::<()>::error_with_code("INTERNAL_ERROR", &e.to_string())),
241211
),
242212
}
243213
}
@@ -260,15 +230,12 @@ async fn api_create_file_group(
260230
Ok(_) =>
261231
// 成功时返回 Created 状态码及提示信息
262232
{
263-
Ok(HttpResponse::Created().json(json!({
264-
"success": true,
265-
"message": "文件组关联创建成功"
266-
})))
233+
Ok(HttpResponse::Created().json(ApiResponse::success_with_msg((), "文件组关联创建成功")))
267234
}
268235

269236
// 错误处理
270237
Err(e) => Ok(
271-
HttpResponse::InternalServerError().json(ApiError { success: false, message: e.to_string() }),
238+
HttpResponse::InternalServerError().json(ApiResponse::<()>::error_with_code("CREATE_FILE_GROUP_FAILED", &e.to_string())),
272239
),
273240
}
274241
}
@@ -291,15 +258,12 @@ async fn api_delete_file_group(
291258
Ok(_) =>
292259
// 成功时返回 OK 状态码及确认消息
293260
{
294-
Ok(HttpResponse::Ok().json(json!({
295-
"success": true,
296-
"message": "文件组关联删除成功"
297-
})))
261+
Ok(HttpResponse::Ok().json(ApiResponse::success_with_msg((), "文件组关联删除成功")))
298262
}
299263

300264
// 错误处理
301265
Err(e) => Ok(
302-
HttpResponse::InternalServerError().json(ApiError { success: false, message: e.to_string() }),
266+
HttpResponse::InternalServerError().json(ApiResponse::<()>::error_with_code("DELETE_FILE_GROUP_FAILED", &e.to_string())),
303267
),
304268
}
305269
}
@@ -322,16 +286,12 @@ async fn api_delete_file_groups_by_conditions(
322286
Ok(count) =>
323287
// 成功时返回删除条目数量
324288
{
325-
Ok(HttpResponse::Ok().json(json!({
326-
"success": true,
327-
"message": format!("成功删除 {} 条记录", count),
328-
"count": count
329-
})))
289+
Ok(HttpResponse::Ok().json(ApiResponse::success_with_msg(count, &format!("成功删除 {} 条记录", count))))
330290
}
331291

332292
// 错误处理
333293
Err(e) => Ok(
334-
HttpResponse::InternalServerError().json(ApiError { success: false, message: e.to_string() }),
294+
HttpResponse::InternalServerError().json(ApiResponse::<()>::error_with_code("DELETE_FILE_GROUP_FAILED", &e.to_string())),
335295
),
336296
}
337297
}
@@ -354,15 +314,11 @@ async fn api_delete_file_groups_by_dtos(
354314
Ok(count) =>
355315
// 构造成功响应,包含删除记录数
356316
{
357-
Ok(HttpResponse::Ok().json(json!({
358-
"success": true,
359-
"message": format!("成功删除 {} 条记录", count),
360-
"count": count
361-
})))
317+
Ok(HttpResponse::Ok().json(ApiResponse::success_with_msg(count, &format!("成功删除 {} 条记录", count))))
362318
}
363319
// 错误处理
364320
Err(e) => Ok(
365-
HttpResponse::InternalServerError().json(ApiError { success: false, message: e.to_string() }),
321+
HttpResponse::InternalServerError().json(ApiResponse::<()>::error_with_code("DELETE_FILE_GROUP_FAILED", &e.to_string())),
366322
),
367323
}
368324
}

0 commit comments

Comments
 (0)