UserIdentCertServiceImpl.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package com.cyksj.service.user.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  5. import com.cyksj.common.exception.BusinessRuntimeException;
  6. import com.cyksj.common.util.Jsons;
  7. import com.cyksj.common.util.StringUtil;
  8. import com.cyksj.mapper.UserCertRecordMapper;
  9. import com.cyksj.mapper.UserMapper;
  10. import com.cyksj.model.entity.User;
  11. import com.cyksj.model.entity.UserCertRecord;
  12. import com.cyksj.model.request.UserBindInfoReq;
  13. import com.cyksj.model.request.UserIndentCertReq;
  14. import com.cyksj.model.response.WxCertAuthTokenResp;
  15. import com.cyksj.redis.RedisService;
  16. import com.cyksj.service.market.task.BindTaskService;
  17. import com.cyksj.service.user.UserBindRelationService;
  18. import com.cyksj.service.user.UserIdentCertService;
  19. import com.tencentcloudapi.common.Credential;
  20. import com.tencentcloudapi.common.profile.ClientProfile;
  21. import com.tencentcloudapi.common.profile.HttpProfile;
  22. import com.tencentcloudapi.faceid.v20180301.FaceidClient;
  23. import com.tencentcloudapi.faceid.v20180301.models.*;
  24. import lombok.RequiredArgsConstructor;
  25. import lombok.extern.slf4j.Slf4j;
  26. import org.springframework.stereotype.Service;
  27. import java.util.List;
  28. /**
  29. * 项目名: yhlxj2
  30. * 文件名: UserIdentCertServiceImpl
  31. * 创建者: JavaZou
  32. * 创建时间:2024/2/21 10:21
  33. */
  34. @Service
  35. @RequiredArgsConstructor
  36. @Slf4j
  37. public class UserIdentCertServiceImpl implements UserIdentCertService {
  38. private final UserBindRelationService userBindRelationService;
  39. private final UserCertRecordMapper userCertRecordMapper;
  40. private final BindTaskService bindTaskService;
  41. private final UserMapper userMapper;
  42. private final RedisService redisService;
  43. private static final String TENCENT_SECRET_ID = "AKIDBB3jEqR0Y95zpfTix1vd6fMqAIahc4Ka";
  44. private static final String TENCENT_SECRET_KEY = "c6YtNm63rcQ1QdiGmEbmEPSTPF3e4KQK";
  45. private static final String ruleId = "1";
  46. @Override
  47. public Boolean checkCertRecord(long userId) {
  48. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  49. Integer count = userCertRecordMapper.selectCount(Wrappers.lambdaQuery(UserCertRecord.class)
  50. .in(UserCertRecord::getUserId, userIdList));
  51. return count > 0 ? true : false;
  52. }
  53. @Override
  54. public UserCertRecord getUserCertRecord(long userId) {
  55. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  56. UserCertRecord record = userCertRecordMapper.selectOne(Wrappers.lambdaQuery(UserCertRecord.class)
  57. .in(UserCertRecord::getUserId, userIdList)
  58. .select(UserCertRecord::getName, UserCertRecord::getIdent, UserCertRecord::getCreatedTime)
  59. .last("limit 1"));
  60. record.setIdent(StringUtil.deIndent(record.getIdent()));
  61. return record;
  62. }
  63. @Override
  64. public String bindWxUser(long userId) {
  65. User user = userMapper.selectById(userId);
  66. if (StrUtil.isNotBlank(user.getOpenId())) {
  67. return null;
  68. }
  69. String state = StringUtil.randomString(6);
  70. redisService.set(RedisService.key.WX_INDENT_BIND_KEY.getEnvName() + state, userId, RedisService.key.WX_INDENT_BIND_KEY.getTimeout());
  71. return state;
  72. }
  73. @Override
  74. public WxCertAuthTokenResp wxH5IndentCert(UserIndentCertReq indentCertReq) throws Exception {
  75. Long userId = indentCertReq.getUserId();
  76. String redirectUrl = indentCertReq.getRedirectUrl();
  77. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  78. String bindState = indentCertReq.getBindState();
  79. if (userIdList.size() == 1 && StrUtil.isNotEmpty(bindState)) {
  80. User user = userMapper.selectById(userId);
  81. if (StrUtil.isEmpty(user.getOpenId())) {
  82. String key = RedisService.key.WX_INDENT_BIND_KEY.getEnvName() + bindState;
  83. Object bindInfo = redisService.get(key);
  84. if (bindInfo == null) {
  85. throw BusinessRuntimeException.getInstance("认证信息已失效,请重新操作..");
  86. }
  87. Long bindUserId = Long.parseLong(bindInfo.toString());
  88. User bindUser = userMapper.selectById(bindUserId);
  89. //绑定微信端用户
  90. UserBindInfoReq request = new UserBindInfoReq();
  91. request.setType(StrUtil.isNotBlank(bindUser.getLoginPhone()) ? 1 : 2);
  92. request.setUserId(userId);
  93. request.setAccount(request.getType() == 1 ? bindUser.getLoginPhone() : bindUser.getEmail());
  94. bindTaskService.bindOtherAccount(request);
  95. userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  96. redisService.del(key);
  97. }
  98. }
  99. Integer count = userCertRecordMapper.selectCount(Wrappers.lambdaQuery(UserCertRecord.class)
  100. .in(UserCertRecord::getUserId, userIdList));
  101. if (count > 0) throw BusinessRuntimeException.getInstance("您已完成认证了..");
  102. Credential cred = new Credential(TENCENT_SECRET_ID, TENCENT_SECRET_KEY);
  103. // 实例化一个http选项,可选的,没有特殊需求可以跳过
  104. HttpProfile httpProfile = new HttpProfile();
  105. httpProfile.setEndpoint("faceid.tencentcloudapi.com");
  106. // 实例化一个client选项,可选的,没有特殊需求可以跳过
  107. ClientProfile clientProfile = new ClientProfile();
  108. clientProfile.setHttpProfile(httpProfile);
  109. // 实例化要请求产品的client对象,clientProfile是可选的
  110. FaceidClient client = new FaceidClient(cred, "", clientProfile);
  111. // 实例化一个请求对象,每个接口都会对应一个request对象
  112. DetectAuthRequest req = new DetectAuthRequest();
  113. req.setRuleId(ruleId);
  114. req.setRedirectUrl(redirectUrl);
  115. // 返回的resp是一个DetectAuthResponse的实例,与请求对象对应
  116. DetectAuthResponse resp = client.DetectAuth(req);
  117. log.info("微信h5核身认证,返回:{}", Jsons.toJson(resp));
  118. WxCertAuthTokenResp tokenResp = new WxCertAuthTokenResp();
  119. BeanUtil.copyProperties(resp, tokenResp);
  120. return tokenResp;
  121. }
  122. @Override
  123. public Boolean wxH5IndentCertResult(Long userId, String bizToken) throws Exception {
  124. Credential cred = new Credential(TENCENT_SECRET_ID, TENCENT_SECRET_KEY);
  125. // 实例化一个http选项,可选的,没有特殊需求可以跳过
  126. HttpProfile httpProfile = new HttpProfile();
  127. httpProfile.setEndpoint("faceid.tencentcloudapi.com");
  128. // 实例化一个client选项,可选的,没有特殊需求可以跳过
  129. ClientProfile clientProfile = new ClientProfile();
  130. clientProfile.setHttpProfile(httpProfile);
  131. // 实例化要请求产品的client对象,clientProfile是可选的
  132. FaceidClient client = new FaceidClient(cred, "", clientProfile);
  133. // 实例化一个请求对象,每个接口都会对应一个request对象
  134. GetDetectInfoEnhancedRequest req = new GetDetectInfoEnhancedRequest();
  135. req.setBizToken(bizToken);
  136. req.setRuleId(ruleId);
  137. // 返回的resp是一个GetDetectInfoEnhancedResponse的实例,与请求对象对应
  138. GetDetectInfoEnhancedResponse resp = client.GetDetectInfoEnhanced(req);
  139. DetectInfoText text = resp.getText();
  140. Boolean flag = false;
  141. if (text.getErrCode() == 0) {
  142. UserCertRecord certRecord = new UserCertRecord();
  143. String ocrIdCard = text.getOcrIdCard();
  144. Integer count = userCertRecordMapper.selectCount(Wrappers.lambdaQuery(UserCertRecord.class)
  145. .eq(UserCertRecord::getIdent, ocrIdCard));
  146. if (count == 0) {
  147. flag = true;
  148. certRecord.setIdent(text.getOcrIdCard());
  149. certRecord.setName(text.getOcrName());
  150. certRecord.setSex(text.getOcrGender());
  151. certRecord.setUserId(userId);
  152. certRecord.setAddr(text.getOcrAddress());
  153. userCertRecordMapper.insert(certRecord);
  154. }
  155. }
  156. return flag;
  157. }
  158. }