Jelajahi Sumber

:new: #2423 【企业微信】新增企业微信 Spring Boot Starter

Forever杨 3 tahun lalu
induk
melakukan
e3fc624052

+ 1 - 0
spring-boot-starters/pom.xml

@@ -21,6 +21,7 @@
     <module>wx-java-pay-spring-boot-starter</module>
     <module>wx-java-open-spring-boot-starter</module>
     <module>wx-java-qidian-spring-boot-starter</module>
+    <module>wx-java-cp-spring-boot-starter</module>
   </modules>
 
   <dependencies>

+ 45 - 0
spring-boot-starters/wx-java-cp-spring-boot-starter/pom.xml

@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <parent>
+    <artifactId>wx-java-spring-boot-starters</artifactId>
+    <groupId>com.github.binarywang</groupId>
+    <version>4.2.1.B</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+
+  <artifactId>wx-java-cp-spring-boot-starter</artifactId>
+  <name>WxJava - Spring Boot Starter for WxCp</name>
+  <description>微信企业号开发的 Spring Boot Starter</description>
+
+  <dependencies>
+    <dependency>
+      <groupId>com.github.binarywang</groupId>
+      <artifactId>weixin-java-cp</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-maven-plugin</artifactId>
+        <version>${spring.boot.version}</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-source-plugin</artifactId>
+        <version>2.2.1</version>
+        <executions>
+          <execution>
+            <id>attach-sources</id>
+            <goals>
+              <goal>jar-no-fork</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>

+ 21 - 0
spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/config/WxCpAutoConfiguration.java

@@ -0,0 +1,21 @@
+package com.binarywang.spring.starter.wxjava.cp.config;
+
+import com.binarywang.spring.starter.wxjava.cp.properties.WxCpProperties;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+
+/**
+ * 企业微信自动注册
+ *
+ * @author yl
+ * @date 2021/12/6
+ */
+@Configuration
+@EnableConfigurationProperties(WxCpProperties.class)
+@Import({
+  WxCpStorageAutoConfiguration.class,
+  WxCpServiceAutoConfiguration.class
+})
+public class WxCpAutoConfiguration {
+}

+ 44 - 0
spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/config/WxCpServiceAutoConfiguration.java

@@ -0,0 +1,44 @@
+package com.binarywang.spring.starter.wxjava.cp.config;
+
+import com.binarywang.spring.starter.wxjava.cp.properties.WxCpProperties;
+import lombok.RequiredArgsConstructor;
+import me.chanjar.weixin.cp.api.WxCpService;
+import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
+import me.chanjar.weixin.cp.config.WxCpConfigStorage;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * 企业微信平台相关服务自动注册
+ *
+ * @author yl
+ * @date 2021/12/6
+ */
+@Configuration
+@RequiredArgsConstructor
+public class WxCpServiceAutoConfiguration {
+  private final WxCpProperties wxCpProperties;
+
+  @Bean
+  @ConditionalOnMissingBean
+  @ConditionalOnBean(WxCpConfigStorage.class)
+  public WxCpService wxCpService(WxCpConfigStorage wxCpConfigStorage) {
+    WxCpService wxCpService = new WxCpServiceImpl();
+    wxCpService.setWxCpConfigStorage(wxCpConfigStorage);
+
+    WxCpProperties.ConfigStorage storage = wxCpProperties.getConfigStorage();
+    int maxRetryTimes = storage.getMaxRetryTimes();
+    if (maxRetryTimes < 0) {
+      maxRetryTimes = 0;
+    }
+    int retrySleepMillis = storage.getRetrySleepMillis();
+    if (retrySleepMillis < 0) {
+      retrySleepMillis = 1000;
+    }
+    wxCpService.setRetrySleepMillis(retrySleepMillis);
+    wxCpService.setMaxRetryTimes(maxRetryTimes);
+    return wxCpService;
+  }
+}

+ 18 - 0
spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/config/WxCpStorageAutoConfiguration.java

@@ -0,0 +1,18 @@
+package com.binarywang.spring.starter.wxjava.cp.config;
+
+import com.binarywang.spring.starter.wxjava.cp.storage.WxCpInMemoryConfigStorageConfiguration;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+
+/**
+ * 企业微信存储策略自动配置
+ *
+ * @author yl
+ * @date 2021/12/6
+ */
+@Configuration
+@Import({
+  WxCpInMemoryConfigStorageConfiguration.class
+})
+public class WxCpStorageAutoConfiguration {
+}

+ 101 - 0
spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/properties/WxCpProperties.java

@@ -0,0 +1,101 @@
+package com.binarywang.spring.starter.wxjava.cp.properties;
+
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+import java.io.Serializable;
+
+/**
+ * 企业微信接入相关配置属性
+ *
+ * @author yl
+ * @date 2021/12/6
+ */
+@Data
+@NoArgsConstructor
+@ConfigurationProperties(prefix = WxCpProperties.PREFIX)
+public class WxCpProperties {
+  public static final String PREFIX = "wx.cp";
+
+  /**
+   * 微信企业号 corpId
+   */
+  private String corpId;
+  /**
+   * 微信企业号 corpSecret
+   */
+  private String corpSecret;
+  /**
+   * 微信企业号应用 token
+   */
+  private String token;
+  /**
+   * 微信企业号应用 ID
+   */
+  private Integer agentId;
+  /**
+   * 微信企业号应用 EncodingAESKey
+   */
+  private String aesKey;
+
+  /**
+   * 配置存储策略,默认内存
+   */
+  private ConfigStorage configStorage = new ConfigStorage();
+
+  @Data
+  @NoArgsConstructor
+  public static class ConfigStorage implements Serializable {
+    private static final long serialVersionUID = 4815731027000065434L;
+    /**
+     * 存储类型
+     */
+    private StorageType type = StorageType.memory;
+
+    /**
+     * http代理主机
+     */
+    private String httpProxyHost;
+
+    /**
+     * http代理端口
+     */
+    private Integer httpProxyPort;
+
+    /**
+     * http代理用户名
+     */
+    private String httpProxyUsername;
+
+    /**
+     * http代理密码
+     */
+    private String httpProxyPassword;
+
+    /**
+     * http 请求最大重试次数
+     * <pre>
+     *   {@link me.chanjar.weixin.cp.api.WxCpService#setMaxRetryTimes(int)}
+     *   {@link me.chanjar.weixin.cp.api.impl.BaseWxCpServiceImpl#setMaxRetryTimes(int)}
+     * </pre>
+     */
+    private int maxRetryTimes = 5;
+
+    /**
+     * http 请求重试间隔
+     * <pre>
+     *   {@link me.chanjar.weixin.cp.api.WxCpService#setRetrySleepMillis(int)}
+     *   {@link me.chanjar.weixin.cp.api.impl.BaseWxCpServiceImpl#setRetrySleepMillis(int)}
+     * </pre>
+     */
+    private int retrySleepMillis = 1000;
+  }
+
+  public enum StorageType {
+    /**
+     * 内存
+     */
+    memory
+  }
+}

+ 53 - 0
spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/storage/AbstractWxCpConfigStorageConfiguration.java

@@ -0,0 +1,53 @@
+package com.binarywang.spring.starter.wxjava.cp.storage;
+
+import com.binarywang.spring.starter.wxjava.cp.properties.WxCpProperties;
+import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
+import org.apache.commons.lang3.StringUtils;
+
+/**
+ * WxCpConfigStorage 抽象配置类
+ *
+ * @author yl
+ * @date 2021/12/6
+ */
+public abstract class AbstractWxCpConfigStorageConfiguration {
+
+  protected WxCpDefaultConfigImpl config(WxCpDefaultConfigImpl config, WxCpProperties properties) {
+    String corpId = properties.getCorpId();
+    String corpSecret = properties.getCorpSecret();
+    String token = properties.getToken();
+    Integer agentId = properties.getAgentId();
+    String aesKey = properties.getAesKey();
+
+    config.setCorpId(corpId);
+    config.setCorpSecret(corpSecret);
+    if (StringUtils.isNotBlank(token)) {
+      config.setToken(token);
+    }
+    if (agentId != null) {
+      config.setAgentId(agentId);
+    }
+    if (StringUtils.isNotBlank(aesKey)) {
+      config.setAesKey(aesKey);
+    }
+
+    WxCpProperties.ConfigStorage storage = properties.getConfigStorage();
+    String httpProxyHost = storage.getHttpProxyHost();
+    Integer httpProxyPort = storage.getHttpProxyPort();
+    String httpProxyUsername = storage.getHttpProxyUsername();
+    String httpProxyPassword = storage.getHttpProxyPassword();
+    if (StringUtils.isNotBlank(httpProxyHost)) {
+      config.setHttpProxyHost(httpProxyHost);
+      if (httpProxyPort != null) {
+        config.setHttpProxyPort(httpProxyPort);
+      }
+      if (StringUtils.isNotBlank(httpProxyUsername)) {
+        config.setHttpProxyUsername(httpProxyUsername);
+      }
+      if (StringUtils.isNotBlank(httpProxyPassword)) {
+        config.setHttpProxyPassword(httpProxyPassword);
+      }
+    }
+    return config;
+  }
+}

+ 33 - 0
spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/storage/WxCpInMemoryConfigStorageConfiguration.java

@@ -0,0 +1,33 @@
+package com.binarywang.spring.starter.wxjava.cp.storage;
+
+import com.binarywang.spring.starter.wxjava.cp.properties.WxCpProperties;
+import lombok.RequiredArgsConstructor;
+import me.chanjar.weixin.cp.config.WxCpConfigStorage;
+import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * 自动装配基于内存策略配置
+ *
+ * @author yl
+ * @date 2021/12/6
+ */
+@Configuration
+@ConditionalOnProperty(
+  prefix = WxCpProperties.PREFIX + ".config-storage", name = "type",
+  matchIfMissing = true, havingValue = "memory"
+)
+@RequiredArgsConstructor
+public class WxCpInMemoryConfigStorageConfiguration extends AbstractWxCpConfigStorageConfiguration {
+  private final WxCpProperties wxCpProperties;
+
+  @Bean
+  @ConditionalOnMissingBean(WxCpConfigStorage.class)
+  public WxCpConfigStorage wxCpConfigStorage() {
+    WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
+    return this.config(config, wxCpProperties);
+  }
+}

+ 2 - 0
spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/resources/META-INF/spring.factories

@@ -0,0 +1,2 @@
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+com.binarywang.spring.starter.wxjava.cp.config.WxCpAutoConfiguration