refactor(): 人脸识别功能补充异常排查与权限判断(增加了nacos请求内网图片配置)解决SSL证书问题

main
lishiwei 2026-08-03 17:03:22 +08:00
parent 20a5a3a4fc
commit 6ae851adf6
2 changed files with 183 additions and 116 deletions

View File

@ -57,16 +57,16 @@ public class StudentSignAddExe {
String faceUrl = null; String faceUrl = null;
try { try {
faceUrl = Base64Util.getBase64String(studentSignE.getFiles()[0]); faceUrl = Base64Util.getBase64String(studentSignE.getFiles()[0]);
log.info("StudentSignAddExe-execute,调用阿里进行人脸识别开始");
long startAliTime = System.currentTimeMillis();
Boolean environment = false; Boolean environment = false;
log.info("StudentSignAddExe-execute,fileUrlConfig.getPrefixUrl():{}",fileUrlConfig.getPrefixUrl()); if (!"https://jpfz.qhdsafety.com/gbsFileTest/".equals(fileUrlConfig.getPrefixUrl())) {
if(!"https://jpfz.qhdsafety.com/gbsFileTest/".equals(fileUrlConfig.getPrefixUrl())){
environment = true; environment = true;
} }
log.info("StudentSignAddExe-execute,开始进行人脸识别,prefixUrl:{},environment:{}", fileUrlConfig.getPrefixUrl(), environment);
studentSignE.compareFace(student.getUserAvatarUrl(), faceUrl, fileUrlConfig.getPrefixUrl(),environment); long startAliTime = System.currentTimeMillis();
log.info("StudentSignAddExe-execute,调用阿里进行人脸识别结束,耗时:{}",System.currentTimeMillis() - startAliTime); studentSignE.compareFace(student.getUserAvatarUrl(), faceUrl, fileUrlConfig.getPrefixUrl(), environment);
log.info("StudentSignAddExe-execute,人脸识别结束,耗时:{}", System.currentTimeMillis() - startAliTime);
} catch (BizException e) {
throw e;
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -75,7 +75,12 @@ public class StudentSignAddExe {
String fileName = Tools.get32UUID() + studentSignE.getFiles()[0].getOriginalFilename().substring(studentSignE.getFiles()[0].getOriginalFilename().lastIndexOf(".")); String fileName = Tools.get32UUID() + studentSignE.getFiles()[0].getOriginalFilename().substring(studentSignE.getFiles()[0].getOriginalFilename().lastIndexOf("."));
log.info("StudentSignAddExe-execute,保存人脸图片开始"); log.info("StudentSignAddExe-execute,保存人脸图片开始");
long l = System.currentTimeMillis(); long l = System.currentTimeMillis();
studentSignE.setFaceUrl(zcloudImgFilesFacade.saveFile( faceUrl,fileName, path)); try {
studentSignE.setFaceUrl(zcloudImgFilesFacade.saveFile(
studentSignE.getFiles()[0].getBytes(), fileName, path, stu.getClassCorpinfoId()));
} catch (IOException e) {
throw new BizException("保存人脸图片失败");
}
log.info("StudentSignAddExe-execute,保存人脸图片结束,耗时:{}",System.currentTimeMillis() - l); log.info("StudentSignAddExe-execute,保存人脸图片结束,耗时:{}",System.currentTimeMillis() - l);
studentSignE.init(stu.getStudentId(), stu.getClassId(), stu.getClassCorpinfoId()); studentSignE.init(stu.getStudentId(), stu.getClassId(), stu.getClassCorpinfoId());
boolean res = false; boolean res = false;

View File

@ -1,108 +1,170 @@
package com.zcloud.edu.domain.model.study; package com.zcloud.edu.domain.model.study;
import com.alibaba.cola.exception.BizException; import com.alibaba.cola.exception.BizException;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.jjb.saas.framework.domain.model.BaseE; import com.jjb.saas.framework.domain.model.BaseE;
import com.zcloud.gbscommon.utils.Base64Util; import com.zcloud.gbscommon.utils.Base64Util;
import com.zcloud.gbscommon.utils.FaceUtil; import com.zcloud.gbscommon.utils.FaceUtil;
import com.zcloud.gbscommon.utils.HttpRequestUtil; import com.zcloud.gbscommon.utils.Tools;
import com.zcloud.gbscommon.utils.Tools; import lombok.Data;
import lombok.Data; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedReader;
import java.time.LocalDateTime; import java.io.InputStream;
import java.util.HashMap; import java.io.InputStreamReader;
import java.util.Map; import java.io.OutputStream;
import java.net.HttpURLConnection;
/** import java.net.URL;
* web-domain import java.nio.charset.StandardCharsets;
* import java.time.LocalDateTime;
* @Author zhangyue import java.util.HashMap;
* @Date 2026-01-13 14:18:20 import java.util.Map;
*/
@Data /**
public class StudentSignE extends BaseE { * web-domain
private Long id; *
//学员id * @Author zhangyue
private String studentId; * @Date 2026-01-13 14:18:20
//业务id */
private String studentSignId; @Data
//班级id public class StudentSignE extends BaseE {
private String classId; private Long id;
//用户id //学员id
private Long userId; private String studentId;
//企业id //业务id
private Long corpinfoId; private String studentSignId;
//签到人脸路径 //班级id
private String faceUrl; private String classId;
//打卡签字路径 //用户id
private String signUrl; private Long userId;
//签到类型 1-打卡签到 2-考试签到 //企业id
private Integer type; private Long corpinfoId;
//签到人脸路径
private String phone; private String faceUrl;
private MultipartFile[] files; //打卡签字路径
//环境 private String signUrl;
private String env; //签到类型 1-打卡签到 2-考试签到
//删除标识true false private Integer type;
private String deleteEnum;
//备注 private String phone;
private String remarks; private MultipartFile[] files;
//创建人姓名 //环境
private String createName; private String env;
//更新人姓名 //删除标识true false
private String updateName; private String deleteEnum;
//租户id //备注
private Long tenantId; private String remarks;
//单位id //创建人姓名
private Long orgId; private String createName;
//版本 //更新人姓名
private Integer version; private String updateName;
//创建时间 //租户id
private LocalDateTime createTime; private Long tenantId;
//修改时间 //单位id
private LocalDateTime updateTime; private Long orgId;
//创建人id //版本
private Long createId; private Integer version;
//修改人id //创建时间
private Long updateId; private LocalDateTime createTime;
//修改时间
public void compareFace(String templateFaceUrl, String faceUrl, String prefixUrl, Boolean environment) throws Exception { private LocalDateTime updateTime;
try { //创建人id
String templateFace = Base64Util.urlToBase64(prefixUrl + templateFaceUrl); private Long createId;
Map<String, Object> map = new HashMap<String, Object>(); //修改人id
map.put("pic1", templateFace); private Long updateId;
map.put("pic2", faceUrl);
String jsonParam = JSON.toJSONString(map); public void compareFace(String templateFaceUrl, String faceUrl, String prefixUrl, Boolean environment) throws Exception {
String compareResultStr; try {
if (environment) { String templateFace = Base64Util.urlToBase64(prefixUrl + templateFaceUrl);
// 正式环境 String compareResultStr;
String compareResult = HttpRequestUtil.doPost("http://192.168.192.201:8971/gbs-face/face/compareFace", jsonParam); if (environment) {
JSONObject jsonObject = JSONObject.parseObject(compareResult); // 正式环境gbs-face
jsonObject.getJSONObject("info").getString("confidence"); Map<String, Object> map = new HashMap<>();
compareResultStr = jsonObject.getJSONObject("info").getString("confidence"); map.put("pic1", templateFace);
} else { map.put("pic2", faceUrl);
// 测试环境 String compareResult = callGbsFaceCompare(JSON.toJSONString(map));
compareResultStr = FaceUtil.compareFace(templateFace, faceUrl); compareResultStr = parseGbsFaceConfidence(compareResult);
} } else {
// 测试环境:阿里云
if (Double.valueOf(compareResultStr) < 75) { compareResultStr = FaceUtil.compareFace(templateFace, faceUrl);
throw new BizException("人脸不匹配"); }
}
this.setStudentSignId(Tools.get32UUID()); if (Double.valueOf(compareResultStr) < 75) {
} catch (Exception e) { throw new BizException("人脸不匹配");
e.printStackTrace(); }
throw new BizException("人脸不匹配"); this.setStudentSignId(Tools.get32UUID());
} } catch (BizException e) {
throw e;
} } catch (Exception e) {
throw new BizException("人脸比对失败: " + e.getMessage());
public void init(String studentId, String classId, long corpinfoId) { }
this.setStudentId(studentId); }
this.setClassId(classId);
this.setCorpinfoId(corpinfoId); private String callGbsFaceCompare(String jsonParam) throws Exception {
this.setStudentSignId(Tools.get32UUID()); URL url = new URL("http://192.168.192.201:8971/gbs-face/face/compareFace");
} HttpURLConnection connection = (HttpURLConnection) url.openConnection();
} connection.setRequestMethod("POST");
connection.setConnectTimeout(15000);
connection.setReadTimeout(60000);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
byte[] body = jsonParam.getBytes(StandardCharsets.UTF_8);
connection.setFixedLengthStreamingMode(body.length);
try (OutputStream os = connection.getOutputStream()) {
os.write(body);
}
int responseCode = connection.getResponseCode();
InputStream is = responseCode >= 200 && responseCode < 300
? connection.getInputStream() : connection.getErrorStream();
if (is == null) {
return "";
}
StringBuilder result = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
result.append(line);
}
} finally {
connection.disconnect();
}
return result.toString();
}
private String parseGbsFaceConfidence(String compareResult) {
if (compareResult == null || compareResult.trim().isEmpty()) {
throw new BizException("人脸比对服务无响应");
}
JSONObject jsonObject = JSONObject.parseObject(compareResult);
if (jsonObject == null) {
throw new BizException("人脸比对服务返回异常: " + compareResult);
}
if ("exception".equals(jsonObject.getString("result"))) {
String exceptionMsg = jsonObject.getString("exception");
throw new BizException(exceptionMsg != null ? exceptionMsg : "人脸比对服务异常");
}
JSONObject info = jsonObject.getJSONObject("info");
if (info != null && info.getString("confidence") != null) {
return info.getString("confidence");
}
JSONObject data = jsonObject.getJSONObject("data");
if (data != null && data.getString("confidence") != null) {
return data.getString("confidence");
}
if (jsonObject.getString("confidence") != null) {
return jsonObject.getString("confidence");
}
throw new BizException("人脸比对服务返回异常: " + compareResult);
}
public void init(String studentId, String classId, long corpinfoId) {
this.setStudentId(studentId);
this.setClassId(classId);
this.setCorpinfoId(corpinfoId);
this.setStudentSignId(Tools.get32UUID());
}
}