UserIdentCertServiceImpl.java 9.1 KB

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