Skip to content

Commit ab5f573

Browse files
forfunsbinarywang
authored andcommitted
#292 增加小程序码支持
* 更新小程序二维码(小程序码)接口 修复小程序模板推送读取错误字段导致的NullPoint * 更新小程序二维码(小程序码)接口 修复小程序模板推送读取错误字段导致的NullPoint * 更正WxMaMsgService接口逻辑 * 使用IDEA对miniapp做了批量格式化
1 parent 6638ca7 commit ab5f573

File tree

16 files changed

+314
-14
lines changed

16 files changed

+314
-14
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/fs/FileUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static File createTmpFile(InputStream inputStream, String name, String ex
2424
tmpFile = File.createTempFile(name, '.' + ext, tmpDirFile);
2525
}
2626

27-
tmpFile.deleteOnExit();
27+
// tmpFile.deleteOnExit();
2828

2929
try (FileOutputStream fos = new FileOutputStream(tmpFile)) {
3030
int read = 0;

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaQrcodeService.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
/**
88
* <pre>
99
* 二维码相关操作接口
10+
*
11+
* 接口A(createWxCode)加上接口C(createQrcode),总共生成的码数量限制为100,000,请谨慎调用。
12+
*
1013
* 文档地址:https://mp.weixin.qq.com/debug/wxadoc/dev/api/qrcode.html
1114
* </pre>
1215
*
@@ -15,6 +18,7 @@
1518
public interface WxMaQrcodeService {
1619

1720
/**
21+
* 接口C
1822
* <pre>
1923
* 获取小程序页面二维码
2024
* 适用于需要的码数量较少的业务场景
@@ -27,4 +31,83 @@ public interface WxMaQrcodeService {
2731
* @param width 默认430 二维码的宽度
2832
*/
2933
File createQrcode(String path, int width) throws WxErrorException;
34+
35+
File createQrcode(String path) throws WxErrorException;
36+
37+
/**
38+
* 接口A
39+
* 获取小程序码
40+
*
41+
* @param path 不能为空,最大长度 128 字节
42+
* @param width 默认430 二维码的宽度
43+
* @param autoColor 默认true 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
44+
* @param lineColor auth_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"}
45+
* @return
46+
* @throws WxErrorException
47+
*/
48+
File createWxCode(String path, int width, boolean autoColor, LineColor lineColor) throws WxErrorException;
49+
50+
File createWxCode(String path, int width) throws WxErrorException;
51+
52+
File createWxCode(String path) throws WxErrorException;
53+
54+
/**
55+
* 接口B
56+
* 获取小程序码(永久有效、数量暂无限制)
57+
* <p>
58+
* 通过该接口生成的小程序码,永久有效,数量暂无限制。
59+
* 用户扫描该码进入小程序后,将统一打开首页,开发者需在对应页面根据获取的码中 scene 字段的值,再做处理逻辑。
60+
* 使用如下代码可以获取到二维码中的 scene 字段的值。
61+
* 调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟,开发工具模拟时的 scene 的参数值需要进行 urlencode
62+
*
63+
* @param scene 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式)
64+
* @param page 必须是已经发布的小程序页面,例如 "pages/index/index" ,如果不填写这个字段,默认跳主页面
65+
* @param width 默认false 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
66+
* @param autoColor 默认true 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
67+
* @param lineColor auth_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"}
68+
* @return
69+
* @throws WxErrorException
70+
*/
71+
File createWxCodeLimit(String scene, String page, int width, boolean autoColor, LineColor lineColor) throws WxErrorException;
72+
73+
File createWxCodeLimit(String scene, String page) throws WxErrorException;
74+
75+
/**
76+
* lineColor 包装类
77+
* 用于描述二维码(小程序码)颜色(RGB参数值),详情请查看文档
78+
*/
79+
public static class LineColor {
80+
81+
private String r = "0", g = "0", b = "0";
82+
83+
public LineColor(String r, String g, String b) {
84+
this.r = r;
85+
this.g = g;
86+
this.b = b;
87+
}
88+
89+
public String getR() {
90+
return r;
91+
}
92+
93+
public void setR(String r) {
94+
this.r = r;
95+
}
96+
97+
public String getG() {
98+
return g;
99+
}
100+
101+
public void setG(String g) {
102+
this.g = g;
103+
}
104+
105+
public String getB() {
106+
return b;
107+
}
108+
109+
public void setB(String b) {
110+
this.b = b;
111+
}
112+
}
30113
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaQrcodeServiceImpl.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
44
import cn.binarywang.wx.miniapp.api.WxMaService;
55
import cn.binarywang.wx.miniapp.bean.WxMaQrcode;
6+
import cn.binarywang.wx.miniapp.bean.WxMaWxcode;
7+
import cn.binarywang.wx.miniapp.bean.WxMaWxcodeLimit;
68
import cn.binarywang.wx.miniapp.util.http.QrCodeRequestExecutor;
79
import me.chanjar.weixin.common.exception.WxErrorException;
810

@@ -25,4 +27,49 @@ public File createQrcode(String path, int width) throws WxErrorException {
2527
url, new WxMaQrcode(path, width));
2628
}
2729

30+
@Override
31+
public File createQrcode(String path) throws WxErrorException {
32+
return this.createQrcode(path, 430);
33+
}
34+
35+
@Override
36+
public File createWxCode(String path, int width, boolean autoColor, LineColor lineColor) throws WxErrorException {
37+
String url = "https://api.weixin.qq.com/wxa/getwxacode";
38+
WxMaWxcode wxMaWxcode = new WxMaWxcode();
39+
wxMaWxcode.setPath(path);
40+
wxMaWxcode.setWidth(width);
41+
wxMaWxcode.setAutoColor(autoColor);
42+
wxMaWxcode.setLineColor(lineColor);
43+
return this.wxMaService.execute(new QrCodeRequestExecutor(this.wxMaService.getRequestHttp()),
44+
url, wxMaWxcode);
45+
}
46+
47+
@Override
48+
public File createWxCode(String path, int width) throws WxErrorException {
49+
return this.createWxCode(path, width, true, null);
50+
}
51+
52+
@Override
53+
public File createWxCode(String path) throws WxErrorException {
54+
return this.createWxCode(path, 430, true, null);
55+
}
56+
57+
@Override
58+
public File createWxCodeLimit(String scene, String page, int width, boolean autoColor, LineColor lineColor) throws WxErrorException {
59+
String url = "http://api.weixin.qq.com/wxa/getwxacodeunlimit";
60+
WxMaWxcodeLimit wxMaWxcodeLimit = new WxMaWxcodeLimit();
61+
wxMaWxcodeLimit.setScene(scene);
62+
wxMaWxcodeLimit.setPage(page);
63+
wxMaWxcodeLimit.setWidth(width);
64+
wxMaWxcodeLimit.setAutoColor(autoColor);
65+
wxMaWxcodeLimit.setLineColor(lineColor);
66+
return this.wxMaService.execute(new QrCodeRequestExecutor(this.wxMaService.getRequestHttp()),
67+
url, wxMaWxcodeLimit);
68+
}
69+
70+
@Override
71+
public File createWxCodeLimit(String scene, String page) throws WxErrorException {
72+
return this.createWxCodeLimit(scene, page, 430, true, null);
73+
}
74+
2875
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaJscode2SessionResult.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ public class WxMaJscode2SessionResult {
1818
@SerializedName("openid")
1919
private String openid;
2020

21+
@SerializedName("unionid")
22+
private String unionid;
23+
24+
public static WxMaJscode2SessionResult fromJson(String json) {
25+
return WxMaGsonBuilder.create().fromJson(json, WxMaJscode2SessionResult.class);
26+
}
27+
2128
public String getSessionKey() {
2229
return sessionKey;
2330
}
@@ -42,8 +49,12 @@ public void setOpenid(String openid) {
4249
this.openid = openid;
4350
}
4451

45-
public static WxMaJscode2SessionResult fromJson(String json) {
46-
return WxMaGsonBuilder.create().fromJson(json, WxMaJscode2SessionResult.class);
52+
public String getUnionid() {
53+
return unionid;
54+
}
55+
56+
public void setUnionid(String unionid) {
57+
this.unionid = unionid;
4758
}
4859

4960
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaQrcode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* @author <a href="https://github.com/binarywang">Binary Wang</a>
99
*/
10-
public class WxMaQrcode implements Serializable {
10+
public class WxMaQrcode extends WxMaQrcodeWrapper implements Serializable {
1111
private static final long serialVersionUID = 5777119669111011584L;
1212
private String path;
1313
private int width = 430;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.binarywang.wx.miniapp.bean;
2+
3+
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
4+
5+
/**
6+
* 微信二维码(小程序码)包装器
7+
* Created by Element on 2017/7/27.
8+
*/
9+
public abstract class WxMaQrcodeWrapper {
10+
11+
@Override
12+
public String toString() {
13+
return WxMaGsonBuilder.create().toJson(this);
14+
}
15+
16+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package cn.binarywang.wx.miniapp.bean;
2+
3+
import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
4+
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
5+
import com.google.gson.annotations.SerializedName;
6+
7+
import java.io.Serializable;
8+
9+
/**
10+
* Created by Element on 2017/7/27.
11+
*/
12+
public class WxMaWxcode extends WxMaQrcodeWrapper implements Serializable {
13+
14+
private static final long serialVersionUID = 1287399621649210322L;
15+
private String path;
16+
private int width = 430;
17+
18+
@SerializedName("auto_color")
19+
private boolean autoColor = true;
20+
21+
@SerializedName("line_color")
22+
private WxMaQrcodeService.LineColor lineColor = new WxMaQrcodeService.LineColor("0", "0", "0");
23+
24+
public static WxMaWxcode fromJson(String json) {
25+
return WxMaGsonBuilder.create().fromJson(json, WxMaWxcode.class);
26+
}
27+
28+
public static long getSerialVersionUID() {
29+
return serialVersionUID;
30+
}
31+
32+
public String getPath() {
33+
return path;
34+
}
35+
36+
public void setPath(String path) {
37+
this.path = path;
38+
}
39+
40+
public int getWidth() {
41+
return width;
42+
}
43+
44+
public void setWidth(int width) {
45+
this.width = width;
46+
}
47+
48+
public boolean isAutoColor() {
49+
return autoColor;
50+
}
51+
52+
public void setAutoColor(boolean autoColor) {
53+
this.autoColor = autoColor;
54+
}
55+
56+
public WxMaQrcodeService.LineColor getLineColor() {
57+
return lineColor;
58+
}
59+
60+
public void setLineColor(WxMaQrcodeService.LineColor lineColor) {
61+
this.lineColor = lineColor;
62+
}
63+
64+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package cn.binarywang.wx.miniapp.bean;
2+
3+
import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
4+
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
5+
import com.google.gson.annotations.SerializedName;
6+
7+
import java.io.Serializable;
8+
9+
/**
10+
* Created by Element on 2017/7/27.
11+
*/
12+
public class WxMaWxcodeLimit extends WxMaQrcodeWrapper implements Serializable {
13+
private static final long serialVersionUID = 4782193774524960401L;
14+
private String scene;
15+
private String page;
16+
17+
private int width = 430;
18+
19+
@SerializedName("auto_color")
20+
private boolean autoColor = true;
21+
22+
@SerializedName("line_color")
23+
private WxMaQrcodeService.LineColor lineColor = new WxMaQrcodeService.LineColor("0", "0", "0");
24+
25+
public static WxMaWxcodeLimit fromJson(String json) {
26+
return WxMaGsonBuilder.create().fromJson(json, WxMaWxcodeLimit.class);
27+
}
28+
29+
public String getPage() {
30+
return page;
31+
}
32+
33+
public void setPage(String page) {
34+
this.page = page;
35+
}
36+
37+
public String getScene() {
38+
return scene;
39+
}
40+
41+
public void setScene(String scene) {
42+
this.scene = scene;
43+
}
44+
45+
public int getWidth() {
46+
return width;
47+
}
48+
49+
public void setWidth(int width) {
50+
this.width = width;
51+
}
52+
53+
public boolean isAutoColor() {
54+
return autoColor;
55+
}
56+
57+
public void setAutoColor(boolean autoColor) {
58+
this.autoColor = autoColor;
59+
}
60+
61+
public WxMaQrcodeService.LineColor getLineColor() {
62+
return lineColor;
63+
}
64+
65+
public void setLineColor(WxMaQrcodeService.LineColor lineColor) {
66+
this.lineColor = lineColor;
67+
}
68+
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/builder/ImageBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public final class ImageBuilder extends BaseBuilder<ImageBuilder> {
1010
private String mediaId;
1111

1212
public ImageBuilder() {
13-
this.msgType = WxMaConstants.KefuMsgType.IMAGE;
13+
this.msgType = WxMaConstants.KefuMsgType.IMAGE;
1414
}
1515

1616
public ImageBuilder mediaId(String media_id) {

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/message/WxMaMessageInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public interface WxMaMessageInterceptor {
1717
/**
1818
* 拦截微信消息
1919
*
20-
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个
20+
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个
2121
* @return true代表OK,false代表不OK
2222
*/
2323
boolean intercept(WxMaMessage wxMessage,

0 commit comments

Comments
 (0)