Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6600b29

Browse files
authored
Merge pull request#304 from Sean-LL/dev_tmp_idcard
feat: 临时身份证 易道渠道
2 parents2e162ed +a694cda commit6600b29

File tree

6 files changed

+237
-15
lines changed

6 files changed

+237
-15
lines changed

‎api/server/src/main/java/com/ke/bella/openapi/endpoints/OcrController.java‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
importcom.ke.bella.openapi.protocol.ocr.OcrProperty;
2020
importcom.ke.bella.openapi.protocol.ocr.OcrRequest;
2121
importcom.ke.bella.openapi.protocol.ocr.residence_permit.ResidencePermitAdaptor;
22+
importcom.ke.bella.openapi.protocol.ocr.tmp_idcard.TmpIdcardAdaptor;
2223
importcom.ke.bella.openapi.service.EndpointDataService;
2324
importcom.ke.bella.openapi.tables.pojos.ChannelDB;
2425
importcom.ke.bella.openapi.utils.JacksonUtils;
@@ -140,6 +141,39 @@ public Object hmtResidencePermit(@RequestBody OcrRequest request) {
140141
returnadaptor.hmtResidencePermit(request,url,property);
141142
}
142143

144+
@SuppressWarnings({"rawtypes","unchecked" })
145+
@PostMapping("/tmp-idcard")
146+
publicObjecttmpIdcard(@RequestBodyOcrRequestrequest) {
147+
// 1. 设置请求上下文
148+
Stringendpoint =EndpointContext.getRequest().getRequestURI();
149+
Stringmodel =request.getModel();
150+
endpointDataService.setEndpointData(endpoint,model,request);
151+
EndpointProcessDataprocessData =EndpointContext.getProcessData();
152+
153+
// 2. 参数校验
154+
validateRequest(request);
155+
156+
// 3. 渠道路由选择
157+
ChannelDBchannel =router.route(endpoint,model,EndpointContext.getApikey(),processData.isMock());
158+
endpointDataService.setChannel(channel);
159+
160+
// 4. 并发限制管理
161+
if(!EndpointContext.getProcessData().isPrivate()) {
162+
limiterManager.incrementConcurrentCount(EndpointContext.getProcessData().getAkCode(),model);
163+
}
164+
165+
// 5. 获取协议适配器
166+
Stringprotocol =processData.getProtocol();
167+
Stringurl =processData.getForwardUrl();
168+
StringchannelInfo =channel.getChannelInfo();
169+
TmpIdcardAdaptoradaptor =adaptorManager.getProtocolAdaptor(endpoint,protocol,TmpIdcardAdaptor.class);
170+
OcrPropertyproperty = (OcrProperty)JacksonUtils.deserialize(channelInfo,adaptor.getPropertyClass());
171+
EndpointContext.setEncodingType(property.getEncodingType());
172+
173+
// 6. 调用适配器处理
174+
returnadaptor.tmpIdcard(request,url,property);
175+
}
176+
143177
privatevoidvalidateRequest(OcrRequestrequest) {
144178
// 校验模型参数
145179
Assert.hasText(request.getModel(),"model参数不能为空");

‎api/server/src/main/java/com/ke/bella/openapi/protocol/ocr/OcrTmpIdcardAdaptor.java‎

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
packagecom.ke.bella.openapi.protocol.ocr.tmp_idcard;
2+
3+
importcom.ke.bella.openapi.protocol.IProtocolAdaptor;
4+
importcom.ke.bella.openapi.protocol.ocr.OcrProperty;
5+
importcom.ke.bella.openapi.protocol.ocr.OcrRequest;
6+
7+
publicinterfaceTmpIdcardAdaptor<TextendsOcrProperty>extendsIProtocolAdaptor {
8+
9+
OcrTmpIdcardResponsetmpIdcard(OcrRequestrequest,Stringurl,Tproperty);
10+
11+
@Override
12+
defaultStringendpoint() {
13+
return"/v1/ocr/tmp-idcard";
14+
}
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
packagecom.ke.bella.openapi.protocol.ocr.tmp_idcard;
2+
3+
importcom.ke.bella.openapi.protocol.ocr.OcrLogHandler;
4+
importorg.springframework.stereotype.Component;
5+
6+
@Component
7+
publicclassTmpIdcardLogHandlerextendsOcrLogHandler {
8+
@Override
9+
publicStringendpoint() {
10+
return"/v1/ocr/tmp-idcard";
11+
}
12+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
packagecom.ke.bella.openapi.protocol.ocr.tmp_idcard;
2+
3+
importorg.springframework.beans.factory.annotation.Autowired;
4+
importorg.springframework.http.HttpStatus;
5+
importorg.springframework.stereotype.Component;
6+
importorg.springframework.util.StringUtils;
7+
8+
importcom.ke.bella.openapi.protocol.OpenapiResponse;
9+
importcom.ke.bella.openapi.protocol.ocr.ImageRetrievalService;
10+
importcom.ke.bella.openapi.protocol.ocr.OcrRequest;
11+
importcom.ke.bella.openapi.protocol.ocr.YidaoBaseResponse;
12+
importcom.ke.bella.openapi.protocol.ocr.YidaoOcrProperty;
13+
importcom.ke.bella.openapi.protocol.ocr.YidaoRequest;
14+
importcom.ke.bella.openapi.protocol.ocr.util.ImageConverter;
15+
importcom.ke.bella.openapi.utils.HttpUtils;
16+
17+
importlombok.extern.slf4j.Slf4j;
18+
importokhttp3.FormBody;
19+
importokhttp3.MediaType;
20+
importokhttp3.MultipartBody;
21+
importokhttp3.Request;
22+
importokhttp3.RequestBody;
23+
24+
@Slf4j
25+
@Component("yidaoTmpIdcard")
26+
publicclassYidaoAdaptorimplementsTmpIdcardAdaptor<YidaoOcrProperty> {
27+
28+
privatestaticfinalintSUCCESS_ERROR_CODE =0;
29+
privatestaticfinalStringYIDAO_OCR_ERROR_TYPE ="YIDAO_OCR_ERROR";
30+
privatestaticfinalStringVALID_DATE_SEPARATOR ="—";// U+2014,Em Dash
31+
32+
33+
@Autowired
34+
privateImageRetrievalServiceimageRetrievalService;
35+
36+
@Override
37+
publicStringgetDescription() {
38+
return"易道OCR临时身份证协议";
39+
}
40+
41+
@Override
42+
publicClass<YidaoOcrProperty>getPropertyClass() {
43+
returnYidaoOcrProperty.class;
44+
}
45+
46+
@Override
47+
publicOcrTmpIdcardResponsetmpIdcard(OcrRequestrequest,Stringurl,YidaoOcrPropertyproperty) {
48+
YidaoRequestyidaoRequest =requestConvert(request,property);
49+
RequesthttpRequest =buildRequest(yidaoRequest,url);
50+
clearLargeData(request,yidaoRequest);
51+
YidaoResponseyidaoResponse =HttpUtils.httpRequest(httpRequest,YidaoResponse.class);
52+
returnresponseConvert(yidaoResponse);
53+
}
54+
55+
privateYidaoRequestrequestConvert(OcrRequestrequest,YidaoOcrPropertyproperty) {
56+
try {
57+
YidaoRequest.YidaoRequestBuilderbuilder =YidaoRequest.builder()
58+
.appKey(property.getAuth().getApiKey())
59+
.appSecret(property.getAuth().getSecret());
60+
61+
if(StringUtils.hasText(request.getImageBase64())) {
62+
StringimageBase64 =ImageConverter.cleanBase64DataHeader(request.getImageBase64());
63+
builder.imageBase64(imageBase64);
64+
}elseif(StringUtils.hasText(request.getImageUrl())) {
65+
builder.imageUrl(request.getImageUrl());
66+
}else {
67+
byte[]imageData =imageRetrievalService.getImageFromFileId(request.getFileId());
68+
builder.imageBinary(imageData);
69+
}
70+
71+
returnbuilder.build();
72+
}catch (Exceptione) {
73+
log.error("Failed to retrieve image data",e);
74+
thrownewIllegalStateException("Failed to retrieve image data",e);
75+
}
76+
}
77+
78+
privateRequestbuildRequest(YidaoRequestrequest,Stringurl) {
79+
if(request.getImageBinary() !=null) {
80+
MultipartBody.BuilderbodyBuilder =newMultipartBody.Builder()
81+
.setType(MultipartBody.FORM)
82+
.addFormDataPart("app_key",request.getAppKey())
83+
.addFormDataPart("app_secret",request.getAppSecret())
84+
.addFormDataPart("image_binary","image.jpg",
85+
RequestBody.create(MediaType.parse("image/jpeg"),request.getImageBinary()));
86+
87+
returnnewRequest.Builder()
88+
.url(url)
89+
.post(bodyBuilder.build())
90+
.build();
91+
}else {
92+
FormBody.BuilderbodyBuilder =newFormBody.Builder()
93+
.add("app_key",request.getAppKey())
94+
.add("app_secret",request.getAppSecret());
95+
96+
if(StringUtils.hasText(request.getImageBase64())) {
97+
bodyBuilder.add("image_base64",request.getImageBase64());
98+
}elseif(StringUtils.hasText(request.getImageUrl())) {
99+
bodyBuilder.add("image_url",request.getImageUrl());
100+
}
101+
102+
returnnewRequest.Builder()
103+
.url(url)
104+
.post(bodyBuilder.build())
105+
.build();
106+
}
107+
}
108+
109+
privateOcrTmpIdcardResponseresponseConvert(YidaoResponseyidaoResponse) {
110+
if(hasError(yidaoResponse)) {
111+
returnbuildErrorResponse(yidaoResponse);
112+
}
113+
114+
OcrTmpIdcardResponseresponse =newOcrTmpIdcardResponse();
115+
response.setRequest_id(yidaoResponse.getRequestId());
116+
117+
YidaoResponse.ResultDataresult =yidaoResponse.getResult();
118+
if(result !=null) {
119+
StringvalidDate =getFieldWords(result.getValid());
120+
String[]dates =parseValidDate(validDate);
121+
122+
OcrTmpIdcardResponse.TmpIdcardDatadata =OcrTmpIdcardResponse.TmpIdcardData.builder()
123+
.name(getFieldWords(result.getName()))
124+
.sex(getFieldWords(result.getGender()))
125+
.nationality(getFieldWords(result.getNationality()))
126+
.birth_date(getFieldWords(result.getBirthdate()))
127+
.address(getFieldWords(result.getAddress()))
128+
.idcard_number(getFieldWords(result.getIdno()))
129+
.issue_authority(getFieldWords(result.getIssued()))
130+
.valid_date_start(dates[0])
131+
.valid_date_end(dates[1])
132+
.build();
133+
response.setData(data);
134+
}
135+
136+
returnresponse;
137+
}
138+
139+
privatebooleanhasError(YidaoResponseyidaoResponse) {
140+
returnyidaoResponse.getErrorCode() ==null ||yidaoResponse.getErrorCode() !=SUCCESS_ERROR_CODE;
141+
}
142+
143+
privateOcrTmpIdcardResponsebuildErrorResponse(YidaoResponseyidaoResponse) {
144+
StringerrorMessage =yidaoResponse.getDescription() !=null
145+
?yidaoResponse.getDescription()
146+
:"Unknown error";
147+
148+
OpenapiResponse.OpenapiErrorerror =OpenapiResponse.OpenapiError.builder()
149+
.code(String.valueOf(yidaoResponse.getErrorCode()))
150+
.message(errorMessage)
151+
.type(YIDAO_OCR_ERROR_TYPE)
152+
.httpCode(HttpStatus.BAD_REQUEST.value())
153+
.build();
154+
155+
returnOcrTmpIdcardResponse.builder().error(error).build();
156+
}
157+
158+
privateStringgetFieldWords(YidaoBaseResponse.FieldDatafieldData) {
159+
returnfieldData !=null ?fieldData.getWords() :null;
160+
}
161+
162+
privateString[]parseValidDate(StringvalidDate) {
163+
if(validDate ==null) {
164+
returnnewString[] {null,null };
165+
}
166+
167+
String[]parts =validDate.split(VALID_DATE_SEPARATOR);
168+
if(parts.length ==2) {
169+
returnnewString[] {parts[0].trim(),parts[1].trim() };
170+
}
171+
172+
returnnewString[] {validDate,null };
173+
}
174+
}

‎api/server/src/main/java/com/ke/bella/openapi/protocol/ocr/tmp_idcard/YidaoResponse.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111
@Data
1212
@EqualsAndHashCode(callSuper =true)
13-
publicclassYidaoResponseextendsYidaoBaseResponse<YidaoResponse.Result> {
13+
publicclassYidaoResponseextendsYidaoBaseResponse<YidaoResponse.ResultData> {
1414
privatestaticfinallongserialVersionUID =1L;
1515

1616
@Data
1717
@JsonInclude(JsonInclude.Include.NON_NULL)
18-
publicstaticclassResultimplementsSerializable {
18+
publicstaticclassResultDataimplementsSerializable {
1919
privatestaticfinallongserialVersionUID =1L;
2020

2121
privateFieldDataname;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp