WxJava for Solon - 咱也不知道为啥要写
2022-04-02 12:08
756 查看
?
应 Solon 技术交流群里小伙伴的要求,我分享下在 Solon 中使用 WxJava 的经验、类库。
具体实现
- 提供统一的 Yaml 配置
package cn.edu.hnuahe.mountain.common.providers; import org.noear.solon.annotation.Component; import org.noear.solon.annotation.Init; import org.noear.solon.core.Aop; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; /** * @author 颖 */ @Component public class YamlProvider extends ServiceProvider { @Init @Override public void bootstrap() { DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN); Aop.wrapAndPut(Yaml.class, new Yaml(options)); } }
- 提供 WxService 统一管理器
package cn.edu.hnuahe.mountain.common.utils; import me.chanjar.weixin.cp.api.WxCpService; import org.noear.solon.annotation.Component; import java.util.concurrent.ConcurrentHashMap; /** * @author 颖 */ @Component public class WxCpClient extends ConcurrentHashMap<String, WxCpService> { public WxCpService get() { return this.get("default"); } }
- WxConfigDefault 注入提供器
package cn.edu.hnuahe.mountain.common.providers; import cn.edu.hnuahe.mountain.common.utils.WxCpClient; import me.chanjar.weixin.cp.api.WxCpService; import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl; import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl; import me.chanjar.weixin.cp.message.WxCpMessageRouter; import org.noear.solon.Solon; import org.noear.solon.annotation.Component; import org.noear.solon.annotation.Init; import org.noear.solon.annotation.Inject; import org.noear.solon.cloud.CloudClient; import org.noear.solon.cloud.model.Config; import org.noear.solon.core.Aop; import org.yaml.snakeyaml.Yaml; import java.util.LinkedHashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * @author 颖 */ @Component public class WxCpServiceProvider extends ServiceProvider { @Inject Yaml yaml; @Inject WxCpClient wxCpClient; public static class WxCpConfig extends ConcurrentHashMap<String, WxCpDefaultConfigImpl> {} @Init @Override public void bootstrap() { Config rawConfig = CloudClient.config().pull(Solon.cfg().appGroup(), "wxCp.yaml"); WxCpConfig configs = this.yaml.loadAs(rawConfig.value(), WxCpConfig.class); configs.forEach((name, config) -> { WxCpService service = new WxCpServiceImpl(); service.setWxCpConfigStorage(config); // 注册到企业微信客户端管理器 this.wxCpClient.put(name, service); }); WxCpMessageRouter router = new WxCpMessageRouter( this.wxCpClient.get() ); Aop.wrapAndPut(WxCpMessageRouter.class, router); } }
[scode type="blue"]步骤2与步骤3共同解决了一个大问题,就是一个企业微信小程序可能需要拥有多种身份的问题。比如,访问企业微信通讯录就需要基于 通讯录同步 小程序的身份进行操作。[/scode]
- 一个 Test,用于生成需要的 wxCp.yaml 配置
package cn.edu.hnuahe.mountain.common.providers; import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl; import me.chanjar.weixin.cp.config.impl.WxCpRedissonConfigImpl; import org.junit.jupiter.api.Test; import org.noear.solon.core.Aop; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; import static org.junit.jupiter.api.Assertions.*; class WxCpServiceProviderTest { @Test void generateConfig() { DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN); Yaml yaml = new Yaml(options); WxCpDefaultConfigImpl defaultConfig = new WxCpDefaultConfigImpl(); defaultConfig.setCorpId("123"); defaultConfig.setCorpSecret("456"); defaultConfig.setAgentId(789); WxCpDefaultConfigImpl contactsConfig = new WxCpDefaultConfigImpl(); contactsConfig.setCorpId("987"); contactsConfig.setCorpSecret("654"); WxCpServiceProvider.WxCpConfig config = new WxCpServiceProvider.WxCpConfig(); config.put("default", defaultConfig); config.put("contacts", contactsConfig); System.out.println(yaml.dump(config)); } }
- 一个简单的使用案例
package cn.edu.hnuahe.mountain.gateway.http.controller; import cn.edu.hnuahe.mountain.common.entities.models.User; import cn.edu.hnuahe.mountain.common.http.controller.BaseController; import cn.edu.hnuahe.mountain.common.mappers.UserMapper; import cn.edu.hnuahe.mountain.common.utils.WxCpClient; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.cp.bean.Gender; import me.chanjar.weixin.cp.bean.WxCpUser; import me.chanjar.weixin.cp.bean.article.NewArticle; import me.chanjar.weixin.cp.bean.message.WxCpMessage; import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult; import org.noear.solon.annotation.Controller; import org.noear.solon.annotation.Inject; import org.noear.solon.annotation.Mapping; import org.noear.solon.core.handle.MethodType; import java.util.List; /** * @author 颖 */ @Controller @Mapping(value = "/wechat") public class WeChatController { @Inject WxCpClient wxCpClient; @Mapping(value = "sendMessage", method = MethodType.ALL) public WxCpMessageSendResult sendMessage() throws WxErrorException { NewArticle part1 = new NewArticle(); part1.setUrl("https://www.baidu.com/"); part1.setPicUrl("https://pc-index-skin.cdn.bcebos.com/hiphoto/56868369122.jpg?x-bce-process=image/crop,x_0,y_0,w_1728,h_1080"); part1.setDescription("Is Really A Happy Day"); part1.setTitle("Happy Day"); WxCpMessage message = WxCpMessage.NEWS() .agentId(this.wxCpClient.get().getWxCpConfigStorage().getAgentId()) .toUser("2102160202008") .addArticle(part1) .build(); return this.wxCpClient.get() .getMessageService() .send(message); } }
散花~
相关文章推荐
- 最难点For的wx:key,您肯定不知道的!(框架细节十一)
- 【你不知道的Java】-【for 循环】
- 最难点For的wx:key,您肯定不知道的!(框架细节十一)
- 关于Qt for Android的一点思考---为啥是C,又为啥不可以是java?!
- Mybatis--java.lang.IllegalArgumentException: Result Maps collection already contains value for XXX
- SOA for the java developer, a focus
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_01 Collection集合_7_增强for循环
- Ganymed SSH-2 for Java系列3之执行远程shell 命令
- Cause: java.sql.SQLException: Data truncated for column 'comment' at row 1
- Embedded Jetty and Spring: The Lightweight Antidote for Java EE Complexity
- 使用Glide框架时报出,java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity
- 推荐一本书(Secure Coding Standard for Java)
- How do you calculate log base 2 in Java for integers?
- java.sql.SQLException: Access denied for user 'root'@'144.0.32.175' (using password: YES)
- java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)解决方法
- java增强for循环与普通for的差异
- GoldenGate for Java adapter介绍二(代码篇)
- Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.
- Could not set parameters for mapping: ParameterMapping{property='id', mode=IN, javaType=class java.l
- Java基础记忆05(for循环中的"*"规则代码)