UserIdentCertServiceImpl.java 8.0 KB

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