UserIdentCertServiceImpl.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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) {
  61. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  62. UserCertRecord record = userCertRecordMapper.selectOne(Wrappers.lambdaQuery(UserCertRecord.class)
  63. .in(UserCertRecord::getUserId, userIdList)
  64. .select(UserCertRecord::getName, UserCertRecord::getIdent, UserCertRecord::getCreatedTime)
  65. .last("limit 1"));
  66. if (record == null) {
  67. return null;
  68. }
  69. record.setIdent(StringUtil.deIndent(record.getIdent()));
  70. return record;
  71. }
  72. @Override
  73. public UserStudentCertRecord getUserStudentCertRecord(long userId) {
  74. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  75. UserStudentCertRecord record = userStudentCertRecordMapper.selectOne(Wrappers.lambdaQuery(UserStudentCertRecord.class)
  76. .in(UserStudentCertRecord::getUserId, userIdList)
  77. .select(UserStudentCertRecord::getSchool, UserStudentCertRecord::getCreatedTime)
  78. .last("limit 1"));
  79. if (record == null) {
  80. return null;
  81. }
  82. return record;
  83. }
  84. @Override
  85. public String bindWxUser(long userId) {
  86. User user = userMapper.selectById(userId);
  87. if (StrUtil.isNotBlank(user.getOpenId())) {
  88. return null;
  89. }
  90. String state = StringUtil.randomString(6);
  91. redisService.set(RedisService.key.WX_INDENT_BIND_KEY.getName() + state, userId, RedisService.key.WX_INDENT_BIND_KEY.getTimeout());
  92. return state;
  93. }
  94. @Override
  95. public WxCertAuthTokenResp wxH5IndentCert(UserIndentCertReq indentCertReq) throws Exception {
  96. Long userId = indentCertReq.getUserId();
  97. String redirectUrl = indentCertReq.getRedirectUrl();
  98. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  99. // String bindState = indentCertReq.getBindState();
  100. // if (userIdList.size() == 1 && StrUtil.isNotEmpty(bindState)) {
  101. // User user = userMapper.selectById(userId);
  102. // if (StrUtil.isEmpty(user.getOpenId())) {
  103. // String key = RedisService.key.WX_INDENT_BIND_KEY.getName() + bindState;
  104. // Object bindInfo = redisService.get(key);
  105. // if (bindInfo == null) {
  106. // throw BusinessRuntimeException.getInstance("认证信息已失效,请重新操作..");
  107. // }
  108. // Long bindUserId = Long.parseLong(bindInfo.toString());
  109. // User bindUser = userMapper.selectById(bindUserId);
  110. // //绑定微信端用户
  111. // UserBindInfoReq request = new UserBindInfoReq();
  112. // request.setType(StrUtil.isNotBlank(bindUser.getLoginPhone()) ? 1 : 2);
  113. // request.setUserId(userId);
  114. // request.setAccount(request.getType() == 1 ? bindUser.getLoginPhone() : bindUser.getEmail());
  115. // bindTaskService.bindOtherAccount(request);
  116. // userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  117. // redisService.del(key);
  118. // }
  119. // }
  120. Integer count = userCertRecordMapper.selectCount(Wrappers.lambdaQuery(UserCertRecord.class)
  121. .in(UserCertRecord::getUserId, userIdList));
  122. if (count > 0) throw BusinessRuntimeException.getInstance("您已完成认证了..");
  123. Credential cred = new Credential(TENCENT_SECRET_ID, TENCENT_SECRET_KEY);
  124. // 实例化一个http选项,可选的,没有特殊需求可以跳过
  125. HttpProfile httpProfile = new HttpProfile();
  126. httpProfile.setEndpoint("faceid.tencentcloudapi.com");
  127. // 实例化一个client选项,可选的,没有特殊需求可以跳过
  128. ClientProfile clientProfile = new ClientProfile();
  129. clientProfile.setHttpProfile(httpProfile);
  130. // 实例化要请求产品的client对象,clientProfile是可选的
  131. FaceidClient client = new FaceidClient(cred, "", clientProfile);
  132. // 实例化一个请求对象,每个接口都会对应一个request对象
  133. DetectAuthRequest req = new DetectAuthRequest();
  134. req.setRuleId(ruleId);
  135. Long mappingId = SEQUENCE.nextId();
  136. req.setRedirectUrl(envCommonService.getNotifyHost() + REDIRECT_URL + mappingId);
  137. // 返回的resp是一个DetectAuthResponse的实例,与请求对象对应
  138. DetectAuthResponse resp = client.DetectAuth(req);
  139. log.info("微信h5核身认证,返回:{}", Jsons.toJson(resp));
  140. WxCertAuthTokenResp tokenResp = new WxCertAuthTokenResp();
  141. BeanUtil.copyProperties(resp, tokenResp);
  142. UserWxIndentDto userWxIndentDto = new UserWxIndentDto();
  143. userWxIndentDto.setUserId(userId);
  144. userWxIndentDto.setRedirectUrl(redirectUrl);
  145. userWxIndentDto.setBizToken(tokenResp.getBizToken());
  146. redisService.set(mappingId.toString(), Jsons.toJson(userWxIndentDto), 60 * 60 * 24L);
  147. return tokenResp;
  148. }
  149. @Override
  150. public void wxH5IndentCertResult(UserWxIndentDto userWxIndentDto) throws Exception {
  151. Long userId = userWxIndentDto.getUserId();
  152. Credential cred = new Credential(TENCENT_SECRET_ID, TENCENT_SECRET_KEY);
  153. // 实例化一个http选项,可选的,没有特殊需求可以跳过
  154. HttpProfile httpProfile = new HttpProfile();
  155. httpProfile.setEndpoint("faceid.tencentcloudapi.com");
  156. // 实例化一个client选项,可选的,没有特殊需求可以跳过
  157. ClientProfile clientProfile = new ClientProfile();
  158. clientProfile.setHttpProfile(httpProfile);
  159. // 实例化要请求产品的client对象,clientProfile是可选的
  160. FaceidClient client = new FaceidClient(cred, "", clientProfile);
  161. // 实例化一个请求对象,每个接口都会对应一个request对象
  162. GetDetectInfoEnhancedRequest req = new GetDetectInfoEnhancedRequest();
  163. req.setBizToken(userWxIndentDto.getBizToken());
  164. req.setRuleId(ruleId);
  165. // 返回的resp是一个GetDetectInfoEnhancedResponse的实例,与请求对象对应
  166. GetDetectInfoEnhancedResponse resp = client.GetDetectInfoEnhanced(req);
  167. DetectInfoText text = resp.getText();
  168. if (text.getErrCode() == 0) {
  169. UserCertRecord certRecord = new UserCertRecord();
  170. String ocrIdCard = text.getOcrIdCard();
  171. Integer count = userCertRecordMapper.selectCount(Wrappers.lambdaQuery(UserCertRecord.class)
  172. .eq(UserCertRecord::getIdent, ocrIdCard));
  173. if (count > 0) {
  174. throw BusinessRuntimeException.getInstance("该身份证号已被其他账号绑定");
  175. }
  176. if (count == 0) {
  177. certRecord.setIdent(text.getOcrIdCard());
  178. certRecord.setName(text.getOcrName());
  179. certRecord.setSex(text.getOcrGender());
  180. certRecord.setUserId(userId);
  181. certRecord.setAddr(text.getOcrAddress());
  182. userCertRecordMapper.insert(certRecord);
  183. }
  184. }
  185. }
  186. }