|
| 1 | +packagecom.ke.bella.openapi.protocol.ocr.residence_permit; |
| 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.YidaoOcrProperty; |
| 12 | +importcom.ke.bella.openapi.protocol.ocr.YidaoRequest; |
| 13 | +importcom.ke.bella.openapi.protocol.ocr.residencepermit.OcrResidencePermitResponse; |
| 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("yidaoResidencePermit") |
| 26 | +publicclassYidaoAdaptorimplementsResidencePermitAdaptor<YidaoOcrProperty> { |
| 27 | + |
| 28 | +privatestaticfinalStringYIDAO_OCR_ERROR_TYPE ="YIDAO_OCR_ERROR"; |
| 29 | +privatestaticfinalintSUCCESS_CODE =0; |
| 30 | +privatestaticfinalStringVALID_DATE_SEPARATOR ="-";// U+002D,Hyphen-Minus |
| 31 | + |
| 32 | +@Autowired |
| 33 | +privateImageRetrievalServiceimageRetrievalService; |
| 34 | + |
| 35 | +@Override |
| 36 | +publicStringgetDescription() { |
| 37 | +return"易道OCR港澳台居民居住证识别协议"; |
| 38 | + } |
| 39 | + |
| 40 | +@Override |
| 41 | +publicClass<YidaoOcrProperty>getPropertyClass() { |
| 42 | +returnYidaoOcrProperty.class; |
| 43 | + } |
| 44 | + |
| 45 | +@Override |
| 46 | +publicOcrResidencePermitResponsehmtResidencePermit(OcrRequestrequest,Stringurl,YidaoOcrPropertyproperty) { |
| 47 | +YidaoRequestyidaoRequest =requestConvert(request,property); |
| 48 | +RequesthttpRequest =buildRequest(yidaoRequest,url); |
| 49 | +clearLargeData(request,yidaoRequest); |
| 50 | +YidaoResponseyidaoResponse =HttpUtils.httpRequest(httpRequest,YidaoResponse.class); |
| 51 | +returnresponseConvert(yidaoResponse); |
| 52 | + } |
| 53 | + |
| 54 | +privateYidaoRequestrequestConvert(OcrRequestrequest,YidaoOcrPropertyproperty) { |
| 55 | +try { |
| 56 | +YidaoRequest.YidaoRequestBuilderbuilder =YidaoRequest.builder() |
| 57 | + .appKey(property.getAuth().getApiKey()) |
| 58 | + .appSecret(property.getAuth().getSecret()); |
| 59 | + |
| 60 | +if(StringUtils.hasText(request.getImageBase64())) { |
| 61 | +StringimageBase64 =ImageConverter.cleanBase64DataHeader(request.getImageBase64()); |
| 62 | +builder.imageBase64(imageBase64); |
| 63 | + }elseif(StringUtils.hasText(request.getImageUrl())) { |
| 64 | +builder.imageUrl(request.getImageUrl()); |
| 65 | + }else { |
| 66 | +byte[]imageData =imageRetrievalService.getImageFromFileId(request.getFileId()); |
| 67 | +builder.imageBinary(imageData); |
| 68 | + } |
| 69 | + |
| 70 | +returnbuilder.build(); |
| 71 | + }catch (Exceptione) { |
| 72 | +log.error("Failed to retrieve image data",e); |
| 73 | +thrownewIllegalStateException("Failed to retrieve image data",e); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | +privateRequestbuildRequest(YidaoRequestrequest,Stringurl) { |
| 78 | +if(request.getImageBinary() !=null) { |
| 79 | +MultipartBody.BuilderbodyBuilder =newMultipartBody.Builder() |
| 80 | + .setType(MultipartBody.FORM) |
| 81 | + .addFormDataPart("app_key",request.getAppKey()) |
| 82 | + .addFormDataPart("app_secret",request.getAppSecret()) |
| 83 | + .addFormDataPart("image_binary","image.jpg", |
| 84 | +RequestBody.create(MediaType.parse("image/jpeg"),request.getImageBinary())); |
| 85 | + |
| 86 | +returnnewRequest.Builder() |
| 87 | + .url(url) |
| 88 | + .post(bodyBuilder.build()) |
| 89 | + .build(); |
| 90 | + }else { |
| 91 | +FormBody.BuilderbodyBuilder =newFormBody.Builder() |
| 92 | + .add("app_key",request.getAppKey()) |
| 93 | + .add("app_secret",request.getAppSecret()); |
| 94 | + |
| 95 | +if(StringUtils.hasText(request.getImageBase64())) { |
| 96 | +bodyBuilder.add("image_base64",request.getImageBase64()); |
| 97 | + }elseif(StringUtils.hasText(request.getImageUrl())) { |
| 98 | +bodyBuilder.add("image_url",request.getImageUrl()); |
| 99 | + } |
| 100 | + |
| 101 | +returnnewRequest.Builder() |
| 102 | + .url(url) |
| 103 | + .post(bodyBuilder.build()) |
| 104 | + .build(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | +privateOcrResidencePermitResponseresponseConvert(YidaoResponseyidaoResponse) { |
| 109 | +if(yidaoResponse.getErrorCode() ==null ||yidaoResponse.getErrorCode() !=SUCCESS_CODE) { |
| 110 | +returnbuildErrorResponse(yidaoResponse); |
| 111 | + } |
| 112 | + |
| 113 | +YidaoResponse.ResultDataresult =yidaoResponse.getResult(); |
| 114 | +if(result ==null) { |
| 115 | +returnbuildErrorResponse(yidaoResponse); |
| 116 | + } |
| 117 | + |
| 118 | +OcrResidencePermitResponse.ResidencePermitSideside =determineSide(result); |
| 119 | +Objectdata =side ==OcrResidencePermitResponse.ResidencePermitSide.PORTRAIT |
| 120 | + ?extractPortraitData(result) |
| 121 | + :extractNationalEmblemData(result); |
| 122 | + |
| 123 | +returnOcrResidencePermitResponse.builder() |
| 124 | + .request_id(yidaoResponse.getRequestId()) |
| 125 | + .side(side) |
| 126 | + .data(data) |
| 127 | + .build(); |
| 128 | + } |
| 129 | + |
| 130 | +privateOcrResidencePermitResponse.ResidencePermitSidedetermineSide(YidaoResponse.ResultDataresult) { |
| 131 | +if(result.getName() !=null ||result.getGender() !=null ||result.getBirthdate() !=null) { |
| 132 | +returnOcrResidencePermitResponse.ResidencePermitSide.PORTRAIT; |
| 133 | + } |
| 134 | +returnOcrResidencePermitResponse.ResidencePermitSide.NATIONAL_EMBLEM; |
| 135 | + } |
| 136 | + |
| 137 | +privateOcrResidencePermitResponse.PortraitDataextractPortraitData(YidaoResponse.ResultDataresult) { |
| 138 | +returnOcrResidencePermitResponse.PortraitData.builder() |
| 139 | + .name(getFieldWords(result.getName())) |
| 140 | + .sex(getFieldWords(result.getGender())) |
| 141 | + .birth_date(getFieldWords(result.getBirthdate())) |
| 142 | + .address(getFieldWords(result.getAddress())) |
| 143 | + .idcard_number(getFieldWords(result.getIdno())) |
| 144 | + .build(); |
| 145 | + } |
| 146 | + |
| 147 | +privateOcrResidencePermitResponse.NationalEmblemDataextractNationalEmblemData(YidaoResponse.ResultDataresult) { |
| 148 | +StringvalidDate =getFieldWords(result.getValid()); |
| 149 | +String[]dates =parseValidDate(validDate); |
| 150 | + |
| 151 | +returnOcrResidencePermitResponse.NationalEmblemData.builder() |
| 152 | + .issue_authority(getFieldWords(result.getIssued())) |
| 153 | + .valid_date_start(dates[0]) |
| 154 | + .valid_date_end(dates[1]) |
| 155 | + .issue_times(getFieldWords(result.getIssuedTimes())) |
| 156 | + .eep_number(getFieldWords(result.getPassNo())) |
| 157 | + .build(); |
| 158 | + } |
| 159 | + |
| 160 | +privateStringgetFieldWords(YidaoResponse.FieldDatafieldData) { |
| 161 | +returnfieldData !=null ?fieldData.getWords() :null; |
| 162 | + } |
| 163 | + |
| 164 | +privateString[]parseValidDate(StringvalidDate) { |
| 165 | +if(validDate ==null) { |
| 166 | +returnnewString[] {null,null }; |
| 167 | + } |
| 168 | + |
| 169 | +String[]parts =validDate.split(VALID_DATE_SEPARATOR); |
| 170 | +if(parts.length ==2) { |
| 171 | +returnnewString[] {parts[0].trim(),parts[1].trim() }; |
| 172 | + } |
| 173 | + |
| 174 | +returnnewString[] {validDate,null }; |
| 175 | + } |
| 176 | + |
| 177 | +privateOcrResidencePermitResponsebuildErrorResponse(YidaoResponseyidaoResponse) { |
| 178 | +StringerrorMessage =yidaoResponse.getDescription() !=null |
| 179 | + ?yidaoResponse.getDescription() |
| 180 | + :"Unknown error"; |
| 181 | + |
| 182 | +OpenapiResponse.OpenapiErrorerror =OpenapiResponse.OpenapiError.builder() |
| 183 | + .code(String.valueOf(yidaoResponse.getErrorCode())) |
| 184 | + .message(errorMessage) |
| 185 | + .type(YIDAO_OCR_ERROR_TYPE) |
| 186 | + .httpCode(HttpStatus.BAD_REQUEST.value()) |
| 187 | + .build(); |
| 188 | + |
| 189 | +returnOcrResidencePermitResponse.builder().error(error).build(); |
| 190 | + } |
| 191 | +} |