添加使用不同的 Open,因为我们需要使用不同的 ID 来处理

This commit is contained in:
YuCheng Hu 2023-02-08 11:38:21 -05:00
parent 8c6840832a
commit 5a20159e6a
6 changed files with 58 additions and 41 deletions

View File

@ -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;
}

View File

@ -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());

View File

@ -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

View File

@ -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<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("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<Object> task = new Callable<Object>() {
public String call() {
return callChatGPT(content);
}
};
Future<Object> 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

View File

@ -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);

View File

@ -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