|
|
@@ -14,6 +14,8 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import shop.alien.entity.result.R;
|
|
|
import shop.alien.entity.store.LifeUser;
|
|
|
import shop.alien.entity.store.StoreAliPayErrorLog;
|
|
|
import shop.alien.entity.store.StoreAliPayLog;
|
|
|
@@ -22,12 +24,15 @@ import shop.alien.store.service.LifeUserService;
|
|
|
import shop.alien.store.service.StoreAliPayErrorLogService;
|
|
|
import shop.alien.store.service.StoreAliPayLogService;
|
|
|
import shop.alien.store.service.StoreUserService;
|
|
|
+import shop.alien.store.util.ali.ocr.OcrStrategy;
|
|
|
+import shop.alien.store.util.ali.ocr.OcrStrategyFactory;
|
|
|
import shop.alien.util.common.RandomCreateUtil;
|
|
|
import shop.alien.util.common.UniqueRandomNumGenerator;
|
|
|
import shop.alien.util.common.UrlEncode;
|
|
|
import shop.alien.util.system.OSUtil;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
+import java.util.Base64;
|
|
|
import java.util.Date;
|
|
|
|
|
|
/**
|
|
|
@@ -51,6 +56,8 @@ public class AliApi {
|
|
|
private final StoreAliPayErrorLogService storeAliPayErrorLogService;
|
|
|
|
|
|
|
|
|
+ private final OcrStrategyFactory ocrStrategyFactory;
|
|
|
+
|
|
|
/**
|
|
|
* 商家端appId
|
|
|
*/
|
|
|
@@ -690,6 +697,46 @@ public class AliApi {
|
|
|
return sdf.format(new Date()) + RandomCreateUtil.getRandomNum(4);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 支付宝OCR识别
|
|
|
+ *
|
|
|
+ * @param imageId 图片id
|
|
|
+ * @param ocrType OCR识别类型
|
|
|
+ * @return OCR识别结果
|
|
|
+ * @throws Exception 识别异常
|
|
|
+ */
|
|
|
+ public R ocrRequest(String imageId, String ocrType) throws Exception {
|
|
|
+ OcrStrategy strategy = ocrStrategyFactory.getStrategy(ocrType);
|
|
|
+ return strategy.recognize(imageId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 支付宝OCR识别
|
|
|
+ *
|
|
|
+ * @param imageUrl 图片url
|
|
|
+ * @param ocrType OCR识别类型
|
|
|
+ * @return OCR识别结果
|
|
|
+ * @throws Exception 识别异常
|
|
|
+ */
|
|
|
+ public R ocrRequestUrl(String imageUrl, String ocrType) throws Exception {
|
|
|
+ OcrStrategy strategy = ocrStrategyFactory.getStrategy(ocrType);
|
|
|
+ return strategy.recognizeUrl(imageUrl);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 支付宝OCR识别(Base64方式,使用策略工厂模式)
|
|
|
+ * 目前没用w
|
|
|
+ * @param imageFile 图片文件
|
|
|
+ * @param ocrType OCR识别类型
|
|
|
+ * @return OCR识别结果
|
|
|
+ * @throws Exception 识别异常
|
|
|
+ */
|
|
|
+ public R ocrRequestByBase64(MultipartFile imageFile, String ocrType) throws Exception {
|
|
|
+ OcrStrategy strategy = ocrStrategyFactory.getStrategy(ocrType);
|
|
|
+ byte[] imageBytes = imageFile.getBytes();
|
|
|
+ String imageBase64 = Base64.getEncoder().encodeToString(imageBytes);
|
|
|
+ return strategy.recognizeByBase64(imageBase64);
|
|
|
+ }
|
|
|
|
|
|
private String getMassage(String subCode) {
|
|
|
switch (subCode) {
|
|
|
@@ -730,4 +777,32 @@ public class AliApi {
|
|
|
return "支付宝请求错误";
|
|
|
}
|
|
|
|
|
|
+ public R secondClient(String imageUrl, String ocrType, String userName, String idCard) {
|
|
|
+ try {
|
|
|
+ R r = this.ocrRequestUrl(imageUrl, ocrType);
|
|
|
+ r.getCode();
|
|
|
+ if(200 == r.getCode()) {
|
|
|
+ if(!JSONObject.parseObject(r.getData().toString()).containsKey("face")) {
|
|
|
+ return R.fail("OCR识别异常:请上传身份证正面照片");
|
|
|
+ }
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(r.getData().toString()).getJSONObject("face").getJSONObject("data");
|
|
|
+ String name = jsonObject.getString("name");
|
|
|
+ String idCardNo = jsonObject.getString("idNumber");
|
|
|
+ if(userName.equals(name) && idCard.equals(idCardNo)) {
|
|
|
+ return R.success("验证成功");
|
|
|
+ } else {
|
|
|
+ return R.fail("委托人姓名或身份证号错误");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ // 7. 细化异常类型(JSON 解析失败,比如数据格式不匹配)
|
|
|
+ log.error("OCR 结果解析失败(数据格式异常):{}", e.getMessage(), e);
|
|
|
+ return R.fail("OCR识别异常:返回数据格式错误");
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 8. 兜底异常(记录详细日志,便于排查)
|
|
|
+ log.error("委托人身份验证未知异常:{}", e.getMessage(), e);
|
|
|
+ return R.fail("委托人身份验证未知异常:"+e.getMessage());
|
|
|
+ }
|
|
|
+ return R.fail("OCR识别异常");
|
|
|
+ }
|
|
|
}
|