feat: OCR 服务 + 会议材料模块
ry-ocr/ (新)
本地发票识别微服务 (PaddleOCR 3.x + FastAPI, 8801)
- QR 优先: 扫到二维码即取开票时间/发票号/金额; 没扫到/格式不合法直接判非发票, 不跑 OCR
- 配置 QR_FULL_OCR 控制快路径(false, 0.2s)还是全字段(true, 4.5s)
- /recognize/invoice (multipart) + /recognize/invoice/by-path (本地路径, 白名单) + /recognize/text
- is_invoice / from_qr / qr_raw / qr_error / error_code 字段
- 12 字段发票抽取 (regex + 启发式, 左右主体识别)
- 超时保护 (15s 单页 / 60s 总流程) + PaddleOCR 单例 + ThreadPoolExecutor
ry-api/ruoyi-business/
- pom.xml: 加 hutool-http/json/core 5.8.27, lombok 1.18.30 (OcrClient @Slf4j 所需)
- ocr/: OcrClient + InvoiceResult/Fields/Line + ZipExtractor + InvoiceOcrScheduler
- oss/: OssUploader + OssConfMeta (OCR 识别后重传 OSS)
- config/: OcrConfig + OcrExecutorConfig (后台线程池)
- service/impl/InvoiceOcrService: 后台提交 OCR, ZIP 路径解压识别, 替换场景先清旧
- 会议材料 CRUD 全套 (BizMeetingAuditLog/Executor/Invoice/Material/Supervisor):
controller + service + mapper + domain + xml
ry-vue3/
- MeetingDetail.vue (新建): 会议详情页 (含评分维度章节, 改只读)
- Meetings.vue / OssFileUploader.vue / router / Login.vue: 适配新字段
ry-api/ruoyi-admin/
- RuoYiApplication.java + application.yml: 启用 @Async 异步支持
_self/
- manager_meetings.md / manager_meeting_detail.md: 文档
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
package com.ruoyi.business.ocr;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* ry-ocr Java 调用客户端
|
||||
*
|
||||
* 依赖:hutool-http, hutool-json, hutool-core, lombok
|
||||
*
|
||||
* 用法:
|
||||
* OcrClient client = new OcrClient("http://127.0.0.1:8801");
|
||||
* InvoiceResult r = client.recognize(new File("d:/发票.pdf"));
|
||||
* InvoiceResult r2 = client.recognizeByUrl("https://oss.example.com/xxx.png");
|
||||
* System.out.println(r.getFields().getAmount());
|
||||
*/
|
||||
@Slf4j
|
||||
public class OcrClient {
|
||||
|
||||
private final String baseUrl;
|
||||
|
||||
public OcrClient(String baseUrl) {
|
||||
this.baseUrl = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl;
|
||||
}
|
||||
|
||||
/** 健康检查 */
|
||||
public boolean ping() {
|
||||
try (HttpResponse resp = HttpRequest.get(baseUrl + "/health").timeout(3000).execute()) {
|
||||
return resp.getStatus() == 200 && "ok".equals(JSONUtil.parseObj(resp.body()).getStr("status"));
|
||||
} catch (Exception e) {
|
||||
log.warn("ocr ping failed: {}", e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** 识别发票(图片或 PDF) */
|
||||
public InvoiceResult recognize(File file) {
|
||||
try (HttpResponse resp = HttpRequest.post(baseUrl + "/recognize/invoice")
|
||||
.form("file", file)
|
||||
.timeout(60_000)
|
||||
.execute()) {
|
||||
|
||||
String body = resp.body();
|
||||
JSONObject json = JSONUtil.parseObj(body);
|
||||
if (resp.getStatus() != 200) {
|
||||
throw new RuntimeException("OCR 调用失败: " + resp.getStatus() + " " + body);
|
||||
}
|
||||
return parse(json);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 URL 识别发票: 后端下载 OSS URL 到临时文件 → recognize → 清理临时文件.
|
||||
* 临时文件目录: System.getProperty("java.io.tmpdir")/ry-ocr/
|
||||
*
|
||||
* @param url OSS 可访问 URL
|
||||
* @return 识别结果
|
||||
*/
|
||||
public InvoiceResult recognizeByUrl(String url) {
|
||||
if (url == null || url.isEmpty()) {
|
||||
throw new IllegalArgumentException("ossUrl 不能为空");
|
||||
}
|
||||
File tmpDir = new File(System.getProperty("java.io.tmpdir"), "ry-ocr");
|
||||
if (!tmpDir.exists() && !tmpDir.mkdirs()) {
|
||||
throw new RuntimeException("无法创建临时目录: " + tmpDir.getAbsolutePath());
|
||||
}
|
||||
// 从 URL 截取文件名, 保留后缀 (用于 ry-ocr 推断图片/PDF)
|
||||
String name = url.substring(url.lastIndexOf('/') + 1);
|
||||
if (name.indexOf('?') >= 0) name = name.substring(0, name.indexOf('?'));
|
||||
if (name.indexOf('.') < 0) name = name + ".png";
|
||||
File tmp = new File(tmpDir, System.currentTimeMillis() + "_" + name);
|
||||
try {
|
||||
long size = HttpUtil.downloadFile(url, tmp);
|
||||
if (size <= 0) {
|
||||
throw new RuntimeException("OSS 文件下载失败或为空: " + url);
|
||||
}
|
||||
log.info("OCR 下载: url={} size={}B tmp={}", url, size, tmp.getAbsolutePath());
|
||||
return recognize(tmp);
|
||||
} finally {
|
||||
FileUtil.del(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
private InvoiceResult parse(JSONObject json) {
|
||||
InvoiceResult r = new InvoiceResult();
|
||||
r.setSuccess(json.getBool("success", false));
|
||||
r.setRawText(json.getStr("rawText", ""));
|
||||
r.setEngine(json.getStr("engine", ""));
|
||||
r.setPageCount(json.getInt("pageCount", 1));
|
||||
r.setElapsedMs(json.getInt("elapsedMs", 0));
|
||||
r.setError(json.getStr("error"));
|
||||
|
||||
JSONObject f = json.getJSONObject("fields");
|
||||
if (f != null) {
|
||||
InvoiceFields fields = new InvoiceFields();
|
||||
fields.setInvoiceType(f.getStr("invoiceType"));
|
||||
fields.setInvoiceNo(f.getStr("invoiceNo"));
|
||||
fields.setInvoiceCode(f.getStr("invoiceCode"));
|
||||
fields.setInvoiceDate(f.getStr("invoiceDate"));
|
||||
fields.setAmount(f.getDouble("amount"));
|
||||
fields.setAmountCn(f.getStr("amountCn"));
|
||||
fields.setAmountPretax(f.getDouble("amount_pretax"));
|
||||
fields.setTaxAmount(f.getDouble("taxAmount"));
|
||||
fields.setSellerName(f.getStr("sellerName"));
|
||||
fields.setSellerTaxNo(f.getStr("sellerTaxNo"));
|
||||
fields.setBuyerName(f.getStr("buyerName"));
|
||||
fields.setBuyerTaxNo(f.getStr("buyerTaxNo"));
|
||||
fields.setAmountMatch(f.getBool("amountMatch"));
|
||||
r.setFields(fields);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user