From 5a20159e6ae70df1749c248f8f558055a183c893 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Wed, 8 Feb 2023 11:38:21 -0500 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BD=BF=E7=94=A8=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E7=9A=84=20Open=EF=BC=8C=E5=9B=A0=E4=B8=BA=E6=88=91?= =?UTF-8?q?=E4=BB=AC=E9=9C=80=E8=A6=81=E4=BD=BF=E7=94=A8=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E7=9A=84=20ID=20=E6=9D=A5=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../visatrack/api/config/WeChatConfig.java | 7 +- .../WeChatStorageAutoConfiguration.java | 2 + .../api/properties/WeChatProperties.java | 6 ++ .../api/service/impl/WeChatService.java | 72 +++++++++---------- .../visatrack/api/util/WeChatUtils.java | 7 ++ src/main/resources/application.yaml | 5 ++ 6 files changed, 58 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/northtecom/visatrack/api/config/WeChatConfig.java b/src/main/java/com/northtecom/visatrack/api/config/WeChatConfig.java index efc2ea7..b9aed2a 100644 --- a/src/main/java/com/northtecom/visatrack/api/config/WeChatConfig.java +++ b/src/main/java/com/northtecom/visatrack/api/config/WeChatConfig.java @@ -27,7 +27,7 @@ public class WeChatConfig { @ConditionalOnMissingBean public WeChatOpenService weChatOpenService(ConfigStorage configStorage) { - WeChatOpenService weChatOpenService = new WeChatOAuth2Service(configStorage.getAppId(),configStorage.getSecret()); + WeChatOpenService weChatOpenService = new WeChatOAuth2Service(configStorage.getOpenAppId(), configStorage.getOpenSecret(), configStorage.getAppId(), configStorage.getSecret()); weChatOpenService.setWxOpenConfigStorage(null); return weChatOpenService; @@ -36,10 +36,13 @@ public class WeChatConfig { @Bean @ConditionalOnMissingBean public WeChatOAuth2Service weChatOAuth2Service(ConfigStorage configStorage) { + String weChatOpenAppId = configStorage.getOpenAppId(); + String weChatOpenSecret = configStorage.getOpenSecret(); + String weChatAppId = configStorage.getAppId(); String weChatSecret = configStorage.getSecret(); - WeChatOAuth2Service weChatOAuth2Service = new WeChatOAuth2Service(weChatAppId,weChatSecret); + WeChatOAuth2Service weChatOAuth2Service = new WeChatOAuth2Service(weChatOpenAppId, weChatOpenSecret, weChatAppId, weChatSecret); weChatOAuth2Service.setWxOpenConfigStorage(null); return weChatOAuth2Service; } diff --git a/src/main/java/com/northtecom/visatrack/api/config/WeChatStorageAutoConfiguration.java b/src/main/java/com/northtecom/visatrack/api/config/WeChatStorageAutoConfiguration.java index 02e6709..c07e8a5 100644 --- a/src/main/java/com/northtecom/visatrack/api/config/WeChatStorageAutoConfiguration.java +++ b/src/main/java/com/northtecom/visatrack/api/config/WeChatStorageAutoConfiguration.java @@ -92,6 +92,8 @@ public class WeChatStorageAutoConfiguration { WeChatProperties properties = weChatProperties; WeChatDataStorage weChatDataStorage = weChatProperties.getWeChatDataStorage(); + defaultConfigStorage.setOpenAppId(properties.getOpenAppId()); + defaultConfigStorage.setOpenSecret(properties.getOpenSecret()); defaultConfigStorage.setAppId(properties.getAppId()); defaultConfigStorage.setSecret(properties.getSecret()); defaultConfigStorage.setToken(properties.getToken()); diff --git a/src/main/java/com/northtecom/visatrack/api/properties/WeChatProperties.java b/src/main/java/com/northtecom/visatrack/api/properties/WeChatProperties.java index e821550..c6f9a1d 100644 --- a/src/main/java/com/northtecom/visatrack/api/properties/WeChatProperties.java +++ b/src/main/java/com/northtecom/visatrack/api/properties/WeChatProperties.java @@ -5,6 +5,7 @@ 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.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @@ -17,6 +18,11 @@ import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "wechat.official-account") public class WeChatProperties { + + @Value("${wechat.open.app-id}") + private String openAppId; + @Value("${wechat.open.secret}") + private String openSecret; private String appId; //微信公众号 appId private String secret; //微信公众号 secret private String token; //微信公众号 token diff --git a/src/main/java/com/northtecom/visatrack/api/service/impl/WeChatService.java b/src/main/java/com/northtecom/visatrack/api/service/impl/WeChatService.java index 6fed321..b5b02c7 100644 --- a/src/main/java/com/northtecom/visatrack/api/service/impl/WeChatService.java +++ b/src/main/java/com/northtecom/visatrack/api/service/impl/WeChatService.java @@ -21,6 +21,9 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.concurrent.Executors; +import java.util.concurrent.*; + import java.io.IOException; import java.time.Instant; import java.util.HashMap; @@ -52,12 +55,7 @@ public class WeChatService { @Autowired - public WeChatService( WeChatOAuth2Service weChatOAuth2Service, WeChatUtils weChatUtils, VisaCaseRepository visaCaseRepository, - WeChatCallStateRepository weChatCallStateRepository, - UserRepository userRepository, - CrawlService crawlService, - VisaCheckeeCrawlDataRepository visaCheckeeCrawlDataRepository, - ObjectMapper objectMapper) { + 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; @@ -77,7 +75,7 @@ public class WeChatService { public String getWeChatLoginQRUrl() { Map 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("appid", wechatParameterConfMap.get(WeChatUtils.WECHAT_OPEN_APP_ID)) .addQueryParameter("redirect_uri", "https://www.usvisatrack.com/wechat/callback") .addQueryParameter("response_type", "code") .addQueryParameter("scope", "snsapi_login") @@ -99,7 +97,7 @@ public class WeChatService { public WeChatOAuth2UserInfo getWeChatUserInfo(String weChatCode, String weChatState) throws IOException { WeChatOAuth2UserInfo weChatOAuth2UserInfo = new WeChatOAuth2UserInfo(); try { - weChatOAuth2UserInfo = weChatOAuth2Service.getWeChatUserInfo(weChatCode,weChatState); + weChatOAuth2UserInfo = weChatOAuth2Service.getWeChatUserInfo(weChatCode, weChatState); } catch (WxErrorException e) { throw new RuntimeException(e); } @@ -154,52 +152,48 @@ public class WeChatService { String toUserName = weChatMessage.getFromUserName(); String fromUserName = weChatMessage.getToUserName(); String content = weChatMessage.getContent(); + String contentGPT = StringUtils.EMPTY; - OpenAiService service = new OpenAiService("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"); - CompletionRequest completionRequest = new CompletionRequestBuilder() - .setModel("text-davinci-003") - .setPrompt(content) - .setMaxTokens(500) - .setEcho(true) - .setUser("testing") - .setLogitBias(new HashMap<>()) - .createCompletionRequest(); - - -// service.createCompletion(completionRequest).getChoices().size(); - content = service.createCompletion(completionRequest).getChoices().get(0).getText(); + ExecutorService executor = Executors.newCachedThreadPool(); + Callable task = new Callable() { + public String call() { + return callChatGPT(content); + } + }; + Future future = executor.submit(task); + try { + Object result = future.get(3, TimeUnit.SECONDS); + contentGPT = (String) result; + } catch (TimeoutException ex) { + // handle the timeout + } catch (InterruptedException e) { + // handle the interrupts + } catch (ExecutionException e) { + // handle other exceptions + } finally { + future.cancel(true); // may or may not desire this + } weChatMessage.setToUserName(toUserName); weChatMessage.setFromUserName(fromUserName); - weChatMessage.setContent(content); + weChatMessage.setContent(contentGPT); weChatMessage.setCreateTime(Instant.now().toEpochMilli()); return WeChatUtils.covertToWeChatMessageXmlStr(weChatMessage); } - private String callWeChatAccessTokenAPI(OkHttpClient client, String weChatCode, String weChatState) throws IOException { - 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"); + private String callChatGPT(String content) { + OpenAiService service = new OpenAiService("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"); + CompletionRequest completionRequest = new CompletionRequestBuilder().setModel("text-davinci-003").setPrompt(content).setMaxTokens(500).setEcho(true).setUser("testing").setLogitBias(new HashMap<>()).createCompletionRequest(); - Request request = new Request.Builder().url(urlBuilder.build().toString()).build(); - Call call = client.newCall(request); +// service.createCompletion(completionRequest).getChoices().size(); + content = service.createCompletion(completionRequest).getChoices().get(0).getText(); - Response response = call.execute(); - responseStr = response.body().string(); - - return responseStr; + return content; } - /** * @param client * @param accessToken diff --git a/src/main/java/com/northtecom/visatrack/api/util/WeChatUtils.java b/src/main/java/com/northtecom/visatrack/api/util/WeChatUtils.java index 93a2606..53332ff 100644 --- a/src/main/java/com/northtecom/visatrack/api/util/WeChatUtils.java +++ b/src/main/java/com/northtecom/visatrack/api/util/WeChatUtils.java @@ -41,6 +41,9 @@ public class WeChatUtils { public static final String WECHAT_API_KEY = "appid"; public static final String WECHAT_API_USERNAME = "secret"; + public static final String WECHAT_OPEN_APP_ID = "open_app_id"; + public static final String WECHAT_OPEN_SECRET = "open_secret"; + public static final String WECHAT_OFFICIAL_ACCOUNT_APP_ID = "official_account_app_id"; public static final String WECHAT_OFFICIAL_ACCOUNT_SECRET = "official_account_secret"; @@ -56,6 +59,10 @@ public class WeChatUtils { wechatParameterConfMap.put(WECHAT_API_KEY, StringUtils.EMPTY); wechatParameterConfMap.put(WECHAT_API_USERNAME, StringUtils.EMPTY); + //Open API + wechatParameterConfMap.put(WECHAT_OPEN_APP_ID, StringUtils.EMPTY); + wechatParameterConfMap.put(WECHAT_OPEN_SECRET, StringUtils.EMPTY); + //Official Account wechatParameterConfMap.put(WECHAT_OFFICIAL_ACCOUNT_APP_ID, StringUtils.EMPTY); wechatParameterConfMap.put(WECHAT_OFFICIAL_ACCOUNT_SECRET, StringUtils.EMPTY); diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 9b18b93..60595cc 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -83,6 +83,11 @@ jwt: remember: 604800000 wechat: + open: + app-id: wx26e01c2be46730f3 + secret: 33a719a41175cac0e8832559dcf50903 + token: 0b01a700891d4a2f93a4323771c32455 + aes-key: 1IG2Xd7UykSga2jgrFFQrQCGFWilvO01hPyXRqS8mMI official-account: app-id: wx6362d43a5e29ec3c secret: 62f6d9a34acbfa1da1977de0057ad4f9