UserIdentCertServiceImpl.java 8.1 KB

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