配置 WeChat 的包的内容,尝试从 WeChat 包中调用需要的数据
This commit is contained in:
parent
706f22397d
commit
4e90d872f7
31
pom.xml
31
pom.xml
|
@ -52,6 +52,23 @@
|
|||
</properties>
|
||||
|
||||
<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>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
|
@ -64,6 +81,10 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
|
@ -101,16 +122,6 @@
|
|||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</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 -->
|
||||
<dependency>
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.northtecom.visatrack.api;
|
|||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
public class VisaTrackApiApplication {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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.UserChangePassword;
|
||||
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.WeChatService;
|
||||
import com.northtecom.visatrack.api.util.WeChatUtils;
|
||||
|
|
|
@ -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.VisaTrackUserDetail;
|
||||
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.WeChatVerificationRequest;
|
||||
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.extern.slf4j.Slf4j;
|
||||
import okhttp3.*;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
|
@ -29,7 +27,6 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|||
import javax.validation.Valid;
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
|
@ -72,17 +69,7 @@ public class WeChatController {
|
|||
@ResponseBody
|
||||
@Operation(summary = "用户注册接口", description = "用户注册接口,用户注册以后会给用户发送一封邮件,邮件中包含了激活链接,用户点击激活链接以后,用户的账号才会被激活")
|
||||
public String getWeChatLoginQR(BlogSearchVo blogSearchVo) throws JsonProcessingException {
|
||||
log.debug("Get WeChat Login QR link");
|
||||
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();
|
||||
return weChatService.getWeChatLoginQRUrl();
|
||||
}
|
||||
|
||||
@PostMapping("/tokenize")
|
||||
|
@ -91,10 +78,10 @@ public class WeChatController {
|
|||
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 weChatUnionId = weChatUser.getUnionId();
|
||||
String weChatOpenId = weChatOAuth2UserInfo.getOpenid();
|
||||
String weChatUnionId = weChatOAuth2UserInfo.getUnionId();
|
||||
|
||||
User user = userService.getUserByWeChat(weChatOpenId,weChatUnionId);
|
||||
String userEmail = user.getUserEmail();
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -1,32 +1,7 @@
|
|||
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.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;
|
||||
|
||||
/**
|
||||
*
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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();
|
||||
|
||||
}
|
|
@ -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.UserRepository;
|
||||
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 lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
|
|
|
@ -1,27 +1,31 @@
|
|||
package com.northtecom.visatrack.api.service.impl;
|
||||
|
||||
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.repository.*;
|
||||
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.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.ossez.openai.OpenAiService;
|
||||
import com.ossez.openai.completion.CompletionRequest;
|
||||
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 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;
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
|
@ -34,6 +38,7 @@ import java.util.Optional;
|
|||
@Slf4j
|
||||
public class WeChatService {
|
||||
|
||||
private final WeChatOAuth2Service weChatOAuth2Service;
|
||||
private final WeChatUtils weChatUtils;
|
||||
private final VisaCaseRepository visaCaseRepository;
|
||||
|
||||
|
@ -46,12 +51,13 @@ public class WeChatService {
|
|||
|
||||
|
||||
@Autowired
|
||||
public WeChatService(WeChatUtils weChatUtils, VisaCaseRepository visaCaseRepository,
|
||||
public WeChatService( WeChatOAuth2Service weChatOAuth2Service, WeChatUtils weChatUtils, VisaCaseRepository visaCaseRepository,
|
||||
WeChatCallStateRepository weChatCallStateRepository,
|
||||
UserRepository userRepository,
|
||||
CrawlService crawlService,
|
||||
VisaCheckeeCrawlDataRepository visaCheckeeCrawlDataRepository,
|
||||
ObjectMapper objectMapper) {
|
||||
this.weChatOAuth2Service = weChatOAuth2Service;
|
||||
this.weChatUtils = weChatUtils;
|
||||
this.visaCaseRepository = visaCaseRepository;
|
||||
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 weChatState
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public WeChatUser getWeChatUserInfo(String weChatCode, String weChatState) throws IOException {
|
||||
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);
|
||||
public WeChatOAuth2UserInfo getWeChatUserInfo(String weChatCode, String weChatState) throws IOException {
|
||||
WeChatOAuth2UserInfo weChatOAuth2UserInfo = new WeChatOAuth2UserInfo();
|
||||
try {
|
||||
weChatOAuth2UserInfo = weChatOAuth2Service.getWeChatUserInfo(weChatCode,weChatState);
|
||||
} catch (WxErrorException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
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) {
|
||||
|
|
|
@ -7,7 +7,8 @@ package com.northtecom.visatrack.api.util;
|
|||
|
||||
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.WeChatUser;
|
||||
import com.ossez.wechat.common.model.entity.WeChatUser;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
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.math.NumberUtils;
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.DocumentException;
|
||||
import org.dom4j.DocumentHelper;
|
||||
import org.dom4j.Element;
|
||||
import org.dom4j.io.SAXReader;
|
||||
|
@ -32,6 +32,7 @@ import java.util.Map;
|
|||
* @author YuCheng Hu
|
||||
*/
|
||||
@Slf4j
|
||||
@Data
|
||||
@Component
|
||||
public class WeChatUtils {
|
||||
|
||||
|
@ -48,7 +49,7 @@ public class WeChatUtils {
|
|||
|
||||
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
|
||||
public WeChatUtils(AwsUtils awsUtils) {
|
||||
|
@ -69,6 +70,7 @@ public class WeChatUtils {
|
|||
|
||||
}
|
||||
|
||||
|
||||
public String getWechatVerificationSignature(String timestamp, String nonce) {
|
||||
StringBuffer strToSHA1 = new StringBuffer();
|
||||
strToSHA1.append(wechatParameterConfMap.get(WECHAT_OFFICIAL_ACCOUNT_VERIFICATION_TOKEN));
|
||||
|
@ -85,10 +87,10 @@ public class WeChatUtils {
|
|||
*/
|
||||
public static WeChatUser covertToWeChatUser(WeChatCallState weChatCallState) {
|
||||
WeChatUser weChatUser = new WeChatUser();
|
||||
weChatUser.setOpenId(weChatCallState.getWeChatOpenId())
|
||||
.setUnionId(weChatCallState.getWeChatUnionId())
|
||||
.setNickname(weChatCallState.getNickName())
|
||||
.setHeadImgURL(weChatCallState.getHeadImgURL());
|
||||
// weChatUser.setOpenId(weChatCallState.getWeChatOpenId())
|
||||
// .setUnionId(weChatCallState.getWeChatUnionId())
|
||||
// .setNickname(weChatCallState.getNickName())
|
||||
// .setHeadImgURL(weChatCallState.getHeadImgURL());
|
||||
|
||||
return weChatUser;
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
app:
|
||||
database:
|
||||
host: 54.39.157.60
|
||||
host: 158.69.254.99
|
||||
port: 3306
|
||||
name: usvisatrack
|
||||
username: root
|
||||
password: ETNN0sqc1qMbgQaeGKWL
|
||||
username: yhu
|
||||
password: hulinghyc
|
||||
web:
|
||||
host: 8282
|
||||
|
||||
|
@ -82,4 +82,9 @@ jwt:
|
|||
ttl: 7200000
|
||||
remember: 604800000
|
||||
|
||||
|
||||
wechat:
|
||||
official-account:
|
||||
app-id: wx637b82a7f94123ef
|
||||
secret: 343cecbc44d69e45367a65cc9b4d3925
|
||||
token: 0b01a700891d4a2f93a4323771c32455
|
||||
aes-key: aesKey
|
||||
|
|
Loading…
Reference in New Issue