AI智能
改变未来

WxJava for Solon – 咱也不知道为啥要写


?

应 Solon 技术交流群里小伙伴的要求,我分享下在 Solon 中使用 WxJava 的经验、类库。

具体实现

  1. 提供统一的 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 颖*/@Componentpublic class YamlProvider extends ServiceProvider {@Init@Overridepublic void bootstrap() {DumperOptions options = new DumperOptions();options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);Aop.wrapAndPut(Yaml.class, new Yaml(options));}}
  1. 提供 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 颖*/@Componentpublic class WxCpClient extends ConcurrentHashMap<String, WxCpService> {public WxCpService get() {return this.get("default");}}
  1. 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 颖*/@Componentpublic class WxCpServiceProvider extends ServiceProvider {@InjectYaml yaml;@InjectWxCpClient wxCpClient;public static class WxCpConfig extends ConcurrentHashMap<String, WxCpDefaultConfigImpl> {}@Init@Overridepublic 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]

  1. 一个 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 {@Testvoid 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));}}
  1. 一个简单的使用案例
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 {@InjectWxCpClient wxCpClient;@Mapping(value = "sendMessage", method = MethodType.ALL)public WxCpMessageSendResult sendMessage() throws WxErrorException {NewArticle part1 = new NewArticle();part1.setUrl("https://www.baidu.com/");part1.setPicUrl("Java开发/https://aiznh.com/wp-content/uploads/2022/08/20220802231007-62e9aecf5f27d.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);}}

散花~

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » WxJava for Solon – 咱也不知道为啥要写