Ver código fonte

fix 增加 CODE

Young 2 anos atrás
pai
commit
deebb665e2

+ 5 - 0
wx-java-tools/wx-java-cp-start/src/main/java/cn/nosum/wx/cp/config/WxCpAppProperties.java

@@ -16,6 +16,11 @@ public class WxCpAppProperties {
     private Integer agentId;
 
     /**
+     * 设置企业微信应用的AgentCode
+     */
+    private String agentCode;
+
+    /**
      * 设置企业微信应用的Secret
      */
     private String secret;

+ 26 - 6
wx-java-tools/wx-java-cp-start/src/main/java/cn/nosum/wx/cp/config/WxCpConfiguration.java

@@ -13,7 +13,9 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
 import org.springframework.context.annotation.Configuration;
 
 import javax.annotation.PostConstruct;
+import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.stream.Collectors;
 
@@ -29,14 +31,18 @@ public class WxCpConfiguration {
     @Autowired
     private WxCpMultiAppProperties properties;
 
-    private static Map<Integer, WxCpService> cpServices = new ConcurrentHashMap<>();
+    private static Map<Integer, WxCpService> cpAgentIdServiceMap = new ConcurrentHashMap<>();
+
+    private static Map<String, WxCpService> cpAgentCodeServiceMap = new ConcurrentHashMap<>();
 
     @PostConstruct
     public void initServices() {
-        cpServices = this.properties.getAppConfigs().stream().map(agent -> {
+        // 获取所有服务的配置
+        List<WxCpService> wxCpServiceList = this.properties.getAppConfigs().stream().map(agent -> {
             val configStorage = new WxCpDefaultConfigImpl();
             configStorage.setCorpId(this.properties.getCorpId());
             configStorage.setAgentId(agent.getAgentId());
+            configStorage.setAgentCode(agent.getAgentCode());
             configStorage.setCorpSecret(agent.getSecret());
             configStorage.setToken(agent.getToken());
             configStorage.setAesKey(agent.getAesKey());
@@ -70,17 +76,31 @@ public class WxCpConfiguration {
 
             service.setWxCpConfigStorage(configStorage);
             return service;
-        }).collect(Collectors.toMap(service -> service.getWxCpConfigStorage().getAgentId(), a -> a));
+        }).collect(Collectors.toList());
+
+        // 保存所有的服务
+        cpAgentIdServiceMap = wxCpServiceList.stream().filter(service -> Objects.nonNull(service.getWxCpConfigStorage().getAgentId())).collect(Collectors.toMap(service -> service.getWxCpConfigStorage().getAgentId(), service -> service));
+        cpAgentCodeServiceMap = wxCpServiceList.stream().filter(service -> Objects.nonNull(service.getWxCpConfigStorage().getAgentCode())).collect(Collectors.toMap(service -> service.getWxCpConfigStorage().getAgentCode(), service -> service));
     }
 
 
     /**
-     * 根据应用ID获取对应的服务.
+     * 根据应用 ID 获取对应的服务.
      *
-     * @param agentId 应用ID
+     * @param agentId 应用 ID
      * @return 服务
      */
     public static WxCpService getCpService(Integer agentId) {
-        return cpServices.get(agentId);
+        return cpAgentIdServiceMap.get(agentId);
+    }
+
+    /**
+     * 根据应用 CODE 获取对应的服务.
+     *
+     * @param agentCode 应用 CODE
+     * @return 服务
+     */
+    public static WxCpService getCpService(String agentCode) {
+        return cpAgentCodeServiceMap.get(agentCode);
     }
 }

+ 224 - 224
wx-java-tools/wx-java-cp/src/main/java/cn/nosum/wx/cp/config/WxCpConfigStorage.java

@@ -12,228 +12,228 @@ import java.util.concurrent.locks.Lock;
  */
 public interface WxCpConfigStorage {
 
-  /**
-   * 设置企业微信服务器 baseUrl.
-   * 默认值是 https://qyapi.weixin.qq.com , 如果使用默认值,则不需要调用 setBaseApiUrl
-   *
-   * @param baseUrl 企业微信服务器 Url
-   */
-  void setBaseApiUrl(String baseUrl);
-
-  /**
-   * 读取企业微信 API Url.
-   * 支持私有化企业微信服务器.
-   *
-   * @param path the path
-   * @return the api url
-   */
-  String getApiUrl(String path);
-
-  /**
-   * Gets access token.
-   *
-   * @return the access token
-   */
-  String getAccessToken();
-
-  /**
-   * Gets access token lock.
-   *
-   * @return the access token lock
-   */
-  Lock getAccessTokenLock();
-
-  /**
-   * Is access token expired boolean.
-   *
-   * @return the boolean
-   */
-  boolean isAccessTokenExpired();
-
-  /**
-   * 强制将access token过期掉.
-   */
-  void expireAccessToken();
-
-  /**
-   * Update access token.
-   *
-   * @param accessToken the access token
-   */
-  void updateAccessToken(WxAccessToken accessToken);
-
-  /**
-   * Update access token.
-   *
-   * @param accessToken the access token
-   * @param expiresIn   the expires in
-   */
-  void updateAccessToken(String accessToken, int expiresIn);
-
-  /**
-   * Gets jsapi ticket.
-   *
-   * @return the jsapi ticket
-   */
-  String getJsapiTicket();
-
-  /**
-   * Gets jsapi ticket lock.
-   *
-   * @return the jsapi ticket lock
-   */
-  Lock getJsapiTicketLock();
-
-  /**
-   * Is jsapi ticket expired boolean.
-   *
-   * @return the boolean
-   */
-  boolean isJsapiTicketExpired();
-
-  /**
-   * 强制将jsapi ticket过期掉.
-   */
-  void expireJsapiTicket();
-
-  /**
-   * 应该是线程安全的.
-   *
-   * @param jsapiTicket      the jsapi ticket
-   * @param expiresInSeconds the expires in seconds
-   */
-  void updateJsapiTicket(String jsapiTicket, int expiresInSeconds);
-
-  /**
-   * Gets agent jsapi ticket.
-   *
-   * @return the agent jsapi ticket
-   */
-  String getAgentJsapiTicket();
-
-  /**
-   * Gets agent jsapi ticket lock.
-   *
-   * @return the agent jsapi ticket lock
-   */
-  Lock getAgentJsapiTicketLock();
-
-  /**
-   * Is agent jsapi ticket expired boolean.
-   *
-   * @return the boolean
-   */
-  boolean isAgentJsapiTicketExpired();
-
-  /**
-   * 强制将jsapi ticket过期掉.
-   */
-  void expireAgentJsapiTicket();
-
-  /**
-   * 应该是线程安全的.
-   *
-   * @param jsapiTicket      the jsapi ticket
-   * @param expiresInSeconds the expires in seconds
-   */
-  void updateAgentJsapiTicket(String jsapiTicket, int expiresInSeconds);
-
-  /**
-   * Gets corp id.
-   *
-   * @return the corp id
-   */
-  String getCorpId();
-
-  /**
-   * Gets corp secret.
-   *
-   * @return the corp secret
-   */
-  String getCorpSecret();
-
-  /**
-   * Gets agent id.
-   *
-   * @return the agent id
-   */
-  Integer getAgentId();
-
-  /**
-   * Gets token.
-   *
-   * @return the token
-   */
-  String getToken();
-
-  /**
-   * Gets aes key.
-   *
-   * @return the aes key
-   */
-  String getAesKey();
-
-  /**
-   * Gets expires time.
-   *
-   * @return the expires time
-   */
-  long getExpiresTime();
-
-  /**
-   * Gets oauth 2 redirect uri.
-   *
-   * @return the oauth 2 redirect uri
-   */
-  String getOauth2redirectUri();
-
-  /**
-   * Gets http proxy host.
-   *
-   * @return the http proxy host
-   */
-  String getHttpProxyHost();
-
-  /**
-   * Gets http proxy port.
-   *
-   * @return the http proxy port
-   */
-  int getHttpProxyPort();
-
-  /**
-   * Gets http proxy username.
-   *
-   * @return the http proxy username
-   */
-  String getHttpProxyUsername();
-
-  /**
-   * Gets http proxy password.
-   *
-   * @return the http proxy password
-   */
-  String getHttpProxyPassword();
-
-  /**
-   * Gets tmp dir file.
-   *
-   * @return the tmp dir file
-   */
-  File getTmpDirFile();
-
-
-  /**
-   * 是否自动刷新token
-   *
-   * @return . boolean
-   */
-  boolean autoRefreshToken();
-
-  /**
-   * 获取群机器人webhook的key
-   *
-   * @return key webhook key
-   */
-  String getWebhookKey();
+    /**
+     * 设置企业微信服务器 baseUrl.
+     * 默认值是 https://qyapi.weixin.qq.com , 如果使用默认值,则不需要调用 setBaseApiUrl
+     *
+     * @param baseUrl 企业微信服务器 Url
+     */
+    void setBaseApiUrl(String baseUrl);
+
+    /**
+     * 读取企业微信 API Url.
+     * 支持私有化企业微信服务器.
+     *
+     * @param path the path
+     * @return the api url
+     */
+    String getApiUrl(String path);
+
+    /**
+     * Gets access token.
+     *
+     * @return the access token
+     */
+    String getAccessToken();
+
+    /**
+     * Gets access token lock.
+     *
+     * @return the access token lock
+     */
+    Lock getAccessTokenLock();
+
+    /**
+     * Is access token expired boolean.
+     *
+     * @return the boolean
+     */
+    boolean isAccessTokenExpired();
+
+    /**
+     * 强制将access token过期掉.
+     */
+    void expireAccessToken();
+
+    /**
+     * Update access token.
+     *
+     * @param accessToken the access token
+     */
+    void updateAccessToken(WxAccessToken accessToken);
+
+    /**
+     * Update access token.
+     *
+     * @param accessToken the access token
+     * @param expiresIn   the expires in
+     */
+    void updateAccessToken(String accessToken, int expiresIn);
+
+    /**
+     * Gets jsapi ticket.
+     *
+     * @return the jsapi ticket
+     */
+    String getJsapiTicket();
+
+    /**
+     * Gets jsapi ticket lock.
+     *
+     * @return the jsapi ticket lock
+     */
+    Lock getJsapiTicketLock();
+
+    /**
+     * Is jsapi ticket expired boolean.
+     *
+     * @return the boolean
+     */
+    boolean isJsapiTicketExpired();
+
+    /**
+     * 强制将jsapi ticket过期掉.
+     */
+    void expireJsapiTicket();
+
+    /**
+     * 应该是线程安全的.
+     *
+     * @param jsapiTicket      the jsapi ticket
+     * @param expiresInSeconds the expires in seconds
+     */
+    void updateJsapiTicket(String jsapiTicket, int expiresInSeconds);
+
+    /**
+     * Gets agent jsapi ticket.
+     *
+     * @return the agent jsapi ticket
+     */
+    String getAgentJsapiTicket();
+
+    /**
+     * Gets agent jsapi ticket lock.
+     *
+     * @return the agent jsapi ticket lock
+     */
+    Lock getAgentJsapiTicketLock();
+
+    /**
+     * Is agent jsapi ticket expired boolean.
+     *
+     * @return the boolean
+     */
+    boolean isAgentJsapiTicketExpired();
+
+    /**
+     * 强制将jsapi ticket过期掉.
+     */
+    void expireAgentJsapiTicket();
+
+    /**
+     * 应该是线程安全的.
+     *
+     * @param jsapiTicket      the jsapi ticket
+     * @param expiresInSeconds the expires in seconds
+     */
+    void updateAgentJsapiTicket(String jsapiTicket, int expiresInSeconds);
+
+    /**
+     * Gets corp id.
+     *
+     * @return the corp id
+     */
+    String getCorpId();
+
+    /**
+     * Gets corp secret.
+     *
+     * @return the corp secret
+     */
+    String getCorpSecret();
+
+    /**
+     * Gets agent id.
+     *
+     * @return the agent id
+     */
+    Integer getAgentId();
+
+    /**
+     * Gets agent Code.
+     *
+     * @return the agent code
+     */
+    String getAgentCode();
+
+    /**
+     * Gets token.
+     *
+     * @return the token
+     */
+    String getToken();
+
+    /**
+     * Gets aes key.
+     *
+     * @return the aes key
+     */
+    String getAesKey();
+
+    /**
+     * Gets expires time.
+     *
+     * @return the expires time
+     */
+    long getExpiresTime();
+
+    /**
+     * Gets oauth 2 redirect uri.
+     *
+     * @return the oauth 2 redirect uri
+     */
+    String getOauth2redirectUri();
+
+    /**
+     * Gets http proxy host.
+     *
+     * @return the http proxy host
+     */
+    String getHttpProxyHost();
+
+    /**
+     * Gets http proxy port.
+     *
+     * @return the http proxy port
+     */
+    int getHttpProxyPort();
+
+    /**
+     * Gets http proxy username.
+     *
+     * @return the http proxy username
+     */
+    String getHttpProxyUsername();
+
+    /**
+     * Gets http proxy password.
+     *
+     * @return the http proxy password
+     */
+    String getHttpProxyPassword();
+
+
+    /**
+     * 是否自动刷新token
+     *
+     * @return . boolean
+     */
+    boolean autoRefreshToken();
+
+    /**
+     * 获取群机器人webhook的key
+     *
+     * @return key webhook key
+     */
+    String getWebhookKey();
 }

+ 25 - 17
wx-java-tools/wx-java-cp/src/main/java/cn/nosum/wx/cp/config/impl/WxCpDefaultConfigImpl.java

@@ -17,26 +17,37 @@ import java.util.concurrent.locks.ReentrantLock;
  */
 public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
     private static final long serialVersionUID = 1154541446729462780L;
+
     /**
      * The Access token.
      */
     protected volatile String accessToken;
+
     /**
      * The Access token lock.
      */
     protected transient Lock accessTokenLock = new ReentrantLock();
+
     /**
      * The Agent id.
      */
     protected volatile Integer agentId;
+
+    /**
+     * The Agent code.
+     */
+    protected volatile String agentCode;
+
     /**
      * The Jsapi ticket lock.
      */
     protected transient Lock jsapiTicketLock = new ReentrantLock();
+
     /**
      * The Agent jsapi ticket lock.
      */
     protected transient Lock agentJsapiTicketLock = new ReentrantLock();
+
     private volatile String corpId;
     private volatile String corpSecret;
     private volatile String token;
@@ -52,9 +63,6 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
     private volatile String agentJsapiTicket;
     private volatile long agentJsapiTicketExpiresTime;
 
-    private volatile File tmpDirFile;
-
-
     private volatile String baseApiUrl;
 
     private volatile String webhookKey;
@@ -278,6 +286,20 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
     }
 
     @Override
+    public String getAgentCode() {
+        return this.agentCode;
+    }
+
+    /**
+     * Sets agent code.
+     *
+     * @param agentCode the agent code
+     */
+    public void setAgentCode(String agentCode) {
+        this.agentCode = agentCode;
+    }
+
+    @Override
     public String getOauth2redirectUri() {
         return this.oauth2redirectUri;
     }
@@ -353,20 +375,6 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
     }
 
     @Override
-    public File getTmpDirFile() {
-        return this.tmpDirFile;
-    }
-
-    /**
-     * Sets tmp dir file.
-     *
-     * @param tmpDirFile the tmp dir file
-     */
-    public void setTmpDirFile(File tmpDirFile) {
-        this.tmpDirFile = tmpDirFile;
-    }
-
-    @Override
     public boolean autoRefreshToken() {
         return true;
     }