package com.ruoyi.lsfx.controller; import com.ruoyi.common.annotation.Anonymous; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.lsfx.client.CreditParseClient; import com.ruoyi.lsfx.domain.response.CreditParseResponse; import com.ruoyi.lsfx.exception.LsfxApiException; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; @Tag(name = "征信解析接口测试", description = "用于测试征信解析接口") @Anonymous @RestController @RequestMapping("/lsfx/credit") public class CreditParseController { private static final String DEFAULT_MODEL = "LXCUSTALL"; private static final String DEFAULT_HTYPE = "PERSON"; @Resource private CreditParseClient creditParseClient; @Operation(summary = "解析征信HTML", description = "上传征信HTML文件并调用外部解析服务") @PostMapping("/parse") public AjaxResult parse(@Parameter(description = "征信HTML文件") @RequestParam("file") MultipartFile file, @Parameter(description = "解析模型,默认LXCUSTALL") @RequestParam(required = false) String model, @Parameter(description = "主体类型,默认PERSON") @RequestParam(required = false) String hType) { if (file == null || file.isEmpty()) { return AjaxResult.error("征信HTML文件不能为空"); } String originalFilename = file.getOriginalFilename(); if (StringUtils.isBlank(originalFilename)) { return AjaxResult.error("文件名不能为空"); } String lowerCaseName = originalFilename.toLowerCase(); if (!lowerCaseName.endsWith(".html") && !lowerCaseName.endsWith(".htm")) { return AjaxResult.error("仅支持 HTML 格式文件"); } String actualModel = StringUtils.isBlank(model) ? DEFAULT_MODEL : model; String actualHType = StringUtils.isBlank(hType) ? DEFAULT_HTYPE : hType; Path tempFile = null; try { String suffix = lowerCaseName.endsWith(".htm") ? ".htm" : ".html"; tempFile = Files.createTempFile("credit_parse_", suffix); Files.copy(file.getInputStream(), tempFile, StandardCopyOption.REPLACE_EXISTING); File convertedFile = tempFile.toFile(); CreditParseResponse response = creditParseClient.parse(actualModel, actualHType, convertedFile); return AjaxResult.success(response); } catch (LsfxApiException e) { return AjaxResult.error(e.getMessage()); } catch (IOException e) { return AjaxResult.error("文件转换失败:" + e.getMessage()); } catch (Exception e) { return AjaxResult.error("征信解析失败:" + e.getMessage()); } finally { if (tempFile != null) { try { Files.deleteIfExists(tempFile); } catch (IOException ignored) { // 忽略临时文件删除失败,避免影响主流程返回 } } } } }