zoujiajian 2 năm trước cách đây
mục cha
commit
7403f882be

+ 20 - 0
netflix-dao/src/main/java/com/cyksj/model/dto/UserWxIndentDto.java

@@ -0,0 +1,20 @@
+package com.cyksj.model.dto;
+
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * 项目名: yhlxj2
+ * 文件名: UserWxIndentDto
+ * 创建者: JavaZou
+ * 创建时间:2024/2/21 16:00
+ */
+@Getter
+@Setter
+public class UserWxIndentDto {
+	private Long userId;
+
+	private String redirectUrl;
+
+	private String bizToken;
+}

+ 2 - 1
netflix-service/src/main/java/com/cyksj/service/user/UserIdentCertService.java

@@ -1,5 +1,6 @@
 package com.cyksj.service.user;
 
+import com.cyksj.model.dto.UserWxIndentDto;
 import com.cyksj.model.entity.UserCertRecord;
 import com.cyksj.model.request.UserIndentCertReq;
 import com.cyksj.model.response.WxCertAuthTokenResp;
@@ -13,7 +14,7 @@ import com.cyksj.model.response.WxCertAuthTokenResp;
 public interface UserIdentCertService {
 	WxCertAuthTokenResp wxH5IndentCert(UserIndentCertReq indentCertReq) throws Exception;
 
-	Boolean wxH5IndentCertResult(Long userId, String bizToken) throws Exception;
+	void wxH5IndentCertResult(UserWxIndentDto userWxIndentDto) throws Exception;
 
 	String bindWxUser(long userId);
 

+ 20 - 6
netflix-service/src/main/java/com/cyksj/service/user/impl/UserIdentCertServiceImpl.java

@@ -3,11 +3,14 @@ package com.cyksj.service.user.impl;
 import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.cyksj.common.EnvCommonService;
 import com.cyksj.common.exception.BusinessRuntimeException;
+import com.cyksj.common.snowflake.Sequence;
 import com.cyksj.common.util.Jsons;
 import com.cyksj.common.util.StringUtil;
 import com.cyksj.mapper.UserCertRecordMapper;
 import com.cyksj.mapper.UserMapper;
+import com.cyksj.model.dto.UserWxIndentDto;
 import com.cyksj.model.entity.User;
 import com.cyksj.model.entity.UserCertRecord;
 import com.cyksj.model.request.UserBindInfoReq;
@@ -48,12 +51,18 @@ public class UserIdentCertServiceImpl implements UserIdentCertService {
 
 	private final RedisService redisService;
 
+	private final EnvCommonService envCommonService;
+
+	private final static Sequence SEQUENCE = new Sequence(0);
+
 	private static final String TENCENT_SECRET_ID = "AKIDBB3jEqR0Y95zpfTix1vd6fMqAIahc4Ka";
 
 	private static final String TENCENT_SECRET_KEY = "c6YtNm63rcQ1QdiGmEbmEPSTPF3e4KQK";
 
 	private static final String ruleId = "1";
 
+	private static final String REDIRECT_URL = "applets/user/cert/auth/result/";
+
 	@Override
 	public Boolean checkCertRecord(long userId) {
 		List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
@@ -130,7 +139,8 @@ public class UserIdentCertServiceImpl implements UserIdentCertService {
 		// 实例化一个请求对象,每个接口都会对应一个request对象
 		DetectAuthRequest req = new DetectAuthRequest();
 		req.setRuleId(ruleId);
-		req.setRedirectUrl(redirectUrl);
+		Long mappingId = SEQUENCE.nextId();
+		req.setRedirectUrl(envCommonService.getHost() + REDIRECT_URL + mappingId);
 		// 返回的resp是一个DetectAuthResponse的实例,与请求对象对应
 		DetectAuthResponse resp = client.DetectAuth(req);
 
@@ -138,11 +148,18 @@ public class UserIdentCertServiceImpl implements UserIdentCertService {
 
 		WxCertAuthTokenResp tokenResp = new WxCertAuthTokenResp();
 		BeanUtil.copyProperties(resp, tokenResp);
+
+		UserWxIndentDto userWxIndentDto = new UserWxIndentDto();
+		userWxIndentDto.setUserId(userId);
+		userWxIndentDto.setRedirectUrl(redirectUrl);
+		userWxIndentDto.setBizToken(tokenResp.getBizToken());
+		redisService.set(mappingId.toString(), Jsons.toJson(userWxIndentDto), 60 * 60 * 24L);
 		return tokenResp;
 	}
 
 	@Override
-	public Boolean wxH5IndentCertResult(Long userId, String bizToken) throws Exception {
+	public void wxH5IndentCertResult(UserWxIndentDto userWxIndentDto) throws Exception {
+		Long userId = userWxIndentDto.getUserId();
 		Credential cred = new Credential(TENCENT_SECRET_ID, TENCENT_SECRET_KEY);
 		// 实例化一个http选项,可选的,没有特殊需求可以跳过
 		HttpProfile httpProfile = new HttpProfile();
@@ -154,19 +171,17 @@ public class UserIdentCertServiceImpl implements UserIdentCertService {
 		FaceidClient client = new FaceidClient(cred, "", clientProfile);
 		// 实例化一个请求对象,每个接口都会对应一个request对象
 		GetDetectInfoEnhancedRequest req = new GetDetectInfoEnhancedRequest();
-		req.setBizToken(bizToken);
+		req.setBizToken(userWxIndentDto.getBizToken());
 		req.setRuleId(ruleId);
 		// 返回的resp是一个GetDetectInfoEnhancedResponse的实例,与请求对象对应
 		GetDetectInfoEnhancedResponse resp = client.GetDetectInfoEnhanced(req);
 		DetectInfoText text = resp.getText();
-		Boolean flag = false;
 		if (text.getErrCode() == 0) {
 			UserCertRecord certRecord = new UserCertRecord();
 			String ocrIdCard = text.getOcrIdCard();
 			Integer count = userCertRecordMapper.selectCount(Wrappers.lambdaQuery(UserCertRecord.class)
 					.eq(UserCertRecord::getIdent, ocrIdCard));
 			if (count == 0) {
-				flag = true;
 				certRecord.setIdent(text.getOcrIdCard());
 				certRecord.setName(text.getOcrName());
 				certRecord.setSex(text.getOcrGender());
@@ -175,6 +190,5 @@ public class UserIdentCertServiceImpl implements UserIdentCertService {
 				userCertRecordMapper.insert(certRecord);
 			}
 		}
-		return flag;
 	}
 }

+ 20 - 5
netflix-web/src/main/java/com/cyksj/web/controller/user/UserCertController.java

@@ -1,15 +1,22 @@
 package com.cyksj.web.controller.user;
 
+import cn.hutool.core.util.StrUtil;
+import com.cyksj.common.util.Jsons;
 import com.cyksj.dto.Result;
 import com.cyksj.enums.GatewayResponse;
+import com.cyksj.mapper.UserMapper;
+import com.cyksj.model.dto.UserWxIndentDto;
 import com.cyksj.model.entity.UserCertRecord;
 import com.cyksj.model.request.UserIndentCertReq;
 import com.cyksj.model.response.WxCertAuthTokenResp;
+import com.cyksj.redis.RedisService;
 import com.cyksj.service.user.UserIdentCertService;
 import com.cyksj.web.util.StpUserUtil;
 import lombok.RequiredArgsConstructor;
 import org.springframework.web.bind.annotation.*;
 
+import javax.servlet.http.HttpServletResponse;
+
 /**
  * 项目名: yhlxj2
  * 文件名: UserCertController
@@ -23,6 +30,10 @@ public class UserCertController {
 
 	private final UserIdentCertService userIdentCertService;
 
+	private final RedisService redisService;
+
+	private final UserMapper userMapper;
+
 
 	/**
 	 * 用户是否已授权认证
@@ -59,11 +70,15 @@ public class UserCertController {
 	/**
 	 * 用户微信H5认证结果查询
 	 */
-	@PostMapping("/auth/result/{token}")
-	public Result<Boolean> wxH5IndentCertAuthResult(@PathVariable String token) throws Exception {
-		long userId = StpUserUtil.getLoginIdAsLong();
-		Boolean flag = userIdentCertService.wxH5IndentCertResult(userId, token);
-		return GatewayResponse.SUCCESS.newBuilder().toResult(flag);
+	@PostMapping("/auth/result/{mappingId}")
+	public void wxH5IndentCertAuthResult(@PathVariable String mappingId, HttpServletResponse response) throws Exception {
+		String redirectParams = redisService.getStr(mappingId);
+		if (StrUtil.isNotBlank(redirectParams)) {
+			UserWxIndentDto userWxIndentDto = Jsons.parseObject(redirectParams, UserWxIndentDto.class);
+			userIdentCertService.wxH5IndentCertResult(userWxIndentDto);
+			String url = userWxIndentDto.getRedirectUrl();
+			response.sendRedirect(url);
+		}
 	}
 
 	/**