Skip to content

Commit 1bd9b15

Browse files
committed
#193 增加小程序模块weixin-java-miniapp,支持小程序后台开发
1 parent 39e79a2 commit 1bd9b15

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+3641
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
<module>weixin-java-cp</module>
9494
<module>weixin-java-mp</module>
9595
<module>weixin-java-pay</module>
96+
<module>weixin-java-miniapp</module>
9697
<!--module>weixin-java-osgi</module-->
9798
</modules>
9899

weixin-java-miniapp/pom.xml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0"?>
2+
<project
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
5+
xmlns="http://maven.apache.org/POM/4.0.0">
6+
<modelVersion>4.0.0</modelVersion>
7+
<parent>
8+
<groupId>com.github.binarywang</groupId>
9+
<artifactId>weixin-java-parent</artifactId>
10+
<version>2.6.3.BETA</version>
11+
</parent>
12+
<artifactId>weixin-java-miniapp</artifactId>
13+
<name>WeiXin Java Tools - MiniApp</name>
14+
<description>微信小程序Java SDK</description>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>com.github.binarywang</groupId>
19+
<artifactId>weixin-java-common</artifactId>
20+
<version>${project.version}</version>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>org.jodd</groupId>
25+
<artifactId>jodd-http</artifactId>
26+
<scope>provided</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.squareup.okhttp3</groupId>
30+
<artifactId>okhttp</artifactId>
31+
<scope>provided</scope>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.testng</groupId>
36+
<artifactId>testng</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>ch.qos.logback</groupId>
41+
<artifactId>logback-classic</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>com.google.inject</groupId>
46+
<artifactId>guice</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.eclipse.jetty</groupId>
51+
<artifactId>jetty-server</artifactId>
52+
<scope>test</scope>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.eclipse.jetty</groupId>
56+
<artifactId>jetty-servlet</artifactId>
57+
<scope>test</scope>
58+
</dependency>
59+
<dependency>
60+
<groupId>joda-time</groupId>
61+
<artifactId>joda-time</artifactId>
62+
<scope>test</scope>
63+
</dependency>
64+
<dependency>
65+
<groupId>redis.clients</groupId>
66+
<artifactId>jedis</artifactId>
67+
</dependency>
68+
</dependencies>
69+
70+
<build>
71+
<plugins>
72+
<plugin>
73+
<groupId>org.apache.maven.plugins</groupId>
74+
<artifactId>maven-surefire-plugin</artifactId>
75+
<configuration>
76+
<suiteXmlFiles>
77+
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
78+
</suiteXmlFiles>
79+
</configuration>
80+
</plugin>
81+
</plugins>
82+
</build>
83+
84+
</project>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package cn.binarywang.wx.miniapp.api;
2+
3+
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
4+
import me.chanjar.weixin.common.exception.WxErrorException;
5+
6+
import java.io.File;
7+
import java.io.InputStream;
8+
9+
/**
10+
* <pre>
11+
* 临时素材接口
12+
* Created by Binary Wang on 2016/7/21.
13+
* </pre>
14+
*
15+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
16+
*/
17+
public interface WxMaMediaService {
18+
String MEDIA_UPLOAD_URL = "https://api.weixin.qq.com/cgi-bin/media/upload?type=%s";
19+
String MEDIA_GET_URL = "https://api.weixin.qq.com/cgi-bin/media/get";
20+
21+
/**
22+
* <pre>
23+
* 新增临时素材
24+
* 小程序可以使用本接口把媒体文件(目前仅支持图片)上传到微信服务器,用户发送客服消息或被动回复用户消息。
25+
* 详情请见: <a href="https://mp.weixin.qq.com/debug/wxadoc/dev/api/custommsg/material.html#新增临时素材">新增临时素材</a>
26+
* 接口url格式:https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE
27+
* </pre>
28+
*
29+
* @param mediaType 媒体类型,
30+
* @param file 文件对象
31+
* @see #uploadMedia(String, String, InputStream)
32+
*/
33+
WxMediaUploadResult uploadMedia(String mediaType, File file) throws WxErrorException;
34+
35+
/**
36+
* <pre>
37+
* 新增临时素材
38+
* 小程序可以使用本接口把媒体文件(目前仅支持图片)上传到微信服务器,用户发送客服消息或被动回复用户消息。
39+
*
40+
* 详情请见: <a href="https://mp.weixin.qq.com/debug/wxadoc/dev/api/custommsg/material.html#新增临时素材">新增临时素材</a>
41+
* 接口url格式:https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE
42+
* </pre>
43+
*
44+
* @param mediaType 媒体类型
45+
* @param fileType 文件类型
46+
* @param inputStream 输入流
47+
* @see #uploadMedia(java.lang.String, java.io.File)
48+
*/
49+
WxMediaUploadResult uploadMedia(String mediaType, String fileType, InputStream inputStream) throws WxErrorException;
50+
51+
/**
52+
* <pre>
53+
* 获取临时素材
54+
* 小程序可以使用本接口获取客服消息内的临时素材(即下载临时的多媒体文件)。目前小程序仅支持下载图片文件。
55+
*
56+
* 详情请见: <a href="https://mp.weixin.qq.com/debug/wxadoc/dev/api/custommsg/material.html#获取临时素材">获取临时素材</a>
57+
* 接口url格式:https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID
58+
* </pre>
59+
*
60+
* @param mediaId 媒体Id
61+
* @return 保存到本地的临时文件
62+
*/
63+
File getMedia(String mediaId) throws WxErrorException;
64+
65+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cn.binarywang.wx.miniapp.api;
2+
3+
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
4+
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
5+
import me.chanjar.weixin.common.exception.WxErrorException;
6+
7+
/**
8+
* <pre>
9+
* 消息发送接口
10+
* </pre>
11+
*
12+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
13+
*/
14+
public interface WxMaMsgService {
15+
String KEFU_MESSAGE_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/custom/send";
16+
String TEMPLATE_MSG_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send";
17+
18+
/**
19+
* <pre>
20+
* 发送客服消息
21+
* 详情请见: <a href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547&token=&lang=zh_CN">发送客服消息</a>
22+
* 接口url格式:https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN
23+
* </pre>
24+
*/
25+
boolean sendKefuMsg(WxMaKefuMessage message) throws WxErrorException;
26+
27+
/**
28+
* <pre>
29+
* 发送模板消息
30+
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN
31+
* </pre>
32+
*
33+
* @return 消息Id
34+
*/
35+
String sendTemplateMsg(WxMaTemplateMessage templateMessage) throws WxErrorException;
36+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cn.binarywang.wx.miniapp.api;
2+
3+
import me.chanjar.weixin.common.exception.WxErrorException;
4+
5+
import java.io.File;
6+
7+
/**
8+
* <pre>
9+
* 二维码相关操作接口
10+
* 文档地址:https://mp.weixin.qq.com/debug/wxadoc/dev/api/qrcode.html
11+
* </pre>
12+
*
13+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
14+
*/
15+
public interface WxMaQrcodeService {
16+
17+
/**
18+
* <pre>
19+
* 获取小程序页面二维码
20+
* 适用于需要的码数量较少的业务场景
21+
* 通过该接口,仅能生成已发布的小程序的二维码。
22+
* 可以在开发者工具预览时生成开发版的带参二维码。
23+
* 带参二维码只有 100000 个,请谨慎调用。
24+
* </pre>
25+
*
26+
* @param path 不能为空,最大长度 128 字节
27+
* @param width 默认430 二维码的宽度
28+
*/
29+
File createQrcode(String path, int width) throws WxErrorException;
30+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package cn.binarywang.wx.miniapp.api;
2+
3+
import cn.binarywang.wx.miniapp.config.WxMaConfig;
4+
import me.chanjar.weixin.common.exception.WxErrorException;
5+
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
6+
import me.chanjar.weixin.common.util.http.RequestExecutor;
7+
import me.chanjar.weixin.common.util.http.RequestHttp;
8+
9+
/**
10+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
11+
*/
12+
public interface WxMaService {
13+
/**
14+
* 获取access_token
15+
*/
16+
String GET_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
17+
18+
/**
19+
* <pre>
20+
* 验证消息的确来自微信服务器
21+
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319&token=&lang=zh_CN
22+
* </pre>
23+
*/
24+
boolean checkSignature(String timestamp, String nonce, String signature);
25+
26+
/**
27+
* 获取access_token, 不强制刷新access_token
28+
*
29+
* @see #getAccessToken(boolean)
30+
*/
31+
String getAccessToken() throws WxErrorException;
32+
33+
/**
34+
* <pre>
35+
* 获取access_token,本方法线程安全
36+
* 且在多线程同时刷新时只刷新一次,避免超出2000次/日的调用次数上限
37+
*
38+
* 另:本service的所有方法都会在access_token过期是调用此方法
39+
*
40+
* 程序员在非必要情况下尽量不要主动调用此方法
41+
*
42+
* 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183&token=&lang=zh_CN
43+
* </pre>
44+
*
45+
* @param forceRefresh 强制刷新
46+
*/
47+
String getAccessToken(boolean forceRefresh) throws WxErrorException;
48+
49+
/**
50+
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求
51+
*/
52+
String get(String url, String queryParam) throws WxErrorException;
53+
54+
/**
55+
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的POST请求
56+
*/
57+
String post(String url, String postData) throws WxErrorException;
58+
59+
/**
60+
* <pre>
61+
* Service没有实现某个API的时候,可以用这个,
62+
* 比{@link #get}和{@link #post}方法更灵活,可以自己构造RequestExecutor用来处理不同的参数和不同的返回类型。
63+
* 可以参考,{@link MediaUploadRequestExecutor}的实现方法
64+
* </pre>
65+
*/
66+
<T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException;
67+
68+
/**
69+
* <pre>
70+
* 设置当微信系统响应系统繁忙时,要等待多少 retrySleepMillis(ms) * 2^(重试次数 - 1) 再发起重试
71+
* 默认:1000ms
72+
* </pre>
73+
*/
74+
void setRetrySleepMillis(int retrySleepMillis);
75+
76+
/**
77+
* <pre>
78+
* 设置当微信系统响应系统繁忙时,最大重试次数
79+
* 默认:5次
80+
* </pre>
81+
*/
82+
void setMaxRetryTimes(int maxRetryTimes);
83+
84+
/**
85+
* 获取WxMaConfig 对象
86+
*
87+
* @return WxMaConfig
88+
*/
89+
WxMaConfig getWxMaConfig();
90+
91+
/**
92+
* 注入 {@link WxMaConfig} 的实现
93+
*/
94+
void setWxMaConfig(WxMaConfig wxConfigProvider);
95+
96+
/**
97+
* 返回消息(客服消息和模版消息)发送接口方法实现类,以方便调用其各个接口
98+
*
99+
* @return WxMaMsgService
100+
*/
101+
WxMaMsgService getMsgService();
102+
103+
/**
104+
* 返回素材相关接口方法的实现类对象,以方便调用其各个接口
105+
*
106+
* @return WxMaMediaService
107+
*/
108+
WxMaMediaService getMediaService();
109+
110+
/**
111+
* 返回用户相关接口方法的实现类对象,以方便调用其各个接口
112+
*
113+
* @return WxMaUserService
114+
*/
115+
WxMaUserService getUserService();
116+
117+
/**
118+
* 返回二维码相关接口方法的实现类对象,以方便调用其各个接口
119+
*
120+
* @return WxMaQrcodeService
121+
*/
122+
WxMaQrcodeService getQrcodeService();
123+
124+
/**
125+
* 初始化http请求对象
126+
*/
127+
void initHttp();
128+
129+
/**
130+
* 请求http请求相关信息
131+
*/
132+
RequestHttp getRequestHttp();
133+
134+
135+
}

0 commit comments

Comments
 (0)