Skip to content

Commit

Permalink
添加sample
Browse files Browse the repository at this point in the history
  • Loading branch information
NotFound403 committed May 30, 2023
1 parent 9433f5e commit 7b4929a
Show file tree
Hide file tree
Showing 600 changed files with 1,009 additions and 29 deletions.
43 changes: 16 additions & 27 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
<modelVersion>4.0.0</modelVersion>

<groupId>cn.felord</groupId>
<artifactId>wecom-sdk</artifactId>
<artifactId>wecom</artifactId>
<version>1.0.12</version>
<packaging>jar</packaging>
<packaging>pom</packaging>

<name>wecom-sdk</name>
<name>wecom</name>
<description>Work WeChat Java SDK</description>
<url>https://github.com/NotFound403/wecom-sdk</url>

Expand All @@ -44,6 +44,9 @@
<organization>felord.cn</organization>
</developer>
</developers>
<modules>
<module>wecom-sdk</module>
</modules>

<scm>
<tag>wecom-sdk</tag>
Expand Down Expand Up @@ -89,6 +92,8 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-boot.version>2.7.9</spring-boot.version>
<xstream.version>1.4.20</xstream.version>
</properties>

<distributionManagement>
Expand All @@ -109,34 +114,17 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.9</version>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>${xstream.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- <scope>provided</scope>-->
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.20</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
Expand Down Expand Up @@ -166,7 +154,8 @@
</configuration>
<executions>
<execution>
<phase>compile</phase>
<id>main-javadoc</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
Expand Down
33 changes: 33 additions & 0 deletions samples/spring-boot-sample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
54 changes: 54 additions & 0 deletions samples/spring-boot-sample/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.9</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.felord.wecom</groupId>
<artifactId>spring-boot-sample</artifactId>
<version>1.0.12</version>
<name>wecom-sdk-spring-boot-sample</name>
<description>wecom-sdk-spring-boot-sample</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>cn.felord</groupId>
<artifactId>wecom-sdk</artifactId>
<version>1.0.12</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cn.felord.wecom;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WecomSdkApplication {

public static void main(String[] args) {
SpringApplication.run(WecomSdkApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package cn.felord.wecom.api;

import cn.felord.callbacks.CallbackCrypto;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
* 通用回调,这里照搬即可,记得权限放行,过滤器拦截器也放行
* <p>
* 所有配置回调的地方都可以配置成一个接口,格式/wecom/callback/企微企业id/企微应用id
* <p>
* 企微企业id去企业微信控制台找
* 企微应用id,如果是自建的应用去应用管理查看agentid,如果是企微内置的应用查看{@link cn.felord.enumeration.NativeAgent}
*
* @author felord.cn
* @since 1.0.14.RELEASE
*/
@AllArgsConstructor
@RestController
@RequestMapping("/wecom/callback")
public class CallbackController {
private final CallbackCrypto callbackCrypto;

/**
* 验证回调URL
*
* @param msgSignature the msg signature
* @param timestamp the timestamp
* @param nonce the nonce
* @param echostr the echostr
* @param corpId the corp id
* @param agentId the agent id
* @return the long
*/
@GetMapping("/{corpId}/{agentId}")
public String verifyUrl(@RequestParam("msg_signature") String msgSignature,
@RequestParam String timestamp,
@RequestParam String nonce,
@RequestParam String echostr,
@PathVariable String corpId,
@PathVariable String agentId) {
return callbackCrypto.decryptMsg(agentId, corpId, msgSignature, timestamp, nonce, echostr);
}

/**
* 消费回调数据
*
* @param msgSignature the msg signature
* @param timestamp the timestamp
* @param nonce the nonce
* @param xmlBody the xml body
* @param corpId the corp id
* @param agentId the agent id
* @return the string
*/
@PostMapping("/{corpId}/{agentId}")
public String consume(@RequestParam("msg_signature") String msgSignature,
@RequestParam String timestamp,
@RequestParam String nonce,
@RequestBody String xmlBody,
@PathVariable String corpId,
@PathVariable String agentId) {
return callbackCrypto
.accept(agentId, corpId, msgSignature, timestamp, nonce, xmlBody);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cn.felord.wecom.cache;

import cn.felord.WeComTokenCacheable;
import org.jetbrains.annotations.NotNull;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;

/**
* @author dax
* @since 2023/5/30
*/
public class EhcacheWeComTokenCacheable implements WeComTokenCacheable {
private static final String QYWX_TOKEN_CACHE = "token::qywx";
private static final String QYWX_CORP_TICKET_CACHE = "ticket::qywx::corp";
private static final String QYWX_AGENT_TICKET_CACHE = "ticket::qywx::agent";

@CachePut(value = {QYWX_TOKEN_CACHE}, key = "#corpId.concat('::').concat(#agentId)")
@Override
public String putAccessToken(@NotNull String corpId, @NotNull String agentId, @NotNull String accessToken) {
return accessToken;
}

@Cacheable(value = {QYWX_TOKEN_CACHE}, key = "#corpId.concat('::').concat(#agentId)")
@Override
public String getAccessToken(@NotNull String corpId, @NotNull String agentId) {
return null;
}

@CachePut(value = {QYWX_CORP_TICKET_CACHE}, key = "#corpId.concat('::').concat(#agentId)")
@Override
public String putCorpTicket(@NotNull String corpId, @NotNull String agentId, @NotNull String corpTicket) {
return corpTicket;
}

@Cacheable(value = {QYWX_TOKEN_CACHE}, key = "#corpId.concat('::').concat(#agentId)")
@Override
public String getCorpTicket(@NotNull String corpId, @NotNull String agentId) {
return null;
}

@CachePut(value = {QYWX_AGENT_TICKET_CACHE}, key = "#corpId.concat('::').concat(#agentId)")
@Override
public String putAgentTicket(@NotNull String corpId, @NotNull String agentId, @NotNull String agentTicket) {
return agentTicket;
}

@Cacheable(value = {QYWX_TOKEN_CACHE}, key = "#corpId.concat('::').concat(#agentId)")
@Override
public String getAgentTicket(@NotNull String corpId, @NotNull String agentId) {
return null;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cn.felord.wecom.configure;

import cn.felord.WeComTokenCacheable;
import cn.felord.api.WorkWeChatApi;
import cn.felord.callbacks.CallbackAsyncConsumer;
import cn.felord.callbacks.CallbackCrypto;
import cn.felord.callbacks.CallbackCryptoBuilder;
import cn.felord.callbacks.CallbackSettingsService;
import cn.felord.domain.callback.CallbackEventBody;
import cn.felord.domain.callback.CallbackSettings;
import cn.felord.enumeration.CallbackEvent;
import cn.felord.wecom.cache.EhcacheWeComTokenCacheable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.function.Consumer;

/**
* Wecom Sdk 配置
*
* @author dax
* @since 2023 /5/30
*/
@Configuration(proxyBeanMethods = false)
public class WecomSdkConfiguration {

/**
* 企微token,ticket 缓存中继,这里使用ehcache作为例子,你可以无缝替换为redis等缓存中间件
*
* @return the we com token cacheable
*/
@Bean
WeComTokenCacheable weComTokenCacheable() {
return new EhcacheWeComTokenCacheable();
}

/**
* 初始化 企微API客户端,这个是平常我们发起调用的入口
* <p>
* 例子参见 test 测试包下的 SpringBootWecomSdkTests
*
* @param weComTokenCacheable the we com token cacheable
* @return the work we chat api
*/
@Bean
WorkWeChatApi workWeChatApi(WeComTokenCacheable weComTokenCacheable) {
return new WorkWeChatApi(weComTokenCacheable);
}

/**
* 回调配置
*
* @return the callback crypto
*/
@Bean
public CallbackCrypto callbackCrypto() {
return new CallbackCryptoBuilder(new Consumer<CallbackEventBody>() {
// 消费 被解密的回调消息 已达到动态处理企微事件业务的能力 这里还有其它高级操作 自行去看底层实现
@Override
public void accept(CallbackEventBody callbackEventBody) {
// 获取事件类型 建议每个类型的处理逻辑抽成接口 统一实现
CallbackEvent event = callbackEventBody.getEvent();

}
})
.build(new CallbackSettingsService() {
// 这里你可以通过实现该接口来持久化 根据 企微应用id 和企业id 查询对应应用的回调 token encodingAesKey 等回调参数,
@Override
public CallbackSettings loadAuthentication(String agentId, String corpId) {
return new CallbackSettings("", "", "");
}
});
}




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
spring:
application:
name: wecom-sdk-springboot
cache:
ehcache:
config:
Loading

0 comments on commit 7b4929a

Please sign in to comment.