优化实名认证

This commit is contained in:
墨大叔 2024-09-24 10:03:37 +08:00
parent 88688e1cea
commit 49469ba6f1
2 changed files with 42 additions and 26 deletions

View File

@ -9,21 +9,13 @@ import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
@ -81,4 +73,18 @@ public class QiNiuUtils {
return upload(fis);
}
}
/**
* Base64上传到七牛云
* @param base64
* @return
*/
public static String uploadBase64(String base64) throws Exception{
byte[] bytes = base64.getBytes();
byte[] decode = Base64Utils.decode(bytes);
Response response = UPLOAD_MANAGER.put(decode, null, getToken());
// 解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
return putRet.key;
}
}

View File

@ -4,6 +4,7 @@ import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.entity.SmUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.UserType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServiceUtil;
@ -41,6 +42,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.Base64Utils;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@ -375,15 +377,21 @@ public class SmUserServiceImpl implements ISmUserService
// 通过userId获取用户提交的信息
String cacheKey = CacheConstants.LIVENESS_TOKEN + userId;
UserFaceDTO face = redisCache.getCacheObject(cacheKey);
ServiceUtil.assertion(face == null, "token已过期");
ServiceUtil.assertion(face == null, "token不存在");
// 获取活体图像及三要素信息
LivenessResponseQueryData livenessResult = LivenessUtils.queryResult(face.getToken());
ServiceUtil.assertion(livenessResult == null, "活体信息不存在");
ServiceUtil.assertion(!LivenessQueryResult.PASS.getResult().equals(livenessResult.getResult()), "活体检测未通过");
ServiceUtil.assertion(StringUtils.isBlank(livenessResult.getFaceImage()), "活体检测未通过");
// TODO 将Base64上传到七牛云中
// QiNiuUtils.upload(livenessResult.getFaceImage());
// 将Base64上传到七牛云中
String imageUrl = livenessResult.getFaceImage();
try {
imageUrl = QiNiuUtils.DOMAIN + "/" + QiNiuUtils.uploadBase64(imageUrl);
} catch (Exception e) {
throw new ServiceException("上传文件失败:" + e.getMessage());
}
// 判断活体是否匹配
FaceResponseData faceIdb = FaceUtils.getFaceIdb(face.getIdCard(), face.getName(), livenessResult.getFaceImage(), null);
@ -392,26 +400,28 @@ public class SmUserServiceImpl implements ISmUserService
ServiceUtil.assertion(!FaceScoreResult.isSame(faceIdb.getScore()), "人证比对未通过,分数:" + faceIdb.getScore());
// 通过修改实名认证信息用户表实名认证表
// 构造用户信息
SmUser data = new SmUserQuery();
data.setUserId(face.getUserId());
data.setRealName(face.getName());
data.setRealIdCard(face.getIdCard());
data.setRealPhone(face.getMobile());
data.setIsReal(true);
// 构造实名认证表
RealName realName = new RealName();
realName.setUserId(userId);
realName.setName(face.getName());
realName.setIdCard(face.getIdCard());
realName.setMobile(face.getMobile());
realName.setScore(faceIdb.getScore());
realName.setFaceImage(imageUrl);
// 数据库操作
Integer result = transactionTemplate.execute(status -> {
// 修改用户实名信息
SmUser data = new SmUserQuery();
data.setUserId(face.getUserId());
data.setRealName(face.getName());
data.setRealIdCard(face.getIdCard());
data.setRealPhone(face.getMobile());
data.setIsReal(true);
int update = smUserMapper.updateSmUser(data);
ServiceUtil.assertion(update != 1, "更新用户信息失败");
// 插入实名认证表
RealName realName = new RealName();
realName.setUserId(userId);
realName.setName(face.getName());
realName.setIdCard(face.getIdCard());
realName.setMobile(face.getMobile());
realName.setScore(faceIdb.getScore());
realName.setFaceImage(livenessResult.getFaceImage());
int insert = realNameService.insertRealName(realName);
ServiceUtil.assertion(insert != 1, "新增实名认证失败");