优化代码
This commit is contained in:
@@ -17,7 +17,7 @@ public class BillImportController(
|
||||
/// <param name="type">账单类型(Alipay | WeChat)</param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<BaseResponse<object>> UploadFile(
|
||||
public async Task<BaseResponse> UploadFile(
|
||||
[FromForm] IFormFile file,
|
||||
[FromForm] string type
|
||||
)
|
||||
@@ -27,12 +27,12 @@ public class BillImportController(
|
||||
// 验证参数
|
||||
if (file.Length == 0)
|
||||
{
|
||||
return BaseResponse<object>.Fail("请选择要上传的文件");
|
||||
return "请选择要上传的文件".Fail();
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(type) || (type != "Alipay" && type != "WeChat"))
|
||||
{
|
||||
return BaseResponse<object>.Fail("账单类型参数错误,必须是 Alipay 或 WeChat");
|
||||
return "账单类型参数错误,必须是 Alipay 或 WeChat".Fail();
|
||||
}
|
||||
|
||||
// 验证文件类型
|
||||
@@ -40,14 +40,14 @@ public class BillImportController(
|
||||
var fileExtension = Path.GetExtension(file.FileName).ToLowerInvariant();
|
||||
if (!allowedExtensions.Contains(fileExtension))
|
||||
{
|
||||
return BaseResponse<object>.Fail("只支持 CSV 或 Excel 文件格式");
|
||||
return "只支持 CSV 或 Excel 文件格式".Fail();
|
||||
}
|
||||
|
||||
// 验证文件大小(10MB限制)
|
||||
const long maxFileSize = 10 * 1024 * 1024;
|
||||
if (file.Length > maxFileSize)
|
||||
{
|
||||
return BaseResponse<object>.Fail("文件大小不能超过 10MB");
|
||||
return "文件大小不能超过 10MB".Fail();
|
||||
}
|
||||
|
||||
// 生成唯一文件名
|
||||
@@ -69,16 +69,12 @@ public class BillImportController(
|
||||
}
|
||||
}
|
||||
|
||||
return new BaseResponse<object>
|
||||
{
|
||||
Success = ok,
|
||||
Message = message
|
||||
};
|
||||
return message.Ok();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "文件上传失败,类型: {Type}", type);
|
||||
return BaseResponse<object>.Fail($"文件上传失败: {ex.Message}");
|
||||
return $"文件上传失败: {ex.Message}".Fail();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user