USVT-125 完成微信地址的回调对象确定
This commit is contained in:
parent
88dfb4153b
commit
0d63e9d26c
|
@ -2,11 +2,10 @@ package com.northtecom.visatrack.api.controller.auth;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.northtecom.visatrack.api.controller.vo.ForgotPasswordRequest;
|
import com.northtecom.visatrack.api.model.entity.wechat.WeChatUser;
|
||||||
import com.northtecom.visatrack.api.controller.vo.UserChangePasswordResponse;
|
|
||||||
import com.northtecom.visatrack.api.data.entity.UserChangePassword;
|
|
||||||
import com.northtecom.visatrack.api.model.request.auth.WeChatTokenizeRequest;
|
import com.northtecom.visatrack.api.model.request.auth.WeChatTokenizeRequest;
|
||||||
import com.northtecom.visatrack.api.service.impl.UserService;
|
import com.northtecom.visatrack.api.service.impl.UserService;
|
||||||
|
import com.northtecom.visatrack.api.service.impl.WeChatService;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
@ -43,13 +42,15 @@ public class WeChatController {
|
||||||
|
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
|
private final WeChatService weChatService;
|
||||||
private final AuthenticationManager authenticationManager;
|
private final AuthenticationManager authenticationManager;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public WeChatController(ObjectMapper objectMapper, UserService userService, AuthenticationManager authenticationManager, BCryptPasswordEncoder encoder) {
|
public WeChatController(ObjectMapper objectMapper, UserService userService, AuthenticationManager authenticationManager, BCryptPasswordEncoder encoder, WeChatService weChatService) {
|
||||||
this.objectMapper = objectMapper;
|
this.objectMapper = objectMapper;
|
||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
this.authenticationManager = authenticationManager;
|
this.authenticationManager = authenticationManager;
|
||||||
|
this.weChatService = weChatService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -73,29 +74,9 @@ public class WeChatController {
|
||||||
@PostMapping("/tokenize")
|
@PostMapping("/tokenize")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@Operation(summary = "用户忘记密码接口", description = "用户忘记密码接口,用户忘记密码以后,通过该接口输入一个邮件地址,系统会给用户发送一封邮件,邮件中包含了重置密码的链接,用户点击以后,可以重置密码")
|
@Operation(summary = "用户忘记密码接口", description = "用户忘记密码接口,用户忘记密码以后,通过该接口输入一个邮件地址,系统会给用户发送一封邮件,邮件中包含了重置密码的链接,用户点击以后,可以重置密码")
|
||||||
public String getWeChatAccessToken(@RequestBody @Valid WeChatTokenizeRequest weChatTokenizeRequest) throws IOException {
|
public WeChatUser getWeChatAccessToken(@RequestBody @Valid WeChatTokenizeRequest weChatTokenizeRequest) throws IOException {
|
||||||
OkHttpClient client = new OkHttpClient();
|
WeChatUser weChatUser = weChatService.getWeChatUserInfo(weChatTokenizeRequest);
|
||||||
|
return weChatUser;
|
||||||
String weChatAppId = "wx26e01c2be46730f3";
|
|
||||||
String weChatSecret = "1eef340a999bc71b431dda85e9bffd3a";
|
|
||||||
String weChatCode = weChatTokenizeRequest.getWeChatCode();
|
|
||||||
String weChatState = weChatTokenizeRequest.getWeChatState();
|
|
||||||
|
|
||||||
HttpUrl.Builder urlBuilder = HttpUrl.parse("https://api.weixin.qq.com/sns/oauth2/access_token").newBuilder();
|
|
||||||
urlBuilder.addQueryParameter("appid", weChatAppId);
|
|
||||||
urlBuilder.addQueryParameter("secret", weChatSecret);
|
|
||||||
urlBuilder.addQueryParameter("code", weChatCode);
|
|
||||||
urlBuilder.addQueryParameter("grant_type", "authorization_code");
|
|
||||||
|
|
||||||
String url = urlBuilder.build().toString();
|
|
||||||
|
|
||||||
Request request = new Request.Builder().url(url).build();
|
|
||||||
Call call = client.newCall(request);
|
|
||||||
|
|
||||||
Response response = call.execute();
|
|
||||||
|
|
||||||
|
|
||||||
return url;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.northtecom.visatrack.api.model.entity.wechat;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class WeChatAccessToken {
|
||||||
|
@JsonProperty(value = "access_token", required = true)
|
||||||
|
private String accessToken;
|
||||||
|
|
||||||
|
@JsonProperty(value = "openid", required = true)
|
||||||
|
private String openId;
|
||||||
|
|
||||||
|
@JsonProperty(value = "unionid", required = true)
|
||||||
|
private String unionid;
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
package com.northtecom.visatrack.api.model.entity.wechat;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class WeChatUser {
|
||||||
|
|
||||||
|
@JsonProperty(value = "openid", required = true)
|
||||||
|
private String openId;
|
||||||
|
|
||||||
|
@JsonProperty(required = true)
|
||||||
|
private String nickname;
|
||||||
|
|
||||||
|
private Integer sex;
|
||||||
|
|
||||||
|
private String language;
|
||||||
|
|
||||||
|
private String city;
|
||||||
|
|
||||||
|
private String province;
|
||||||
|
|
||||||
|
private String country;
|
||||||
|
|
||||||
|
@JsonProperty(value = "headimgurl")
|
||||||
|
private String headImgURL;
|
||||||
|
|
||||||
|
@JsonProperty(value = "unionid", required = true)
|
||||||
|
private String unionId;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,144 @@
|
||||||
|
package com.northtecom.visatrack.api.service.impl;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.northtecom.visatrack.api.data.entity.VisaCase;
|
||||||
|
import com.northtecom.visatrack.api.data.repository.CaseVisaReportRepository;
|
||||||
|
import com.northtecom.visatrack.api.data.repository.UserRepository;
|
||||||
|
import com.northtecom.visatrack.api.data.repository.VisaCaseRepository;
|
||||||
|
import com.northtecom.visatrack.api.data.repository.VisaCheckeeCrawlDataRepository;
|
||||||
|
import com.northtecom.visatrack.api.model.entity.wechat.WeChatAccessToken;
|
||||||
|
import com.northtecom.visatrack.api.model.entity.wechat.WeChatUser;
|
||||||
|
import com.northtecom.visatrack.api.model.request.auth.WeChatTokenizeRequest;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import okhttp3.*;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @author XieYang, YuCheng
|
||||||
|
* @Date: 2022/10/14/16:17
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class WeChatService {
|
||||||
|
private final VisaCaseRepository visaCaseRepository;
|
||||||
|
private final CaseVisaReportRepository caseVisaReportRepository;
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
private final CrawlService crawlService;
|
||||||
|
private final VisaCheckeeCrawlDataRepository visaCheckeeCrawlDataRepository;
|
||||||
|
|
||||||
|
private final ObjectMapper objectMapper;
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public WeChatService(VisaCaseRepository visaCaseRepository, CaseVisaReportRepository caseVisaReportRepository, UserRepository userRepository, CrawlService crawlService, VisaCheckeeCrawlDataRepository visaCheckeeCrawlDataRepository, ObjectMapper objectMapper) {
|
||||||
|
this.visaCaseRepository = visaCaseRepository;
|
||||||
|
this.caseVisaReportRepository = caseVisaReportRepository;
|
||||||
|
this.userRepository = userRepository;
|
||||||
|
this.crawlService = crawlService;
|
||||||
|
this.visaCheckeeCrawlDataRepository = visaCheckeeCrawlDataRepository;
|
||||||
|
this.objectMapper = objectMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分页搜索签证数据
|
||||||
|
*
|
||||||
|
* @param search 搜索参数
|
||||||
|
* @return {@link Page}<{@link VisaCase}>
|
||||||
|
*/
|
||||||
|
public WeChatUser getWeChatUserInfo(WeChatTokenizeRequest weChatTokenizeRequest) {
|
||||||
|
OkHttpClient client = new OkHttpClient();
|
||||||
|
String response = StringUtils.EMPTY;
|
||||||
|
WeChatUser weChatUser = null;
|
||||||
|
|
||||||
|
|
||||||
|
String weChatCode = weChatTokenizeRequest.getWeChatCode();
|
||||||
|
String weChatState = weChatTokenizeRequest.getWeChatState();
|
||||||
|
|
||||||
|
try {
|
||||||
|
response = callWeChatAccessTokenAPI(client, weChatCode, weChatState);
|
||||||
|
WeChatAccessToken weChatAccessToken = objectMapper.readValue(response, WeChatAccessToken.class);
|
||||||
|
|
||||||
|
|
||||||
|
String accessToken = weChatAccessToken.getAccessToken();
|
||||||
|
String openId = weChatAccessToken.getOpenId();
|
||||||
|
response = callWeChatUserInfoAPI(client, accessToken, openId);
|
||||||
|
weChatUser = objectMapper.readValue(response, WeChatUser.class);
|
||||||
|
|
||||||
|
log.debug("WeChat NickName - [{}]", weChatUser.getNickname());
|
||||||
|
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
log.error("Get WeChat User Info Error.", e);
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
return weChatUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String callWeChatAccessTokenAPI(OkHttpClient client, String weChatCode, String weChatState) {
|
||||||
|
String responseStr;
|
||||||
|
String weChatAppId = "wx26e01c2be46730f3";
|
||||||
|
String weChatSecret = "1eef340a999bc71b431dda85e9bffd3a";
|
||||||
|
|
||||||
|
HttpUrl.Builder urlBuilder = HttpUrl.parse("https://api.weixin.qq.com/sns/oauth2/access_token").newBuilder();
|
||||||
|
urlBuilder.addQueryParameter("appid", weChatAppId);
|
||||||
|
urlBuilder.addQueryParameter("secret", weChatSecret);
|
||||||
|
urlBuilder.addQueryParameter("code", weChatCode);
|
||||||
|
urlBuilder.addQueryParameter("grant_type", "authorization_code");
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
Request request = new Request.Builder().url(urlBuilder.build().toString()).build();
|
||||||
|
Call call = client.newCall(request);
|
||||||
|
|
||||||
|
Response response = call.execute();
|
||||||
|
responseStr = response.body().string();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("Call WeChat get access token error.", e);
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return responseStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param client
|
||||||
|
* @param accessToken
|
||||||
|
* @param openId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private String callWeChatUserInfoAPI(OkHttpClient client, String accessToken, String openId) {
|
||||||
|
String responseStr;
|
||||||
|
|
||||||
|
|
||||||
|
HttpUrl.Builder urlBuilder = HttpUrl.parse("https://api.weixin.qq.com/sns/userinfo").newBuilder();
|
||||||
|
urlBuilder.addQueryParameter("access_token", accessToken);
|
||||||
|
urlBuilder.addQueryParameter("openid", openId);
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
Request request = new Request.Builder().url(urlBuilder.build().toString()).build();
|
||||||
|
Call call = client.newCall(request);
|
||||||
|
|
||||||
|
Response response = call.execute();
|
||||||
|
responseStr = response.body().string();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return responseStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue