微信异步消息策略进行测试

This commit is contained in:
YuCheng Hu 2023-02-13 00:41:49 -05:00
parent f1794ee3ef
commit addf6d30fa
5 changed files with 96 additions and 32 deletions

View File

@ -7,6 +7,7 @@ package com.ossez.wechat.demo.common.enums;
* *
* @author YuCheng Hu * @author YuCheng Hu
*/ */
@Deprecated
public enum StorageCategory { public enum StorageCategory {
MEM, REDIS MEM, REDIS
} }

View File

@ -5,14 +5,16 @@ import com.ossez.wechat.common.exception.WxErrorException;
import com.ossez.wechat.demo.common.enums.HttpClientCategory; import com.ossez.wechat.demo.common.enums.HttpClientCategory;
import com.ossez.wechat.demo.properties.WeChatOfficialAccountProperties; import com.ossez.wechat.demo.properties.WeChatOfficialAccountProperties;
import com.ossez.wechat.oa.api.WeChatOfficialAccountService; import com.ossez.wechat.oa.api.WeChatOfficialAccountService;
import com.ossez.wechat.oa.api.impl.okhttp.WeChatMsgService;
import com.ossez.wechat.oa.api.impl.okhttp.WeChatPlatformService; import com.ossez.wechat.oa.api.impl.okhttp.WeChatPlatformService;
import com.ossez.wechat.oa.api.impl.okhttp.WeChatOfficialAccountServiceOkHttp; import com.ossez.wechat.oa.api.impl.okhttp.WeChatOfficialAccountServiceOkHttp;
import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Scope;
/** /**
* . * .
@ -40,17 +42,17 @@ public class WeChatConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public WeChatPlatformService weChatPlatformService(WeChatOfficialAccountService weChatOfficialAccountService) { @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
String accessToken = StringUtils.EMPTY; public WeChatPlatformService weChatPlatformService(WeChatOfficialAccountService weChatOfficialAccountService) throws WxErrorException {
try { weChatOfficialAccountService.getAccessToken();
accessToken = weChatOfficialAccountService.getAccessToken(); return new WeChatPlatformService(weChatOfficialAccountService);
} catch (WxErrorException e) {
e.printStackTrace();
}
WeChatPlatformService weChatPlatformService = new WeChatPlatformService(accessToken);
weChatPlatformService.setWeChatOfficialAccountService(weChatOfficialAccountService);
return weChatPlatformService;
} }
//
// @Bean
// @ConditionalOnMissingBean
// @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
// public WeChatMsgService weChatMsgService(WeChatOfficialAccountService weChatOfficialAccountService) throws WxErrorException {
// return new WeChatMsgService(weChatOfficialAccountService);
//
// }
} }

View File

@ -4,6 +4,7 @@ import com.ossez.wechat.common.exception.WxErrorException;
import com.ossez.wechat.common.model.res.NetworkCheckResponse; import com.ossez.wechat.common.model.res.NetworkCheckResponse;
import com.ossez.wechat.common.model.res.QueryQuotaResponse; import com.ossez.wechat.common.model.res.QueryQuotaResponse;
import com.ossez.wechat.demo.data.repository.redis.StudentRepository; import com.ossez.wechat.demo.data.repository.redis.StudentRepository;
import com.ossez.wechat.demo.service.WeChatService;
import com.ossez.wechat.oa.api.WeChatOfficialAccountService; import com.ossez.wechat.oa.api.WeChatOfficialAccountService;
import com.ossez.wechat.oa.api.impl.okhttp.WeChatPlatformService; import com.ossez.wechat.oa.api.impl.okhttp.WeChatPlatformService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -24,13 +25,13 @@ import org.springframework.web.bind.annotation.RestController;
public class WeChatController { public class WeChatController {
private final WeChatOfficialAccountService weChatOfficialAccountService; private final WeChatOfficialAccountService weChatOfficialAccountService;
private final WeChatPlatformService weChatPlatformService; private final WeChatService weChatService;
private final StudentRepository studentRepository; private final StudentRepository studentRepository;
@Autowired @Autowired
public WeChatController(WeChatOfficialAccountService weChatOfficialAccountService, WeChatPlatformService weChatPlatformService, StudentRepository studentRepository) { public WeChatController(WeChatOfficialAccountService weChatOfficialAccountService, WeChatService weChatService, StudentRepository studentRepository) {
this.weChatOfficialAccountService = weChatOfficialAccountService; this.weChatOfficialAccountService = weChatOfficialAccountService;
this.weChatPlatformService = weChatPlatformService; this.weChatService = weChatService;
this.studentRepository = studentRepository; this.studentRepository = studentRepository;
} }
@ -51,21 +52,28 @@ public class WeChatController {
@ResponseBody @ResponseBody
public String getDomainIPs() throws WxErrorException { public String getDomainIPs() throws WxErrorException {
log.debug("Get access token from WeChat"); log.debug("Get access token from WeChat");
return weChatPlatformService.getDomainIPs(); return weChatService.getDomainIPs();
}
@GetMapping("/networkcheck")
@ResponseBody
public NetworkCheckResponse checkNetwork() throws WxErrorException {
log.debug("Get access token from WeChat");
return weChatPlatformService.checkNetwork();
} }
// @GetMapping("/networkcheck")
// @ResponseBody
// public NetworkCheckResponse checkNetwork() throws WxErrorException {
// log.debug("Get access token from WeChat");
// return weChatPlatformService.checkNetwork();
// }
//
@GetMapping("/query/quota") @GetMapping("/query/quota")
@ResponseBody @ResponseBody
public QueryQuotaResponse queryQuota() throws WxErrorException { public QueryQuotaResponse queryQuota() throws WxErrorException {
log.debug("Get access token from WeChat"); log.debug("Get access token from WeChat");
return weChatPlatformService.queryQuota(); return weChatService.queryQuota();
}
@GetMapping("/message/send")
@ResponseBody
public String sendMessage() throws WxErrorException {
log.debug("Get access token from WeChat");
return weChatService.sendMessage();
} }
} }

View File

@ -1,17 +1,10 @@
package com.ossez.wechat.demo.properties; package com.ossez.wechat.demo.properties;
import com.ossez.wechat.demo.common.enums.HttpClientCategory;
import com.ossez.wechat.demo.common.enums.StorageCategory;
import com.ossez.wechat.demo.model.entity.WeChatDataStorage; import com.ossez.wechat.demo.model.entity.WeChatDataStorage;
import com.ossez.wechat.demo.model.entity.WeChatHost; import com.ossez.wechat.demo.model.entity.WeChatHost;
import lombok.Data; import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import java.io.Serializable;
import static com.ossez.wechat.demo.common.enums.StorageCategory.MEM;
/** /**
* WeChat Official Account Config * WeChat Official Account Config

View File

@ -0,0 +1,60 @@
package com.ossez.wechat.demo.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ossez.wechat.common.exception.WxErrorException;
import com.ossez.wechat.common.model.req.CustomMessage;
import com.ossez.wechat.common.model.res.QueryQuotaResponse;
import com.ossez.wechat.oa.api.WeChatOfficialAccountService;
import com.ossez.wechat.oa.api.impl.okhttp.WeChatMsgService;
import com.ossez.wechat.oa.api.impl.okhttp.WeChatOfficialAccountServiceOkHttp;
import com.ossez.wechat.oa.api.impl.okhttp.WeChatPlatformService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created with IntelliJ IDEA.
*
* @author XieYang, YuCheng
* @Date: 2022/10/14/16:17
* @Description:
*/
@Service
@Slf4j
public class WeChatService {
private final WeChatOfficialAccountService weChatOfficialAccountService;
private final ObjectMapper objectMapper;
@Autowired
public WeChatService(WeChatOfficialAccountService weChatOfficialAccountService, ObjectMapper objectMapper) throws WxErrorException {
this.weChatOfficialAccountService = weChatOfficialAccountService;
this.objectMapper = objectMapper;
}
/**
* Get WeChatLoginQRUrl
*
* @return
*/
public String getDomainIPs() throws WxErrorException {
return new WeChatPlatformService(weChatOfficialAccountService).getDomainIPs();
}
public QueryQuotaResponse queryQuota() throws WxErrorException {
return new WeChatPlatformService(weChatOfficialAccountService).queryQuota();
}
public String sendMessage() throws WxErrorException {
CustomMessage.KfText kfText = new CustomMessage.KfText("微信异步消息发送");
CustomMessage customMessage = new CustomMessage();
customMessage.setToUser("o9phd5jz_We8mPs1ovmyjud97Ock");
customMessage.setMsgType("text");
customMessage.setText(kfText);
return new WeChatMsgService(weChatOfficialAccountService).sendMessage(customMessage);
}
}