配置 WeChat 的包的内容,尝试从 WeChat 包中调用需要的数据

This commit is contained in:
YuCheng Hu 2023-02-06 09:17:11 -05:00
parent 706f22397d
commit 4e90d872f7
15 changed files with 430 additions and 134 deletions

31
pom.xml
View File

@ -52,6 +52,23 @@
</properties> </properties>
<dependencies> <dependencies>
<!-- COM.OSSEZ -->
<dependency>
<groupId>com.ossez.openai</groupId>
<artifactId>openai-j-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.ossez.wechat</groupId>
<artifactId>wechat-j-open</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.ossez.wechat</groupId>
<artifactId>wechat-j-oa</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- SPRING -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId> <artifactId>spring-boot-starter-security</artifactId>
@ -64,6 +81,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId> <artifactId>spring-boot-devtools</artifactId>
@ -101,16 +122,6 @@
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
</dependency>
<dependency>
<groupId>com.ossez.openai</groupId>
<artifactId>openai-j-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- swagger --> <!-- swagger -->
<dependency> <dependency>

View File

@ -2,6 +2,7 @@ package com.northtecom.visatrack.api;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication @SpringBootApplication
public class VisaTrackApiApplication { public class VisaTrackApiApplication {

View File

@ -0,0 +1,47 @@
package com.northtecom.visatrack.api.config;
import com.ossez.wechat.common.config.ConfigStorage;
import com.ossez.wechat.open.api.WeChatOpenService;
import com.ossez.wechat.open.api.impl.WeChatOAuth2Service;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
*
*/
@Configuration
public class WeChatConfig {
private final ApplicationContext applicationContext;
public WeChatConfig(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Bean
@ConditionalOnMissingBean
public WeChatOpenService weChatOpenService(ConfigStorage configStorage) {
WeChatOpenService weChatOpenService = new WeChatOAuth2Service(configStorage.getAppId(),configStorage.getSecret());
weChatOpenService.setWxOpenConfigStorage(null);
return weChatOpenService;
}
@Bean
@ConditionalOnMissingBean
public WeChatOAuth2Service weChatOAuth2Service(ConfigStorage configStorage) {
String weChatAppId = configStorage.getAppId();
String weChatSecret = configStorage.getSecret();
WeChatOAuth2Service weChatOAuth2Service = new WeChatOAuth2Service(weChatAppId,weChatSecret);
weChatOAuth2Service.setWxOpenConfigStorage(null);
return weChatOAuth2Service;
}
}

View File

@ -0,0 +1,108 @@
package com.northtecom.visatrack.api.config;
import com.northtecom.visatrack.api.model.entity.wechat.WeChatDataStorage;
import com.northtecom.visatrack.api.properties.WeChatProperties;
import com.ossez.wechat.common.config.ConfigStorage;
import com.ossez.wechat.common.config.DefaultConfigStorage;
import com.ossez.wechat.common.config.RedisConfigStorage;
import com.ossez.wechat.common.config.WxMpHostConfig;
import com.ossez.wechat.common.redis.RedisTemplateWxRedisOps;
import com.ossez.wechat.common.redis.WxRedisOps;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.ossez.wechat.common.enums.WeChatStorageCategory;
import org.springframework.data.redis.core.StringRedisTemplate;
/**
*
*/
@Slf4j
@Configuration
@RequiredArgsConstructor
public class WeChatStorageAutoConfiguration {
private final ApplicationContext applicationContext;
private final WeChatProperties weChatProperties;
@Bean
@ConditionalOnMissingBean(ConfigStorage.class)
public ConfigStorage wxMpConfigStorage() {
WeChatStorageCategory type = weChatProperties.getWeChatDataStorage().getType();
ConfigStorage config;
switch (type) {
case REDIS:
config = redisTemplateConfigStorage();
break;
default:
config = defaultConfigStorage();
break;
}
// wx host config
if (null != weChatProperties.getHosts() && StringUtils.isNotEmpty(weChatProperties.getHosts().getApiHost())) {
WxMpHostConfig hostConfig = new WxMpHostConfig();
hostConfig.setApiHost(weChatProperties.getHosts().getApiHost());
hostConfig.setMpHost(weChatProperties.getHosts().getMpHost());
hostConfig.setOpenHost(weChatProperties.getHosts().getOpenHost());
config.setHostConfig(hostConfig);
}
return config;
}
private ConfigStorage defaultConfigStorage() {
DefaultConfigStorage config = new DefaultConfigStorage();
configWeChatServer(config);
return config;
}
private ConfigStorage redisTemplateConfigStorage() {
StringRedisTemplate redisTemplate = null;
try {
redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
try {
if (null == redisTemplate) {
redisTemplate = (StringRedisTemplate) applicationContext.getBean("stringRedisTemplate");
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
if (null == redisTemplate) {
redisTemplate = (StringRedisTemplate) applicationContext.getBean("redisTemplate");
}
WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate);
RedisConfigStorage redisConfigStorage = new RedisConfigStorage(redisOps,
weChatProperties.getWeChatDataStorage().getKeyPrefix());
configWeChatServer(redisConfigStorage);
return redisConfigStorage;
}
private void configWeChatServer(DefaultConfigStorage defaultConfigStorage) {
WeChatProperties properties = weChatProperties;
WeChatDataStorage weChatDataStorage = weChatProperties.getWeChatDataStorage();
defaultConfigStorage.setAppId(properties.getAppId());
defaultConfigStorage.setSecret(properties.getSecret());
defaultConfigStorage.setToken(properties.getToken());
defaultConfigStorage.setAesKey(properties.getAesKey());
defaultConfigStorage.setHttpProxyHost(weChatDataStorage.getHttpProxyHost());
defaultConfigStorage.setHttpProxyUsername(weChatDataStorage.getHttpProxyUsername());
defaultConfigStorage.setHttpProxyPassword(weChatDataStorage.getHttpProxyPassword());
if (weChatDataStorage.getHttpProxyPort() != null) {
defaultConfigStorage.setHttpProxyPort(weChatDataStorage.getHttpProxyPort());
}
}
}

View File

@ -9,7 +9,7 @@ import com.northtecom.visatrack.api.controller.vo.*;
import com.northtecom.visatrack.api.data.entity.User; import com.northtecom.visatrack.api.data.entity.User;
import com.northtecom.visatrack.api.data.entity.UserChangePassword; import com.northtecom.visatrack.api.data.entity.UserChangePassword;
import com.northtecom.visatrack.api.data.entity.WeChatCallState; import com.northtecom.visatrack.api.data.entity.WeChatCallState;
import com.northtecom.visatrack.api.model.entity.wechat.WeChatUser; import com.ossez.wechat.common.model.entity.WeChatUser;
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 com.northtecom.visatrack.api.service.impl.WeChatService;
import com.northtecom.visatrack.api.util.WeChatUtils; import com.northtecom.visatrack.api.util.WeChatUtils;

View File

@ -6,16 +6,14 @@ import com.northtecom.visatrack.api.controller.vo.BlogSearchVo;
import com.northtecom.visatrack.api.controller.vo.UserLoginResponse; import com.northtecom.visatrack.api.controller.vo.UserLoginResponse;
import com.northtecom.visatrack.api.controller.vo.VisaTrackUserDetail; import com.northtecom.visatrack.api.controller.vo.VisaTrackUserDetail;
import com.northtecom.visatrack.api.data.entity.User; import com.northtecom.visatrack.api.data.entity.User;
import com.northtecom.visatrack.api.model.entity.wechat.WeChatUser; import com.ossez.wechat.common.model.entity.WeChatOAuth2UserInfo;
import com.ossez.wechat.common.model.entity.WeChatUser;
import com.northtecom.visatrack.api.model.request.auth.WeChatTokenizeRequest; import com.northtecom.visatrack.api.model.request.auth.WeChatTokenizeRequest;
import com.northtecom.visatrack.api.model.request.auth.WeChatVerificationRequest;
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 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;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@ -29,7 +27,6 @@ import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.Valid; import javax.validation.Valid;
import java.io.IOException; import java.io.IOException;
import java.util.Date; import java.util.Date;
import java.util.UUID;
/** /**
* Created with IntelliJ IDEA. * Created with IntelliJ IDEA.
@ -72,17 +69,7 @@ public class WeChatController {
@ResponseBody @ResponseBody
@Operation(summary = "用户注册接口", description = "用户注册接口,用户注册以后会给用户发送一封邮件,邮件中包含了激活链接,用户点击激活链接以后,用户的账号才会被激活") @Operation(summary = "用户注册接口", description = "用户注册接口,用户注册以后会给用户发送一封邮件,邮件中包含了激活链接,用户点击激活链接以后,用户的账号才会被激活")
public String getWeChatLoginQR(BlogSearchVo blogSearchVo) throws JsonProcessingException { public String getWeChatLoginQR(BlogSearchVo blogSearchVo) throws JsonProcessingException {
log.debug("Get WeChat Login QR link"); return weChatService.getWeChatLoginQRUrl();
StringBuffer qrconnect = new StringBuffer();
qrconnect.append("https://open.weixin.qq.com/connect/qrconnect?");
qrconnect.append("appid=wx26e01c2be46730f3&");
qrconnect.append("redirect_uri=https%3A%2F%2Fwww.usvisatrack.com%2Fwechat%2Fcallback&");
qrconnect.append("response_type=code&");
qrconnect.append("scope=snsapi_login&");
qrconnect.append("state=" + UUID.randomUUID().toString() + "#wechat_redirect");
return qrconnect.toString();
} }
@PostMapping("/tokenize") @PostMapping("/tokenize")
@ -91,10 +78,10 @@ public class WeChatController {
public UserLoginResponse getWeChatAccessToken(@RequestBody @Valid WeChatTokenizeRequest weChatTokenizeRequest) throws IOException { public UserLoginResponse getWeChatAccessToken(@RequestBody @Valid WeChatTokenizeRequest weChatTokenizeRequest) throws IOException {
WeChatUser weChatUser = weChatService.getWeChatUserInfo(weChatTokenizeRequest.getWeChatCode(), weChatTokenizeRequest.getWeChatState()); WeChatOAuth2UserInfo weChatOAuth2UserInfo = weChatService.getWeChatUserInfo(weChatTokenizeRequest.getWeChatCode(), weChatTokenizeRequest.getWeChatState());
String weChatOpenId = weChatUser.getOpenId(); String weChatOpenId = weChatOAuth2UserInfo.getOpenid();
String weChatUnionId = weChatUser.getUnionId(); String weChatUnionId = weChatOAuth2UserInfo.getUnionId();
User user = userService.getUserByWeChat(weChatOpenId,weChatUnionId); User user = userService.getUserByWeChat(weChatOpenId,weChatUnionId);
String userEmail = user.getUserEmail(); String userEmail = user.getUserEmail();

View File

@ -0,0 +1,77 @@
package com.northtecom.visatrack.api.model.entity.wechat;
import com.northtecom.visatrack.api.properties.RedisProperties;
import com.ossez.wechat.common.enums.WeChatStorageCategory;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import java.io.Serializable;
import static com.ossez.wechat.common.enums.WeChatStorageCategory.MEM;
/**
*
*/
public class WeChatDataStorage implements Serializable {
private static final long serialVersionUID = -94405301936095366L;
private WeChatStorageCategory type = MEM;
private String keyPrefix = "wx";
@NestedConfigurationProperty
private final RedisProperties redis = new RedisProperties();
private String httpProxyHost;
private Integer httpProxyPort;
private String httpProxyUsername;
private String httpProxyPassword;
public WeChatStorageCategory getType() {
return type;
}
public void setType(WeChatStorageCategory type) {
this.type = type;
}
public String getKeyPrefix() {
return keyPrefix;
}
public void setKeyPrefix(String keyPrefix) {
this.keyPrefix = keyPrefix;
}
public RedisProperties getRedis() {
return redis;
}
public String getHttpProxyHost() {
return httpProxyHost;
}
public void setHttpProxyHost(String httpProxyHost) {
this.httpProxyHost = httpProxyHost;
}
public Integer getHttpProxyPort() {
return httpProxyPort;
}
public void setHttpProxyPort(Integer httpProxyPort) {
this.httpProxyPort = httpProxyPort;
}
public String getHttpProxyUsername() {
return httpProxyUsername;
}
public void setHttpProxyUsername(String httpProxyUsername) {
this.httpProxyUsername = httpProxyUsername;
}
public String getHttpProxyPassword() {
return httpProxyPassword;
}
public void setHttpProxyPassword(String httpProxyPassword) {
this.httpProxyPassword = httpProxyPassword;
}
}

View File

@ -1,35 +0,0 @@
package com.northtecom.visatrack.api.model.entity.wechat;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
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;
}

View File

@ -1,32 +1,7 @@
package com.northtecom.visatrack.api.model.request.auth; package com.northtecom.visatrack.api.model.request.auth;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.northtecom.visatrack.api.controller.vo.BlogSearchVo;
import com.northtecom.visatrack.api.controller.vo.UserLoginResponse;
import com.northtecom.visatrack.api.controller.vo.VisaTrackUserDetail;
import com.northtecom.visatrack.api.data.entity.User;
import com.northtecom.visatrack.api.model.entity.wechat.WeChatUser;
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.tags.Tag;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;
/** /**
* *

View File

@ -0,0 +1,56 @@
package com.northtecom.visatrack.api.properties;
import lombok.Data;
import java.io.Serializable;
/**
* redis 配置属性.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* created on 2020-08-30
*/
@Data
public class RedisProperties implements Serializable {
private static final long serialVersionUID = -5924815351660074401L;
/**
* 主机地址.
*/
private String host = "127.0.0.1";
/**
* 端口号.
*/
private int port = 6379;
/**
* 密码.
*/
private String password;
/**
* 超时.
*/
private int timeout = 2000;
/**
* 数据库.
*/
private int database = 0;
/**
* sentinel ips
*/
private String sentinelIps;
/**
* sentinel name
*/
private String sentinelName;
private Integer maxActive;
private Integer maxIdle;
private Integer maxWaitMillis;
private Integer minIdle;
}

View File

@ -0,0 +1,27 @@
package com.northtecom.visatrack.api.properties;
import com.northtecom.visatrack.api.model.entity.wechat.WeChatDataStorage;
import com.ossez.wechat.common.model.WeChatHost;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* WeChat Official Account Config
*
* @author YuCheng Hu
*/
@Data
@Component
@ConfigurationProperties(prefix = "wechat.official-account")
public class WeChatProperties {
private String appId; //微信公众号 appId
private String secret; //微信公众号 secret
private String token; //微信公众号 token
private String aesKey; //微信公众号 aesKey
private WeChatHost hosts; //自定义 host 配置
private final WeChatDataStorage weChatDataStorage = new WeChatDataStorage();
}

View File

@ -11,7 +11,7 @@ import com.northtecom.visatrack.api.data.entity.VisaCase;
import com.northtecom.visatrack.api.data.repository.UserChangePasswordRepository; import com.northtecom.visatrack.api.data.repository.UserChangePasswordRepository;
import com.northtecom.visatrack.api.data.repository.UserRepository; import com.northtecom.visatrack.api.data.repository.UserRepository;
import com.northtecom.visatrack.api.data.repository.VisaCaseRepository; import com.northtecom.visatrack.api.data.repository.VisaCaseRepository;
import com.northtecom.visatrack.api.model.entity.wechat.WeChatUser; import com.ossez.wechat.common.model.entity.WeChatUser;
import com.northtecom.visatrack.api.service.enums.UserStatus; import com.northtecom.visatrack.api.service.enums.UserStatus;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;

View File

@ -1,27 +1,31 @@
package com.northtecom.visatrack.api.service.impl; package com.northtecom.visatrack.api.service.impl;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.northtecom.visatrack.api.data.entity.VisaCase;
import com.northtecom.visatrack.api.data.entity.WeChatCallState; import com.northtecom.visatrack.api.data.entity.WeChatCallState;
import com.northtecom.visatrack.api.data.repository.*; import com.northtecom.visatrack.api.data.repository.*;
import com.northtecom.visatrack.api.model.entity.wechat.WeChatAccessToken; import com.northtecom.visatrack.api.model.entity.wechat.WeChatAccessToken;
import com.northtecom.visatrack.api.model.entity.wechat.WeChatMessage; import com.northtecom.visatrack.api.model.entity.wechat.WeChatMessage;
import com.northtecom.visatrack.api.model.entity.wechat.WeChatUser; import com.ossez.wechat.common.exception.WxErrorException;
import com.ossez.wechat.common.model.entity.WeChatOAuth2UserInfo;
import com.ossez.wechat.common.model.entity.WeChatUser;
import com.northtecom.visatrack.api.util.WeChatUtils; import com.northtecom.visatrack.api.util.WeChatUtils;
import com.ossez.openai.OpenAiService; import com.ossez.openai.OpenAiService;
import com.ossez.openai.completion.CompletionRequest; import com.ossez.openai.completion.CompletionRequest;
import com.ossez.openai.completion.CompletionRequestBuilder; import com.ossez.openai.completion.CompletionRequestBuilder;
import com.ossez.wechat.open.api.WeChatOpenService;
import com.ossez.wechat.open.api.impl.WeChatOAuth2Service;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import okhttp3.*; import okhttp3.*;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException; import java.io.IOException;
import java.time.Instant; import java.time.Instant;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.UUID;
/** /**
* Created with IntelliJ IDEA. * Created with IntelliJ IDEA.
@ -34,6 +38,7 @@ import java.util.Optional;
@Slf4j @Slf4j
public class WeChatService { public class WeChatService {
private final WeChatOAuth2Service weChatOAuth2Service;
private final WeChatUtils weChatUtils; private final WeChatUtils weChatUtils;
private final VisaCaseRepository visaCaseRepository; private final VisaCaseRepository visaCaseRepository;
@ -46,12 +51,13 @@ public class WeChatService {
@Autowired @Autowired
public WeChatService(WeChatUtils weChatUtils, VisaCaseRepository visaCaseRepository, public WeChatService( WeChatOAuth2Service weChatOAuth2Service, WeChatUtils weChatUtils, VisaCaseRepository visaCaseRepository,
WeChatCallStateRepository weChatCallStateRepository, WeChatCallStateRepository weChatCallStateRepository,
UserRepository userRepository, UserRepository userRepository,
CrawlService crawlService, CrawlService crawlService,
VisaCheckeeCrawlDataRepository visaCheckeeCrawlDataRepository, VisaCheckeeCrawlDataRepository visaCheckeeCrawlDataRepository,
ObjectMapper objectMapper) { ObjectMapper objectMapper) {
this.weChatOAuth2Service = weChatOAuth2Service;
this.weChatUtils = weChatUtils; this.weChatUtils = weChatUtils;
this.visaCaseRepository = visaCaseRepository; this.visaCaseRepository = visaCaseRepository;
this.weChatCallStateRepository = weChatCallStateRepository; this.weChatCallStateRepository = weChatCallStateRepository;
@ -63,43 +69,72 @@ public class WeChatService {
/** /**
* Get WeChatLoginQRUrl
* *
* @return
*/
public String getWeChatLoginQRUrl() {
Map<String, String> wechatParameterConfMap = WeChatUtils.wechatParameterConfMap;
HttpUrl httpUrl = HttpUrl.parse("https://open.weixin.qq.com/connect/qrconnect").newBuilder()
.addQueryParameter("appid", wechatParameterConfMap.get(WeChatUtils.WECHAT_OFFICIAL_ACCOUNT_APP_ID))
.addQueryParameter("redirect_uri", "https://www.usvisatrack.com/wechat/callback")
.addQueryParameter("response_type", "code")
.addQueryParameter("scope", "snsapi_login")
.addQueryParameter("state", UUID.randomUUID().toString() + "#wechat_redirect")
.build();
String urlStr = httpUrl.url().toString();
log.debug("Get WeChat Login QR link - [{}]", urlStr);
return urlStr;
}
/**
* @param weChatCode * @param weChatCode
* @param weChatState * @param weChatState
* @return * @return
* @throws IOException * @throws IOException
*/ */
public WeChatUser getWeChatUserInfo(String weChatCode, String weChatState) throws IOException { public WeChatOAuth2UserInfo getWeChatUserInfo(String weChatCode, String weChatState) throws IOException {
OkHttpClient client = new OkHttpClient(); WeChatOAuth2UserInfo weChatOAuth2UserInfo = new WeChatOAuth2UserInfo();
String response = StringUtils.EMPTY; try {
WeChatUser weChatUser = null; weChatOAuth2UserInfo = weChatOAuth2Service.getWeChatUserInfo(weChatCode,weChatState);
} catch (WxErrorException e) {
throw new RuntimeException(e);
response = callWeChatAccessTokenAPI(client, weChatCode, weChatState);
WeChatAccessToken weChatAccessToken = objectMapper.readValue(response, WeChatAccessToken.class);
String accessToken = weChatAccessToken.getAccessToken();
String openId = weChatAccessToken.getOpenId();
if (StringUtils.isNoneEmpty(accessToken, openId)) {
response = callWeChatUserInfoAPI(client, accessToken, openId);
weChatUser = objectMapper.readValue(response, WeChatUser.class);
log.debug("WeChat NickName - [{}]", weChatUser.getNickname());
WeChatCallState weChatCallState = new WeChatCallState();
weChatCallState.setWeChatState(weChatState);
weChatCallState.setWeChatCode(weChatCode);
weChatCallState.setWeChatAccessToken(accessToken);
weChatCallState.setWeChatOpenId(weChatUser.getOpenId());
weChatCallState.setWeChatUnionId(weChatUser.getUnionId());
weChatCallState.setNickName(weChatUser.getNickname());
weChatCallState.setHeadImgURL(weChatUser.getHeadImgURL());
weChatCallStateRepository.save(weChatCallState);
} }
return weChatOAuth2UserInfo;
return weChatUser; // OkHttpClient client = new OkHttpClient();
// String response = StringUtils.EMPTY;
// WeChatUser weChatUser = null;
//
//
// response = callWeChatAccessTokenAPI(client, weChatCode, weChatState);
// WeChatAccessToken weChatAccessToken = objectMapper.readValue(response, WeChatAccessToken.class);
//
//
// String accessToken = weChatAccessToken.getAccessToken();
// String openId = weChatAccessToken.getOpenId();
//
// if (StringUtils.isNoneEmpty(accessToken, openId)) {
// response = callWeChatUserInfoAPI(client, accessToken, openId);
// weChatUser = objectMapper.readValue(response, WeChatUser.class);
//
// log.debug("WeChat NickName - [{}]", weChatUser.getNickname());
// WeChatCallState weChatCallState = new WeChatCallState();
// weChatCallState.setWeChatState(weChatState);
// weChatCallState.setWeChatCode(weChatCode);
// weChatCallState.setWeChatAccessToken(accessToken);
// weChatCallState.setWeChatOpenId(weChatUser.getOpenId());
// weChatCallState.setWeChatUnionId(weChatUser.getUnionId());
// weChatCallState.setNickName(weChatUser.getNickname());
// weChatCallState.setHeadImgURL(weChatUser.getHeadImgURL());
// weChatCallStateRepository.save(weChatCallState);
// }
//
//
// return weChatUser;
} }
public Optional<WeChatCallState> getWeChatCallState(String weChatState, String weChatCode) { public Optional<WeChatCallState> getWeChatCallState(String weChatState, String weChatCode) {

View File

@ -7,7 +7,8 @@ package com.northtecom.visatrack.api.util;
import com.northtecom.visatrack.api.data.entity.WeChatCallState; import com.northtecom.visatrack.api.data.entity.WeChatCallState;
import com.northtecom.visatrack.api.model.entity.wechat.WeChatMessage; import com.northtecom.visatrack.api.model.entity.wechat.WeChatMessage;
import com.northtecom.visatrack.api.model.entity.wechat.WeChatUser; import com.ossez.wechat.common.model.entity.WeChatUser;
import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
@ -15,7 +16,6 @@ import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.math.NumberUtils;
import org.dom4j.Document; import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper; import org.dom4j.DocumentHelper;
import org.dom4j.Element; import org.dom4j.Element;
import org.dom4j.io.SAXReader; import org.dom4j.io.SAXReader;
@ -32,6 +32,7 @@ import java.util.Map;
* @author YuCheng Hu * @author YuCheng Hu
*/ */
@Slf4j @Slf4j
@Data
@Component @Component
public class WeChatUtils { public class WeChatUtils {
@ -48,7 +49,7 @@ public class WeChatUtils {
public static final String WECHAT_OFFICIAL_ACCOUNT_VERIFICATION_TOKEN = "official_account_verification_token"; public static final String WECHAT_OFFICIAL_ACCOUNT_VERIFICATION_TOKEN = "official_account_verification_token";
private Map<String, String> wechatParameterConfMap = new HashMap<String, String>(); public static Map<String, String> wechatParameterConfMap = new HashMap<String, String>();
@Autowired @Autowired
public WeChatUtils(AwsUtils awsUtils) { public WeChatUtils(AwsUtils awsUtils) {
@ -69,6 +70,7 @@ public class WeChatUtils {
} }
public String getWechatVerificationSignature(String timestamp, String nonce) { public String getWechatVerificationSignature(String timestamp, String nonce) {
StringBuffer strToSHA1 = new StringBuffer(); StringBuffer strToSHA1 = new StringBuffer();
strToSHA1.append(wechatParameterConfMap.get(WECHAT_OFFICIAL_ACCOUNT_VERIFICATION_TOKEN)); strToSHA1.append(wechatParameterConfMap.get(WECHAT_OFFICIAL_ACCOUNT_VERIFICATION_TOKEN));
@ -85,10 +87,10 @@ public class WeChatUtils {
*/ */
public static WeChatUser covertToWeChatUser(WeChatCallState weChatCallState) { public static WeChatUser covertToWeChatUser(WeChatCallState weChatCallState) {
WeChatUser weChatUser = new WeChatUser(); WeChatUser weChatUser = new WeChatUser();
weChatUser.setOpenId(weChatCallState.getWeChatOpenId()) // weChatUser.setOpenId(weChatCallState.getWeChatOpenId())
.setUnionId(weChatCallState.getWeChatUnionId()) // .setUnionId(weChatCallState.getWeChatUnionId())
.setNickname(weChatCallState.getNickName()) // .setNickname(weChatCallState.getNickName())
.setHeadImgURL(weChatCallState.getHeadImgURL()); // .setHeadImgURL(weChatCallState.getHeadImgURL());
return weChatUser; return weChatUser;
} }

View File

@ -1,10 +1,10 @@
app: app:
database: database:
host: 54.39.157.60 host: 158.69.254.99
port: 3306 port: 3306
name: usvisatrack name: usvisatrack
username: root username: yhu
password: ETNN0sqc1qMbgQaeGKWL password: hulinghyc
web: web:
host: 8282 host: 8282
@ -82,4 +82,9 @@ jwt:
ttl: 7200000 ttl: 7200000
remember: 604800000 remember: 604800000
wechat:
official-account:
app-id: wx637b82a7f94123ef
secret: 343cecbc44d69e45367a65cc9b4d3925
token: 0b01a700891d4a2f93a4323771c32455
aes-key: aesKey