Implement WXA code and WXA QR code getting.

This commit is contained in:
Sutra Zhou 2018-05-22 22:19:07 +08:00
parent 822307464e
commit 2acb0efbd2
12 changed files with 569 additions and 1 deletions

View File

@ -25,6 +25,11 @@
<artifactId>weixin4j-base</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.foxinmy</groupId>
<artifactId>weixin4j-mp</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>

View File

@ -1,7 +1,14 @@
package com.foxinmy.weixin4j.wxa;
import com.foxinmy.weixin4j.cache.CacheStorager;
import com.foxinmy.weixin4j.cache.FileCacheStorager;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.model.WeixinAccount;
import com.foxinmy.weixin4j.mp.token.WeixinTokenCreator;
import com.foxinmy.weixin4j.token.TokenCreator;
import com.foxinmy.weixin4j.token.TokenManager;
import com.foxinmy.weixin4j.wxa.api.LoginApi;
import com.foxinmy.weixin4j.wxa.api.QrCodeApi;
/**
* @since 1.8
@ -9,18 +16,60 @@ import com.foxinmy.weixin4j.wxa.api.LoginApi;
public class WeixinProxy {
private final LoginApi loginApi;
private final QrCodeApi qrCodeApi;
public WeixinProxy(WeixinAccount weixinAccount) {
public WeixinProxy(
WeixinAccount weixinAccount
) {
this(
weixinAccount,
new FileCacheStorager<Token>()
);
}
public WeixinProxy(
WeixinAccount weixinAccount,
CacheStorager<Token> cacheStorager
) {
this(
weixinAccount,
new WeixinTokenCreator(weixinAccount.getId(), weixinAccount.getSecret()),
cacheStorager
);
}
private WeixinProxy(
WeixinAccount weixinAccount,
TokenCreator tokenCreator,
CacheStorager<Token> cacheStorager
) {
if (weixinAccount == null) {
throw new IllegalArgumentException(
"weixinAccount must not be empty");
}
if (tokenCreator == null) {
throw new IllegalArgumentException(
"tokenCreator must not be empty");
}
if (cacheStorager == null) {
throw new IllegalArgumentException(
"cacheStorager must not be empty");
}
final TokenManager tokenManager = new TokenManager(tokenCreator, cacheStorager);
this.loginApi = new LoginApi(weixinAccount);
this.qrCodeApi = new QrCodeApi(tokenManager);
}
public LoginApi getLoginApi() {
return loginApi;
}
public QrCodeApi getQrCodeApi() {
return qrCodeApi;
}
}

View File

@ -0,0 +1,46 @@
package com.foxinmy.weixin4j.wxa.api;
import java.io.Serializable;
class Color implements Serializable {
private static final long serialVersionUID = 2018052201L;
private String r;
private String g;
private String b;
public Color() {
}
public Color(java.awt.Color color) {
this.r = String.valueOf(color.getRed());
this.g = String.valueOf(color.getGreen());
this.b = String.valueOf(color.getBlue());
}
public String getR() {
return r;
}
public void setR(String r) {
this.r = r;
}
public String getG() {
return g;
}
public void setG(String g) {
this.g = g;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
}

View File

@ -0,0 +1,163 @@
package com.foxinmy.weixin4j.wxa.api;
import java.awt.Color;
import java.io.IOException;
import com.alibaba.fastjson.JSON;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.ContentType;
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
import com.foxinmy.weixin4j.token.TokenManager;
/**
* 获取二维码.
*
* <p>
* 通过后台接口可以获取小程序任意页面的二维码扫描该二维码可以直接进入小程序对应的页面
* 目前微信支持两种二维码小程序码小程序二维码
* </p>
*
* @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api/qrcode.html">获取二维码</a>
* @since 1.8
*/
public class QrCodeApi extends WxaApi {
private final TokenManager tokenManager;
public QrCodeApi(TokenManager tokenManager) {
this.tokenManager = tokenManager;
}
/**
* 获取小程序码.
*
* <p>接口A: 适用于需要的码数量较少的业务场景</p>
* <p>
* 注意通过该接口生成的小程序码永久有效数量限制见微信小程序文档文末说明请谨慎使用
* 用户扫描该码进入小程序后将直接进入 path 对应的页面
* </p>
* @param path 不能为空最大长度 128 字节
* @param width 二维码的宽度默认值 430
* @param autoColor 自动配置线条颜色如果颜色依然是黑色则说明不建议配置主色调
* @param lineColor <code>authColor</code> false 时生效
* @param hyaline 是否需要透明底色 为true时生成透明底色的小程序码
* @return image bytes of WXA code.
* @throws WeixinException indicates get access token failed or get WXA code failed.
*
* @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api/qrcode.html#%E8%8E%B7%E5%8F%96%E5%B0%8F%E7%A8%8B%E5%BA%8F%E7%A0%81">获取小程序码</a>
*/
public byte[] getWxaCode(
String path,
Integer width,
Boolean autoColor,
Color lineColor,
Boolean hyaline
) throws WeixinException {
final String accessToken = tokenManager.getAccessToken();
final String getWxaCodeUri = this.getRequestUri("wxa_getwxacode", accessToken);
final WxaCodeParameter param = new WxaCodeParameter(path, width, autoColor, lineColor, hyaline);
return this.postAsImageBytes(getWxaCodeUri, param);
}
/**
* 获取小程序码.
*
* <p>接口B适用于需要的码数量极多的业务场景</p>
* <p>
* 注意通过该接口生成的小程序码永久有效数量暂无限制
* 用户扫描该码进入小程序后开发者需在对应页面获取的码中 scene 字段的值再做处理逻辑
* 使用如下代码可以获取到二维码中的 scene 字段的值
* 调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟
* 开发工具模拟时的 scene 的参数值需要进行 urlencode
* <code>
* // 这是首页的 js
* Page({
* onLoad: function(options) {
* // options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
* var scene = decodeURIComponent(options.scene)
* }
* })
* </code>
* </p>
* @param scene 最大32个可见字符只支持数字
* 大小写英文以及部分特殊字符!#$&'()*+,/:;=?@-._~
* 其它字符请自行编码为合法字符因不支持%中文无法使用 urlencode 处理请使用其他编码方式
* @param page 必须是已经发布的小程序存在的页面否则报错
* 例如 "pages/index/index"根路径前不要填加'/'
* 不能携带参数参数请放在scene字段里
* 如果不填写这个字段默认跳主页面
* @param width 二维码的宽度默认值 430
* @param autoColor 自动配置线条颜色如果颜色依然是黑色则说明不建议配置主色调默认值 false
* @param lineColor <code>autoColor</code> false 时生效使用 rgb 设置颜色
* @param hyaline 是否需要透明底色为true时生成透明底色的小程序码
* @return image bytes of WXA code.
* @throws WeixinException indicates get access token failed or get WXA code failed.
*
* @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api/qrcode.html#%E8%8E%B7%E5%8F%96%E5%B0%8F%E7%A8%8B%E5%BA%8F%E7%A0%81">获取小程序码</a>
*/
public byte[] getWxaCodeUnlimit(
String scene,
String page,
Integer width,
Boolean autoColor,
Color lineColor,
Boolean hyaline
) throws WeixinException {
final String accessToken = tokenManager.getAccessToken();
final String getWxaCodeUnlimitUri = this.getRequestUri("wxa_getwxacodeunlimit", accessToken);
final WxaCodeUnlimitParameter param = new WxaCodeUnlimitParameter(scene, page, width, autoColor, lineColor, hyaline);
return this.postAsImageBytes(getWxaCodeUnlimitUri, param);
}
/**
* 获取小程序二维码.
*
* <p>接口C适用于需要的码数量较少的业务场景</p>
* <p>
* 注意通过该接口生成的小程序二维码永久有效数量限制见微信小程序文档文末说明请谨慎使用
* 用户扫描该码进入小程序后将直接进入 path 对应的页面
* </p>
* @param path 不能为空最大长度 128 字节
* @param width 二维码的宽度默认值 430
* @return image bytes of WXA QR code.
* @throws WeixinException indicates get access token failed or get WXA QR code failed.
*
* @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api/qrcode.html#%E8%8E%B7%E5%8F%96%E5%B0%8F%E7%A8%8B%E5%BA%8F%E4%BA%8C%E7%BB%B4%E7%A0%81">获取小程序二维码</a>
*/
public byte[] createWxaQrCode(
String path,
Integer width
) throws WeixinException {
final String accessToken = tokenManager.getAccessToken();
final String createWxaQrCode = this.getRequestUri("wxaapp_createwxaqrcode", accessToken);
final WxaQrCodeParameter param = new WxaQrCodeParameter(path, width);
return this.postAsImageBytes(createWxaQrCode, param);
}
private byte[] postAsImageBytes(String uri, Object param) throws WeixinException {
final String body = JSON.toJSONString(param);
final WeixinResponse response = this.weixinExecutor.post(uri, body);
return toImageBytes(response);
}
private byte[] toImageBytes(WeixinResponse response) throws WeixinException {
try {
return readImageBytes(response);
} catch (IOException e) {
throw new WeixinException(e);
} catch (WxaCodeError e) {
throw new WeixinException(Integer.toString(e.getErrcode()), e.getErrmsg());
}
}
private byte[] readImageBytes(WeixinResponse response) throws IOException, WxaCodeError {
final String contentType = response.getHeaders().getContentType();
if (contentType != null && contentType.equals(ContentType.APPLICATION_JSON.getMimeType().getType())) {
WxaCodeError wxaCodeError = JSON.parseObject(response.getContent(), WxaCodeError.class);
throw wxaCodeError;
} else {
return response.getContent();
}
}
}

View File

@ -0,0 +1,39 @@
package com.foxinmy.weixin4j.wxa.api;
class WxaCodeError extends Exception {
private static final long serialVersionUID = 2018052201L;
private int errcode;
private String errmsg;
public WxaCodeError() {
}
public WxaCodeError(int errcode, String errmsg) {
this.errcode = errcode;
this.errmsg = errmsg;
}
public int getErrcode() {
return errcode;
}
public void setErrcode(int errcode) {
this.errcode = errcode;
}
public String getErrmsg() {
return errmsg;
}
public void setErrmsg(String errmsg) {
this.errmsg = errmsg;
}
@Override
public String getMessage() {
return errmsg;
}
}

View File

@ -0,0 +1,76 @@
package com.foxinmy.weixin4j.wxa.api;
import java.io.Serializable;
import com.alibaba.fastjson.annotation.JSONField;
class WxaCodeParameter implements Serializable {
private static final long serialVersionUID = 2018052201L;
private String path;
private Integer width;
private Boolean autoColor;
private Color color;
private Boolean hyaline;
public WxaCodeParameter(
String path,
Integer width,
Boolean autoColor,
java.awt.Color color,
Boolean hyaline
) {
this.path = path;
this.width = width;
this.autoColor = autoColor;
if (color != null) {
this.color = new Color(color);
}
this.hyaline = hyaline;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public Integer getWidth() {
return width;
}
public void setWidth(Integer width) {
this.width = width;
}
@JSONField(name = "auto_color")
public Boolean getAutoColor() {
return autoColor;
}
public void setAutoColor(Boolean autoColor) {
this.autoColor = autoColor;
}
@JSONField(name = "line_color")
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
@JSONField(name = "is_hyaline")
public Boolean getHyaline() {
return hyaline;
}
public void setHyaline(Boolean hyaline) {
this.hyaline = hyaline;
}
}

View File

@ -0,0 +1,87 @@
package com.foxinmy.weixin4j.wxa.api;
import java.io.Serializable;
import com.alibaba.fastjson.annotation.JSONField;
class WxaCodeUnlimitParameter implements Serializable {
private static final long serialVersionUID = 2018052201L;
private String scene;
private String page;
private Integer width;
private Boolean autoColor;
private Color color;
private Boolean hyaline;
public WxaCodeUnlimitParameter(
String scene,
String page,
Integer width,
Boolean autoColor,
java.awt.Color color,
Boolean hyaline
) {
this.scene = scene;
this.page = page;
this.width = width;
this.autoColor = autoColor;
if (color != null) {
this.color = new Color(color);
}
this.hyaline = hyaline;
}
public String getScene() {
return scene;
}
public void setScene(String scene) {
this.scene = scene;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public Integer getWidth() {
return width;
}
public void setWidth(Integer width) {
this.width = width;
}
@JSONField(name = "auto_color")
public Boolean getAutoColor() {
return autoColor;
}
public void setAutoColor(Boolean autoColor) {
this.autoColor = autoColor;
}
@JSONField(name = "line_color")
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
@JSONField(name = "is_hyaline")
public Boolean getHyaline() {
return hyaline;
}
public void setHyaline(Boolean hyaline) {
this.hyaline = hyaline;
}
}

View File

@ -0,0 +1,33 @@
package com.foxinmy.weixin4j.wxa.api;
import java.io.Serializable;
class WxaQrCodeParameter implements Serializable {
private static final long serialVersionUID = 2018052201L;
private String path;
private Integer width;
public WxaQrCodeParameter(String path, Integer width) {
this.path = path;
this.width = width;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public Integer getWidth() {
return width;
}
public void setWidth(Integer width) {
this.width = width;
}
}

View File

@ -6,3 +6,12 @@ api_base_url=https://api.weixin.qq.com
# \u767b\u5f55\u51ed\u8bc1\u6821\u9a8c
sns_jscode2session={api_base_url}/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=%s
# \u83b7\u53d6\u5c0f\u7a0b\u5e8f\u7801 \u63a5\u53e3A: \u9002\u7528\u4e8e\u9700\u8981\u7684\u7801\u6570\u91cf\u8f83\u5c11\u7684\u4e1a\u52a1\u573a\u666f
wxa_getwxacode={api_base_url}/wxa/getwxacode?access_token=%s
# \u83b7\u53d6\u5c0f\u7a0b\u5e8f\u7801 \u63a5\u53e3B\uff1a\u9002\u7528\u4e8e\u9700\u8981\u7684\u7801\u6570\u91cf\u6781\u591a\u7684\u4e1a\u52a1\u573a\u666f
wxa_getwxacodeunlimit={api_base_url}/wxa/getwxacodeunlimit?access_token=%s
# \u83b7\u53d6\u5c0f\u7a0b\u5e8f\u4e8c\u7ef4\u7801 \u63a5\u53e3C\uff1a\u9002\u7528\u4e8e\u9700\u8981\u7684\u7801\u6570\u91cf\u8f83\u5c11\u7684\u4e1a\u52a1\u573a\u666f
wxaapp_createwxaqrcode={api_base_url}/cgi-bin/wxaapp/createwxaqrcode?access_token=%s

View File

@ -0,0 +1,21 @@
package com.foxinmy.weixin4j.wxa.api;
import static org.junit.Assert.assertTrue;
import java.awt.Color;
import org.junit.Test;
import com.alibaba.fastjson.JSON;
public class WxaCodeParameterTest {
@Test
public void test() {
WxaCodeParameter param = new WxaCodeParameter("pages/index/index", 430, false, new Color(1, 2, 3), true);
String json = JSON.toJSONString(param);
assertTrue(json.contains("\"line_color\":"));
assertTrue(json.contains("\"is_hyaline\":true"));
}
}

View File

@ -0,0 +1,21 @@
package com.foxinmy.weixin4j.wxa.api;
import static org.junit.Assert.assertTrue;
import java.awt.Color;
import org.junit.Test;
import com.alibaba.fastjson.JSON;
public class WxaCodeUnlimitParameterTest {
@Test
public void test() {
WxaCodeUnlimitParameter param = new WxaCodeUnlimitParameter("myScene", "pages/index/index", 430, false, new Color(1, 2, 3), true);
String json = JSON.toJSONString(param);
assertTrue(json.contains("\"line_color\":"));
assertTrue(json.contains("\"is_hyaline\":true"));
}
}

View File

@ -0,0 +1,19 @@
package com.foxinmy.weixin4j.wxa.api;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import com.alibaba.fastjson.JSON;
public class WxaQrCodeParameterTest {
@Test
public void test() {
WxaQrCodeParameter param = new WxaQrCodeParameter("myPath", 100);
String json = JSON.toJSONString(param);
assertTrue(json.contains("\"path\":\"myPath\""));
assertTrue(json.contains("\"width\":100"));
}
}