diff --git a/src/main/java/com/ossez/wechat/demo/common/enums/StorageCategory.java b/src/main/java/com/ossez/wechat/demo/common/enums/StorageCategory.java index 331dc4b..cf168e0 100644 --- a/src/main/java/com/ossez/wechat/demo/common/enums/StorageCategory.java +++ b/src/main/java/com/ossez/wechat/demo/common/enums/StorageCategory.java @@ -7,6 +7,7 @@ package com.ossez.wechat.demo.common.enums; * * @author YuCheng Hu */ +@Deprecated public enum StorageCategory { MEM, REDIS } diff --git a/src/main/java/com/ossez/wechat/demo/config/WeChatConfiguration.java b/src/main/java/com/ossez/wechat/demo/config/WeChatConfiguration.java index e6636aa..7d41568 100644 --- a/src/main/java/com/ossez/wechat/demo/config/WeChatConfiguration.java +++ b/src/main/java/com/ossez/wechat/demo/config/WeChatConfiguration.java @@ -5,14 +5,16 @@ import com.ossez.wechat.common.exception.WxErrorException; import com.ossez.wechat.demo.common.enums.HttpClientCategory; import com.ossez.wechat.demo.properties.WeChatOfficialAccountProperties; 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.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.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.Scope; /** * . @@ -40,17 +42,17 @@ public class WeChatConfiguration { @Bean @ConditionalOnMissingBean - public WeChatPlatformService weChatPlatformService(WeChatOfficialAccountService weChatOfficialAccountService) { - String accessToken = StringUtils.EMPTY; - try { - accessToken = weChatOfficialAccountService.getAccessToken(); - } catch (WxErrorException e) { - e.printStackTrace(); - } - - WeChatPlatformService weChatPlatformService = new WeChatPlatformService(accessToken); - - weChatPlatformService.setWeChatOfficialAccountService(weChatOfficialAccountService); - return weChatPlatformService; + @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public WeChatPlatformService weChatPlatformService(WeChatOfficialAccountService weChatOfficialAccountService) throws WxErrorException { + weChatOfficialAccountService.getAccessToken(); + return new WeChatPlatformService(weChatOfficialAccountService); } +// +// @Bean +// @ConditionalOnMissingBean +// @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +// public WeChatMsgService weChatMsgService(WeChatOfficialAccountService weChatOfficialAccountService) throws WxErrorException { +// return new WeChatMsgService(weChatOfficialAccountService); +// +// } } diff --git a/src/main/java/com/ossez/wechat/demo/controller/WeChatController.java b/src/main/java/com/ossez/wechat/demo/controller/WeChatController.java index a83735a..3593092 100644 --- a/src/main/java/com/ossez/wechat/demo/controller/WeChatController.java +++ b/src/main/java/com/ossez/wechat/demo/controller/WeChatController.java @@ -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.QueryQuotaResponse; 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.impl.okhttp.WeChatPlatformService; import lombok.extern.slf4j.Slf4j; @@ -24,13 +25,13 @@ import org.springframework.web.bind.annotation.RestController; public class WeChatController { private final WeChatOfficialAccountService weChatOfficialAccountService; - private final WeChatPlatformService weChatPlatformService; + private final WeChatService weChatService; private final StudentRepository studentRepository; @Autowired - public WeChatController(WeChatOfficialAccountService weChatOfficialAccountService, WeChatPlatformService weChatPlatformService, StudentRepository studentRepository) { + public WeChatController(WeChatOfficialAccountService weChatOfficialAccountService, WeChatService weChatService, StudentRepository studentRepository) { this.weChatOfficialAccountService = weChatOfficialAccountService; - this.weChatPlatformService = weChatPlatformService; + this.weChatService = weChatService; this.studentRepository = studentRepository; } @@ -51,21 +52,28 @@ public class WeChatController { @ResponseBody public String getDomainIPs() throws WxErrorException { log.debug("Get access token from WeChat"); - return weChatPlatformService.getDomainIPs(); - } - - @GetMapping("/networkcheck") - @ResponseBody - public NetworkCheckResponse checkNetwork() throws WxErrorException { - log.debug("Get access token from WeChat"); - return weChatPlatformService.checkNetwork(); + return weChatService.getDomainIPs(); } +// @GetMapping("/networkcheck") +// @ResponseBody +// public NetworkCheckResponse checkNetwork() throws WxErrorException { +// log.debug("Get access token from WeChat"); +// return weChatPlatformService.checkNetwork(); +// } +// @GetMapping("/query/quota") @ResponseBody public QueryQuotaResponse queryQuota() throws WxErrorException { 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(); } } diff --git a/src/main/java/com/ossez/wechat/demo/properties/WeChatOfficialAccountProperties.java b/src/main/java/com/ossez/wechat/demo/properties/WeChatOfficialAccountProperties.java index 7bfce97..0228747 100644 --- a/src/main/java/com/ossez/wechat/demo/properties/WeChatOfficialAccountProperties.java +++ b/src/main/java/com/ossez/wechat/demo/properties/WeChatOfficialAccountProperties.java @@ -1,17 +1,10 @@ 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.WeChatHost; import lombok.Data; 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 diff --git a/src/main/java/com/ossez/wechat/demo/service/WeChatService.java b/src/main/java/com/ossez/wechat/demo/service/WeChatService.java new file mode 100644 index 0000000..d0f0f2a --- /dev/null +++ b/src/main/java/com/ossez/wechat/demo/service/WeChatService.java @@ -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); + } + + +}