|
| 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 | +} |