refactor(huawei): 封装返回结果
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
zcxlsm 2025-08-05 10:57:39 +08:00
parent 5d81537bdb
commit 2a36ddf688
3 changed files with 29 additions and 25 deletions

View File

@ -1,6 +1,7 @@
package org.dromara.sis.sdk.huawei; package org.dromara.sis.sdk.huawei;
import org.dromara.sis.sdk.huawei.domain.AddHWPersonReq; import org.dromara.sis.sdk.huawei.domain.AddHWPersonReq;
import org.dromara.sis.sdk.huawei.domain.HWResult;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -35,5 +36,5 @@ public interface HuaWeiBoxApi {
* @param base64Img 入参 * @param base64Img 入参
* @return Map<String, Object> * @return Map<String, Object>
*/ */
Map<String, Object> findPerson(String base64Img); HWResult findPerson(String base64Img);
} }

View File

@ -0,0 +1,18 @@
package org.dromara.sis.sdk.huawei.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* @author lsm
* @apiNote HWResult
* @since 2025/8/5
*/
@Data
@AllArgsConstructor
public class HWResult {
public Integer code;
public String message;
}

View File

@ -8,6 +8,7 @@ import lombok.extern.slf4j.Slf4j;
import org.dromara.sis.sdk.huawei.HuaWeiBoxApi; import org.dromara.sis.sdk.huawei.HuaWeiBoxApi;
import org.dromara.sis.sdk.huawei.domain.AddHWPersonReq; import org.dromara.sis.sdk.huawei.domain.AddHWPersonReq;
import org.dromara.sis.sdk.huawei.domain.FinaHWPersonReq; import org.dromara.sis.sdk.huawei.domain.FinaHWPersonReq;
import org.dromara.sis.sdk.huawei.domain.HWResult;
import org.dromara.sis.sdk.huawei.utils.HuaWeiHttp; import org.dromara.sis.sdk.huawei.utils.HuaWeiHttp;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -88,7 +89,7 @@ public class HuaWeiBoxApiService implements HuaWeiBoxApi {
* @return Long * @return Long
*/ */
@Override @Override
public Map<String, Object> findPerson(String base64Img) { public HWResult findPerson(String base64Img) {
String url = "/sdk_service/rest/facerepositories/peoples"; String url = "/sdk_service/rest/facerepositories/peoples";
FinaHWPersonReq req = new FinaHWPersonReq(); FinaHWPersonReq req = new FinaHWPersonReq();
@ -99,42 +100,26 @@ public class HuaWeiBoxApiService implements HuaWeiBoxApi {
String jsonReq = JSONUtil.toJsonStr(req); String jsonReq = JSONUtil.toJsonStr(req);
String jsonStrRes = huaWeiHttp.doPost(url, jsonReq); String jsonStrRes = huaWeiHttp.doPost(url, jsonReq);
JSONObject jsonRes = JSONUtil.parseObj(jsonStrRes); JSONObject jsonRes = JSONUtil.parseObj(jsonStrRes);
Map<String, Object> result = new HashMap<>();
if (jsonRes.getInt("resultCode") != 0) { if (jsonRes.getInt("resultCode") != 0) {
switch (jsonRes.getInt("resultCode")) { return switch (jsonRes.getInt("resultCode")) {
case 912322041: case 912322041 -> new HWResult(912322041, "特征值提取失败");
result.put("code", 912322041); case 912333003 -> new HWResult(912333003, "base64参数非法");
result.put("msg", "特征值提取失败"); default -> new HWResult(jsonRes.getInt("resultCode"), jsonRes.getStr("resultMsg"));
break; };
case 912322022:
result.put("code", 912333003);
result.put("msg", "base64参数非法");
break;
default:
result.put("code", jsonRes.getInt("resultCode"));
result.put("msg", jsonRes.getStr("resultMsg"));
break;
}
log.info("调用华为盒子人脸比对接口失败code:{}msg{}", jsonRes.getInt("resultCode"), jsonRes.getStr("resultMsg"));
return result;
} }
JSONArray jsonArr = jsonRes.getJSONArray("algorithmResults"); JSONArray jsonArr = jsonRes.getJSONArray("algorithmResults");
JSONObject obj = jsonArr.getJSONObject(0); JSONObject obj = jsonArr.getJSONObject(0);
if (Integer.parseInt(obj.getStr("number")) == 0) { if (Integer.parseInt(obj.getStr("number")) == 0) {
result.put("code", 201);
result.put("msg", "无匹配数据");
log.info("无人脸比对数据"); log.info("无人脸比对数据");
return null; return new HWResult(201, "无匹配数据");
} }
JSONArray peopleList = obj.getJSONArray("peopleList"); JSONArray peopleList = obj.getJSONArray("peopleList");
JSONObject people = peopleList.getJSONObject(0); JSONObject people = peopleList.getJSONObject(0);
result.put("code", 200); return new HWResult(200, people.getStr("peopleId"));
result.put("msg", Long.parseLong(people.getStr("peopleId")));
return result;
} }
} }