Skip to content

Commit ffcbb2a

Browse files
committed
小程序二维码接口代码优化
1 parent 17a446e commit ffcbb2a

5 files changed

Lines changed: 60 additions & 60 deletions

File tree

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cn.binarywang.wx.miniapp.api;
22

3+
import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor;
34
import me.chanjar.weixin.common.exception.WxErrorException;
45

56
import java.io.File;
@@ -16,6 +17,9 @@
1617
* @author <a href="https://github.com/binarywang">Binary Wang</a>
1718
*/
1819
public interface WxMaQrcodeService {
20+
String CREATE_QRCODE_URL = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode";
21+
String GET_WXACODE_URL = "https://api.weixin.qq.com/wxa/getwxacode";
22+
String GET_WXACODE_UNLIMIT_URL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit";
1923

2024
/**
2125
* 接口C
@@ -42,10 +46,8 @@ public interface WxMaQrcodeService {
4246
* @param width 默认430 二维码的宽度
4347
* @param autoColor 默认true 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
4448
* @param lineColor auth_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"}
45-
* @return
46-
* @throws WxErrorException
4749
*/
48-
File createWxCode(String path, int width, boolean autoColor, LineColor lineColor) throws WxErrorException;
50+
File createWxCode(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor) throws WxErrorException;
4951

5052
File createWxCode(String path, int width) throws WxErrorException;
5153

@@ -65,49 +67,9 @@ public interface WxMaQrcodeService {
6567
* @param width 默认false 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
6668
* @param autoColor 默认true 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
6769
* @param lineColor auth_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"}
68-
* @return
69-
* @throws WxErrorException
7070
*/
71-
File createWxCodeLimit(String scene, String page, int width, boolean autoColor, LineColor lineColor) throws WxErrorException;
71+
File createWxCodeLimit(String scene, String page, int width, boolean autoColor, WxMaCodeLineColor lineColor) throws WxErrorException;
7272

7373
File createWxCodeLimit(String scene, String page) throws WxErrorException;
7474

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-
}
11375
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
44
import cn.binarywang.wx.miniapp.api.WxMaService;
5+
import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor;
56
import cn.binarywang.wx.miniapp.bean.WxMaQrcode;
67
import cn.binarywang.wx.miniapp.bean.WxMaWxcode;
78
import cn.binarywang.wx.miniapp.bean.WxMaWxcodeLimit;
@@ -22,9 +23,8 @@ public WxMaQrcodeServiceImpl(WxMaService wxMaService) {
2223

2324
@Override
2425
public File createQrcode(String path, int width) throws WxErrorException {
25-
String url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode";
2626
return this.wxMaService.execute(new QrCodeRequestExecutor(this.wxMaService.getRequestHttp()),
27-
url, new WxMaQrcode(path, width));
27+
CREATE_QRCODE_URL, new WxMaQrcode(path, width));
2828
}
2929

3030
@Override
@@ -33,15 +33,14 @@ public File createQrcode(String path) throws WxErrorException {
3333
}
3434

3535
@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";
36+
public File createWxCode(String path, int width, boolean autoColor, WxMaCodeLineColor lineColor) throws WxErrorException {
3837
WxMaWxcode wxMaWxcode = new WxMaWxcode();
3938
wxMaWxcode.setPath(path);
4039
wxMaWxcode.setWidth(width);
4140
wxMaWxcode.setAutoColor(autoColor);
4241
wxMaWxcode.setLineColor(lineColor);
4342
return this.wxMaService.execute(new QrCodeRequestExecutor(this.wxMaService.getRequestHttp()),
44-
url, wxMaWxcode);
43+
GET_WXACODE_URL, wxMaWxcode);
4544
}
4645

4746
@Override
@@ -55,16 +54,15 @@ public File createWxCode(String path) throws WxErrorException {
5554
}
5655

5756
@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";
57+
public File createWxCodeLimit(String scene, String page, int width, boolean autoColor, WxMaCodeLineColor lineColor) throws WxErrorException {
6058
WxMaWxcodeLimit wxMaWxcodeLimit = new WxMaWxcodeLimit();
6159
wxMaWxcodeLimit.setScene(scene);
6260
wxMaWxcodeLimit.setPage(page);
6361
wxMaWxcodeLimit.setWidth(width);
6462
wxMaWxcodeLimit.setAutoColor(autoColor);
6563
wxMaWxcodeLimit.setLineColor(lineColor);
6664
return this.wxMaService.execute(new QrCodeRequestExecutor(this.wxMaService.getRequestHttp()),
67-
url, wxMaWxcodeLimit);
65+
GET_WXACODE_UNLIMIT_URL, wxMaWxcodeLimit);
6866
}
6967

7068
@Override
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package cn.binarywang.wx.miniapp.bean;
2+
3+
/**
4+
* <pre>
5+
* lineColor 包装类
6+
* 用于描述二维码(小程序码)颜色(RGB参数值),
7+
* 详情请查看文档 https://mp.weixin.qq.com/debug/wxadoc/dev/api/qrcode.html
8+
* </pre>
9+
*/
10+
public class WxMaCodeLineColor {
11+
private String r = "0", g = "0", b = "0";
12+
13+
public WxMaCodeLineColor(String r, String g, String b) {
14+
this.r = r;
15+
this.g = g;
16+
this.b = b;
17+
}
18+
19+
public String getR() {
20+
return r;
21+
}
22+
23+
public void setR(String r) {
24+
this.r = r;
25+
}
26+
27+
public String getG() {
28+
return g;
29+
}
30+
31+
public void setG(String g) {
32+
this.g = g;
33+
}
34+
35+
public String getB() {
36+
return b;
37+
}
38+
39+
public void setB(String b) {
40+
this.b = b;
41+
}
42+
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package cn.binarywang.wx.miniapp.bean;
22

3-
import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
43
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
54
import com.google.gson.annotations.SerializedName;
65

@@ -19,7 +18,7 @@ public class WxMaWxcode extends WxMaQrcodeWrapper implements Serializable {
1918
private boolean autoColor = true;
2019

2120
@SerializedName("line_color")
22-
private WxMaQrcodeService.LineColor lineColor = new WxMaQrcodeService.LineColor("0", "0", "0");
21+
private WxMaCodeLineColor lineColor = new WxMaCodeLineColor("0", "0", "0");
2322

2423
public static WxMaWxcode fromJson(String json) {
2524
return WxMaGsonBuilder.create().fromJson(json, WxMaWxcode.class);
@@ -53,11 +52,11 @@ public void setAutoColor(boolean autoColor) {
5352
this.autoColor = autoColor;
5453
}
5554

56-
public WxMaQrcodeService.LineColor getLineColor() {
55+
public WxMaCodeLineColor getLineColor() {
5756
return lineColor;
5857
}
5958

60-
public void setLineColor(WxMaQrcodeService.LineColor lineColor) {
59+
public void setLineColor(WxMaCodeLineColor lineColor) {
6160
this.lineColor = lineColor;
6261
}
6362

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package cn.binarywang.wx.miniapp.bean;
22

3-
import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
43
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
54
import com.google.gson.annotations.SerializedName;
65

@@ -20,7 +19,7 @@ public class WxMaWxcodeLimit extends WxMaQrcodeWrapper implements Serializable {
2019
private boolean autoColor = true;
2120

2221
@SerializedName("line_color")
23-
private WxMaQrcodeService.LineColor lineColor = new WxMaQrcodeService.LineColor("0", "0", "0");
22+
private WxMaCodeLineColor lineColor = new WxMaCodeLineColor("0", "0", "0");
2423

2524
public static WxMaWxcodeLimit fromJson(String json) {
2625
return WxMaGsonBuilder.create().fromJson(json, WxMaWxcodeLimit.class);
@@ -58,11 +57,11 @@ public void setAutoColor(boolean autoColor) {
5857
this.autoColor = autoColor;
5958
}
6059

61-
public WxMaQrcodeService.LineColor getLineColor() {
60+
public WxMaCodeLineColor getLineColor() {
6261
return lineColor;
6362
}
6463

65-
public void setLineColor(WxMaQrcodeService.LineColor lineColor) {
64+
public void setLineColor(WxMaCodeLineColor lineColor) {
6665
this.lineColor = lineColor;
6766
}
6867
}

0 commit comments

Comments
 (0)