yaorongkeji 2 месяцев назад
Родитель
Сommit
a2d2e83740

+ 57 - 0
hcp-app/src/main/java/com/yingyangfly/app/controller/FaceController.java

@@ -0,0 +1,57 @@
+package com.yingyangfly.app.controller;
+
+import com.tencentcloudapi.common.exception.TencentCloudSDKException;
+import com.yingyangfly.common.dto.ResultResponse;
+import com.yingyangfly.common.log.annotation.TraceLog;
+import com.yingyangfly.core.annotation.Log;
+import com.yingyangfly.core.domain.Face;
+import com.yingyangfly.core.dto.AppCurrentLoginUser;
+import com.yingyangfly.core.dto.CurrentLoginUser;
+import com.yingyangfly.core.enums.OperatorType;
+import com.yingyangfly.core.security.util.TokenUtil;
+import com.yingyangfly.core.service.FaceService;
+import io.swagger.annotations.Api;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.Resource;
+import java.util.List;
+
+@Slf4j
+@RestController
+@Api(tags = "人脸照片")
+@RequestMapping("/face")
+public class FaceController {
+
+    @Resource
+    private FaceService faceService;
+
+    @Autowired
+    private TokenUtil tokenUtil;
+
+    @TraceLog
+    @PostMapping("/getHumanFace")
+    public ResultResponse getHumanFace() {
+        AppCurrentLoginUser appCurrentLoginUser = tokenUtil.getAppCurrentLoginUser();
+        List<Face> faces = faceService.selectByUserId(appCurrentLoginUser.getId());
+
+        if (faces.size()>0) {
+            return ResultResponse.success(faces.get(0).getFaceBase());
+        }
+        return ResultResponse.success();
+    }
+
+
+    @Log(title = "人脸照片删除",operatorType = OperatorType.MOBILE)
+    @PostMapping("/deleteHumanFace")
+    @TraceLog
+    public ResultResponse deleteHumanFace(String faceUrl) throws TencentCloudSDKException {
+        AppCurrentLoginUser appCurrentLoginUser = tokenUtil.getAppCurrentLoginUser();
+        return ResultResponse.success(faceService.deleteFaceUrl(appCurrentLoginUser.getId(),faceUrl));
+    }
+
+
+}

+ 3 - 3
hcp-app/src/main/java/com/yingyangfly/app/controller/LoginController.java

@@ -52,9 +52,9 @@ public class LoginController {
 
     @Log(title = "人脸录入",operatorType = OperatorType.MOBILE)
     @TraceLog
-    @PostMapping("Face/registration")
-    public ResultResponse FaceRegistration(@RequestBody String imageBase) {
-        return ResultResponse.success();
+    @PostMapping("face/registration")
+    public ResultResponse faceRegistration(String humanFaceUrl) {
+        return ResultResponse.success(appUserService.faceRegistration(humanFaceUrl));
     }
 
 

Разница между файлами не показана из-за своего большого размера
+ 13 - 0
hcp-app/src/test/java/com/yingyangfly/app/controller/VideoLearnControllerTest.java


+ 56 - 0
hcp-core/src/main/java/com/yingyangfly/core/api/impl/FaceContrastServer.java

@@ -290,6 +290,62 @@ public class FaceContrastServer {
     }
 
 
+    /**
+     * 删除人员
+     * @param personId 人员ID
+     * @return boolean 删除是否成功
+     */
+    public boolean deletePerson(String personId) {
+        return deletePerson(personId, groupId);
+    }
+
+    /**
+     * 删除人员(指定人员库)
+     * @param personId 人员ID
+     * @param groupId 人员库ID
+     * @return boolean 删除是否成功
+     */
+    public boolean deletePerson(String personId, String groupId) {
+        try {
+            // 1. 初始化客户端
+            Credential cred = new Credential(secretId, secretKey);
+            HttpProfile httpProfile = new HttpProfile();
+            httpProfile.setEndpoint(endpoint);
+            ClientProfile clientProfile = new ClientProfile();
+            clientProfile.setHttpProfile(httpProfile);
+            IaiClient client = new IaiClient(cred, region, clientProfile);
+
+
+
+            // 2. 构建删除请求
+            DeletePersonFromGroupRequest req = new DeletePersonFromGroupRequest();
+            req.setPersonId(personId);
+            req.setGroupId(groupId);
+
+            logger.info("开始删除人员: personId={}, groupId={}", personId, groupId);
+
+            // 3. 调用接口
+            DeletePersonFromGroupResponse resp = client.DeletePersonFromGroup(req);
+
+            // 4. 处理响应
+            if (resp != null) {
+                logger.info("删除人员成功: personId={}, 请求ID={}", personId, resp.getRequestId());
+                return true;
+            } else {
+                logger.error("删除人员失败: 响应为空");
+                return false;
+            }
+
+        } catch (TencentCloudSDKException e) {
+            logger.error("腾讯云删除人员失败: personId={}, errorCode={}, errorMessage={}",
+                    personId, e.getErrorCode(), e.getMessage(), e);
+            return false;
+        } catch (Exception e) {
+            logger.error("删除人员异常: personId={}, error={}", personId, e.getMessage(), e);
+            return false;
+        }
+    }
+
     private boolean validateInput(String personId, String personName, String base64Image, CreatePersonResult result) {
         // 校验人员ID格式(只支持英文、数字、-%@#&_)
         if (personId == null || !personId.matches("^[A-Za-z0-9\\-%@#&_]+$")) {

+ 9 - 0
hcp-core/src/main/java/com/yingyangfly/core/service/FaceService.java

@@ -44,4 +44,13 @@ public interface FaceService extends IService<Face> {
      * @return
      */
     Boolean deleteFace(Long UserId,Long faceId) throws TencentCloudSDKException;
+
+    /**
+     * 根据url删除
+     * @param userId
+     * @param url
+     * @return
+     * @throws TencentCloudSDKException
+     */
+    Boolean deleteFaceUrl(Long userId,String url) throws TencentCloudSDKException;
 }

+ 32 - 1
hcp-core/src/main/java/com/yingyangfly/core/service/impl/AppUserService.java

@@ -19,6 +19,7 @@ import com.yingyangfly.common.utils.DateUtils;
 import com.yingyangfly.common.utils.ids.IdUtils;
 import com.yingyangfly.core.api.ImApi;
 import com.yingyangfly.core.api.impl.FaceContrastServer;
+import com.yingyangfly.core.bean.CreatePersonResult;
 import com.yingyangfly.core.bean.FaceSearchResult;
 import com.yingyangfly.core.domain.*;
 import com.yingyangfly.core.dto.*;
@@ -740,6 +741,11 @@ public class AppUserService extends ServiceImpl<AppUserMapper, AppUser> implemen
     @Autowired
     FaceContrastServer faceContrastServer;
 
+    /**
+     * 人脸登录
+     * @param imageBase
+     * @return
+     */
     public String humanFaceLogin(String imageBase){
         FaceSearchResult result = faceContrastServer.searchPersonByFace(imageBase);
 
@@ -769,7 +775,32 @@ public class AppUserService extends ServiceImpl<AppUserMapper, AppUser> implemen
             return token;
         } else {
             // 验证失败
-            throw new RuntimeException("人脸原则失败");
+            throw new RuntimeException("人脸验证失败");
+        }
+    }
+
+    @Resource
+    FaceService faceService;
+
+    public Boolean faceRegistration(String humanFaceUrl){
+        LambdaQueryWrapper<Face> lambdaQueryWrapper = new LambdaQueryWrapper();
+        AppCurrentLoginUser currentUser = tokenUtil.getAppCurrentLoginUser();
+        lambdaQueryWrapper.eq(Face::getUserId,currentUser.getId());
+        List<Face> list = faceService.list(lambdaQueryWrapper);
+        if (list.size()>0) throw new RuntimeException("请删除照片后再添加!");
+        Face face = new Face();
+        face.setFaceBase(humanFaceUrl);
+        face.setCreateTime(new Date());
+        face.setFaceName(currentUser.getUsername());
+        face.setFaceStatus(0);
+        face.setUserId(currentUser.getId());
+        String imageBase = OSSImageUtils.getBase64FromOssUrl(humanFaceUrl, 30, 3);
+        CreatePersonResult person = faceContrastServer.createPerson(String.valueOf(currentUser.getId()), currentUser.getUsername(), imageBase);
+        if (person.isSuccess() && org.apache.commons.lang3.StringUtils.isNotEmpty(person.getFaceId())){
+            face.setFid(Long.parseLong(person.getFaceId()));
+            return faceService.save(face);
+        }else {
+            throw new RuntimeException("添加失败");
         }
     }
 

+ 62 - 10
hcp-core/src/main/java/com/yingyangfly/core/service/impl/FaceServiceImpl.java

@@ -13,6 +13,7 @@ import com.yingyangfly.core.domain.Face;
 import com.yingyangfly.core.mapper.FaceMapper;
 import com.yingyangfly.core.service.FaceService;
 import com.yingyangfly.core.util.OSSImageUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -35,6 +36,7 @@ public class FaceServiceImpl extends ServiceImpl<FaceMapper, Face> implements Fa
     public List<Face> selectByUserId(Long UserId) {
         LambdaQueryWrapper<Face> faceLambdaQueryWrapper = new LambdaQueryWrapper<>();
         faceLambdaQueryWrapper.eq(Face::getUserId,UserId);
+        faceLambdaQueryWrapper.orderByDesc(Face::getCreateTime);
         return list(faceLambdaQueryWrapper);
     }
 
@@ -54,7 +56,7 @@ public class FaceServiceImpl extends ServiceImpl<FaceMapper, Face> implements Fa
         face.setFaceStatus(0);
         face.setUserId(UserId);
         CreatePersonResult person = faceContrastServer.createPerson(String.valueOf(UserId), UserName, imageBase);
-        if (person.isSuccess()){
+        if (person.isSuccess() && StringUtils.isNotEmpty(person.getFaceId())){
             face.setFid(Long.parseLong(person.getFaceId()));
             return save(face);
         }else {
@@ -97,20 +99,70 @@ public class FaceServiceImpl extends ServiceImpl<FaceMapper, Face> implements Fa
 
     /**
      * 删除人脸
-     * @param UserId
+     * @param userId
      * @param faceId
      * @return
      * @throws TencentCloudSDKException
      */
-    public Boolean deleteFace(Long UserId,Long faceId) throws TencentCloudSDKException {
-        String[] faceIds = new String[]{String.valueOf(faceId)};
-        DeleteFaceResponse deleteFaceResponse = faceContrastServer.deleteFaces(String.valueOf(UserId), faceIds);
-        if (deleteFaceResponse.getSucDeletedNum()>0) {
-            LambdaQueryWrapper<Face> lambdaQueryWrapper = new LambdaQueryWrapper<>();
-            lambdaQueryWrapper.eq(Face::getFid,faceId);
-            return remove(lambdaQueryWrapper);
+    public Boolean deleteFace(Long userId,Long faceId) throws TencentCloudSDKException {
+        LambdaQueryWrapper<Face> lambdaQueryWrapper = new LambdaQueryWrapper<>();
+        lambdaQueryWrapper.eq(Face::getUserId,userId);
+        List<Face> list = list(lambdaQueryWrapper);
+        if (list.size() == 1) {
+            if (faceContrastServer.deletePerson(String.valueOf(userId))) {
+                LambdaQueryWrapper<Face> deleteLambdaQueryWrapper = new LambdaQueryWrapper<>();
+                deleteLambdaQueryWrapper.eq(Face::getUserId,userId);
+                return remove(deleteLambdaQueryWrapper);
+            }else {
+                throw new RuntimeException("删除失败");
+            }
         }else {
-            throw new RuntimeException("删除失败");
+            String[] faceIds = new String[]{String.valueOf(faceId)};
+            DeleteFaceResponse deleteFaceResponse = faceContrastServer.deleteFaces(String.valueOf(userId), faceIds);
+            if (deleteFaceResponse.getSucDeletedNum()>0) {
+                LambdaQueryWrapper<Face> deleteLambdaQueryWrapper = new LambdaQueryWrapper<>();
+                deleteLambdaQueryWrapper.eq(Face::getFid,faceId);
+                return remove(deleteLambdaQueryWrapper);
+            }else {
+                throw new RuntimeException("删除失败");
+            }
+        }
+    }
+
+
+    /**
+     * 删除人脸
+     * @param userId
+     * @param url
+     * @return
+     * @throws TencentCloudSDKException
+     */
+    public Boolean deleteFaceUrl(Long userId,String url) throws TencentCloudSDKException {
+        LambdaQueryWrapper<Face> lambdaQueryWrapper = new LambdaQueryWrapper<>();
+        lambdaQueryWrapper.eq(Face::getUserId,userId);
+        List<Face> list = list(lambdaQueryWrapper);
+        if (list.size() == 1) {
+            if (faceContrastServer.deletePerson(String.valueOf(userId))) {
+                LambdaQueryWrapper<Face> deleteLambdaQueryWrapper = new LambdaQueryWrapper<>();
+                deleteLambdaQueryWrapper.eq(Face::getUserId,userId);
+                return remove(deleteLambdaQueryWrapper);
+            }else {
+                throw new RuntimeException("删除失败");
+            }
+        }else {
+            LambdaQueryWrapper<Face> faceLambdaQueryWrapper = new LambdaQueryWrapper<>();
+            faceLambdaQueryWrapper.eq(Face::getUserId,userId);
+            faceLambdaQueryWrapper.eq(Face::getFaceBase,url);
+            Face face = getOne(faceLambdaQueryWrapper);
+            String[] faceIds = new String[]{String.valueOf(face.getFid())};
+            DeleteFaceResponse deleteFaceResponse = faceContrastServer.deleteFaces(String.valueOf(userId), faceIds);
+            if (deleteFaceResponse.getSucDeletedNum()>0) {
+                LambdaQueryWrapper<Face> deleteLambdaQueryWrapper = new LambdaQueryWrapper<>();
+                deleteLambdaQueryWrapper.eq(Face::getFid,face.getFid());
+                return remove(deleteLambdaQueryWrapper);
+            }else {
+                throw new RuntimeException("删除失败");
+            }
         }
     }
 }

Некоторые файлы не были показаны из-за большого количества измененных файлов