UserIdentCertServiceImpl.java 10 KB

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