* 更新小程序二维码(小程序码)接口 修复小程序模板推送读取错误字段导致的NullPoint * 更新小程序二维码(小程序码)接口 修复小程序模板推送读取错误字段导致的NullPoint * 更正WxMaMsgService接口逻辑 * 使用IDEA对miniapp做了批量格式化master
| @@ -24,7 +24,7 @@ public class FileUtils { | |||
| tmpFile = File.createTempFile(name, '.' + ext, tmpDirFile); | |||
| } | |||
| tmpFile.deleteOnExit(); | |||
| // tmpFile.deleteOnExit(); | |||
| try (FileOutputStream fos = new FileOutputStream(tmpFile)) { | |||
| int read = 0; | |||
| @@ -7,6 +7,9 @@ import java.io.File; | |||
| /** | |||
| * <pre> | |||
| * 二维码相关操作接口 | |||
| * | |||
| * 接口A(createWxCode)加上接口C(createQrcode),总共生成的码数量限制为100,000,请谨慎调用。 | |||
| * | |||
| * 文档地址:https://mp.weixin.qq.com/debug/wxadoc/dev/api/qrcode.html | |||
| * </pre> | |||
| * | |||
| @@ -15,6 +18,7 @@ import java.io.File; | |||
| public interface WxMaQrcodeService { | |||
| /** | |||
| * 接口C | |||
| * <pre> | |||
| * 获取小程序页面二维码 | |||
| * 适用于需要的码数量较少的业务场景 | |||
| @@ -27,4 +31,83 @@ public interface WxMaQrcodeService { | |||
| * @param width 默认430 二维码的宽度 | |||
| */ | |||
| File createQrcode(String path, int width) throws WxErrorException; | |||
| File createQrcode(String path) throws WxErrorException; | |||
| /** | |||
| * 接口A | |||
| * 获取小程序码 | |||
| * | |||
| * @param path 不能为空,最大长度 128 字节 | |||
| * @param width 默认430 二维码的宽度 | |||
| * @param autoColor 默认true 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 | |||
| * @param lineColor auth_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} | |||
| * @return | |||
| * @throws WxErrorException | |||
| */ | |||
| File createWxCode(String path, int width, boolean autoColor, LineColor lineColor) throws WxErrorException; | |||
| File createWxCode(String path, int width) throws WxErrorException; | |||
| File createWxCode(String path) throws WxErrorException; | |||
| /** | |||
| * 接口B | |||
| * 获取小程序码(永久有效、数量暂无限制) | |||
| * <p> | |||
| * 通过该接口生成的小程序码,永久有效,数量暂无限制。 | |||
| * 用户扫描该码进入小程序后,将统一打开首页,开发者需在对应页面根据获取的码中 scene 字段的值,再做处理逻辑。 | |||
| * 使用如下代码可以获取到二维码中的 scene 字段的值。 | |||
| * 调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟,开发工具模拟时的 scene 的参数值需要进行 urlencode | |||
| * | |||
| * @param scene 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式) | |||
| * @param page 必须是已经发布的小程序页面,例如 "pages/index/index" ,如果不填写这个字段,默认跳主页面 | |||
| * @param width 默认false 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 | |||
| * @param autoColor 默认true 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 | |||
| * @param lineColor auth_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} | |||
| * @return | |||
| * @throws WxErrorException | |||
| */ | |||
| File createWxCodeLimit(String scene, String page, int width, boolean autoColor, LineColor lineColor) throws WxErrorException; | |||
| File createWxCodeLimit(String scene, String page) throws WxErrorException; | |||
| /** | |||
| * lineColor 包装类 | |||
| * 用于描述二维码(小程序码)颜色(RGB参数值),详情请查看文档 | |||
| */ | |||
| public static class LineColor { | |||
| private String r = "0", g = "0", b = "0"; | |||
| public LineColor(String r, String g, String b) { | |||
| this.r = r; | |||
| this.g = g; | |||
| this.b = b; | |||
| } | |||
| 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; | |||
| } | |||
| } | |||
| } | |||
| @@ -3,6 +3,8 @@ package cn.binarywang.wx.miniapp.api.impl; | |||
| import cn.binarywang.wx.miniapp.api.WxMaQrcodeService; | |||
| import cn.binarywang.wx.miniapp.api.WxMaService; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaQrcode; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaWxcode; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaWxcodeLimit; | |||
| import cn.binarywang.wx.miniapp.util.http.QrCodeRequestExecutor; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| @@ -25,4 +27,49 @@ public class WxMaQrcodeServiceImpl implements WxMaQrcodeService { | |||
| url, new WxMaQrcode(path, width)); | |||
| } | |||
| @Override | |||
| public File createQrcode(String path) throws WxErrorException { | |||
| return this.createQrcode(path, 430); | |||
| } | |||
| @Override | |||
| public File createWxCode(String path, int width, boolean autoColor, LineColor lineColor) throws WxErrorException { | |||
| String url = "https://api.weixin.qq.com/wxa/getwxacode"; | |||
| WxMaWxcode wxMaWxcode = new WxMaWxcode(); | |||
| wxMaWxcode.setPath(path); | |||
| wxMaWxcode.setWidth(width); | |||
| wxMaWxcode.setAutoColor(autoColor); | |||
| wxMaWxcode.setLineColor(lineColor); | |||
| return this.wxMaService.execute(new QrCodeRequestExecutor(this.wxMaService.getRequestHttp()), | |||
| url, wxMaWxcode); | |||
| } | |||
| @Override | |||
| public File createWxCode(String path, int width) throws WxErrorException { | |||
| return this.createWxCode(path, width, true, null); | |||
| } | |||
| @Override | |||
| public File createWxCode(String path) throws WxErrorException { | |||
| return this.createWxCode(path, 430, true, null); | |||
| } | |||
| @Override | |||
| public File createWxCodeLimit(String scene, String page, int width, boolean autoColor, LineColor lineColor) throws WxErrorException { | |||
| String url = "http://api.weixin.qq.com/wxa/getwxacodeunlimit"; | |||
| WxMaWxcodeLimit wxMaWxcodeLimit = new WxMaWxcodeLimit(); | |||
| wxMaWxcodeLimit.setScene(scene); | |||
| wxMaWxcodeLimit.setPage(page); | |||
| wxMaWxcodeLimit.setWidth(width); | |||
| wxMaWxcodeLimit.setAutoColor(autoColor); | |||
| wxMaWxcodeLimit.setLineColor(lineColor); | |||
| return this.wxMaService.execute(new QrCodeRequestExecutor(this.wxMaService.getRequestHttp()), | |||
| url, wxMaWxcodeLimit); | |||
| } | |||
| @Override | |||
| public File createWxCodeLimit(String scene, String page) throws WxErrorException { | |||
| return this.createWxCodeLimit(scene, page, 430, true, null); | |||
| } | |||
| } | |||
| @@ -18,6 +18,13 @@ public class WxMaJscode2SessionResult { | |||
| @SerializedName("openid") | |||
| private String openid; | |||
| @SerializedName("unionid") | |||
| private String unionid; | |||
| public static WxMaJscode2SessionResult fromJson(String json) { | |||
| return WxMaGsonBuilder.create().fromJson(json, WxMaJscode2SessionResult.class); | |||
| } | |||
| public String getSessionKey() { | |||
| return sessionKey; | |||
| } | |||
| @@ -42,8 +49,12 @@ public class WxMaJscode2SessionResult { | |||
| this.openid = openid; | |||
| } | |||
| public static WxMaJscode2SessionResult fromJson(String json) { | |||
| return WxMaGsonBuilder.create().fromJson(json, WxMaJscode2SessionResult.class); | |||
| public String getUnionid() { | |||
| return unionid; | |||
| } | |||
| public void setUnionid(String unionid) { | |||
| this.unionid = unionid; | |||
| } | |||
| } | |||
| @@ -7,7 +7,7 @@ import java.io.Serializable; | |||
| /** | |||
| * @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
| */ | |||
| public class WxMaQrcode implements Serializable { | |||
| public class WxMaQrcode extends WxMaQrcodeWrapper implements Serializable { | |||
| private static final long serialVersionUID = 5777119669111011584L; | |||
| private String path; | |||
| private int width = 430; | |||
| @@ -0,0 +1,16 @@ | |||
| package cn.binarywang.wx.miniapp.bean; | |||
| import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; | |||
| /** | |||
| * 微信二维码(小程序码)包装器 | |||
| * Created by Element on 2017/7/27. | |||
| */ | |||
| public abstract class WxMaQrcodeWrapper { | |||
| @Override | |||
| public String toString() { | |||
| return WxMaGsonBuilder.create().toJson(this); | |||
| } | |||
| } | |||
| @@ -0,0 +1,64 @@ | |||
| package cn.binarywang.wx.miniapp.bean; | |||
| import cn.binarywang.wx.miniapp.api.WxMaQrcodeService; | |||
| import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; | |||
| import com.google.gson.annotations.SerializedName; | |||
| import java.io.Serializable; | |||
| /** | |||
| * Created by Element on 2017/7/27. | |||
| */ | |||
| public class WxMaWxcode extends WxMaQrcodeWrapper implements Serializable { | |||
| private static final long serialVersionUID = 1287399621649210322L; | |||
| private String path; | |||
| private int width = 430; | |||
| @SerializedName("auto_color") | |||
| private boolean autoColor = true; | |||
| @SerializedName("line_color") | |||
| private WxMaQrcodeService.LineColor lineColor = new WxMaQrcodeService.LineColor("0", "0", "0"); | |||
| public static WxMaWxcode fromJson(String json) { | |||
| return WxMaGsonBuilder.create().fromJson(json, WxMaWxcode.class); | |||
| } | |||
| public static long getSerialVersionUID() { | |||
| return serialVersionUID; | |||
| } | |||
| public String getPath() { | |||
| return path; | |||
| } | |||
| public void setPath(String path) { | |||
| this.path = path; | |||
| } | |||
| public int getWidth() { | |||
| return width; | |||
| } | |||
| public void setWidth(int width) { | |||
| this.width = width; | |||
| } | |||
| public boolean isAutoColor() { | |||
| return autoColor; | |||
| } | |||
| public void setAutoColor(boolean autoColor) { | |||
| this.autoColor = autoColor; | |||
| } | |||
| public WxMaQrcodeService.LineColor getLineColor() { | |||
| return lineColor; | |||
| } | |||
| public void setLineColor(WxMaQrcodeService.LineColor lineColor) { | |||
| this.lineColor = lineColor; | |||
| } | |||
| } | |||
| @@ -0,0 +1,68 @@ | |||
| package cn.binarywang.wx.miniapp.bean; | |||
| import cn.binarywang.wx.miniapp.api.WxMaQrcodeService; | |||
| import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; | |||
| import com.google.gson.annotations.SerializedName; | |||
| import java.io.Serializable; | |||
| /** | |||
| * Created by Element on 2017/7/27. | |||
| */ | |||
| public class WxMaWxcodeLimit extends WxMaQrcodeWrapper implements Serializable { | |||
| private static final long serialVersionUID = 4782193774524960401L; | |||
| private String scene; | |||
| private String page; | |||
| private int width = 430; | |||
| @SerializedName("auto_color") | |||
| private boolean autoColor = true; | |||
| @SerializedName("line_color") | |||
| private WxMaQrcodeService.LineColor lineColor = new WxMaQrcodeService.LineColor("0", "0", "0"); | |||
| public static WxMaWxcodeLimit fromJson(String json) { | |||
| return WxMaGsonBuilder.create().fromJson(json, WxMaWxcodeLimit.class); | |||
| } | |||
| public String getPage() { | |||
| return page; | |||
| } | |||
| public void setPage(String page) { | |||
| this.page = page; | |||
| } | |||
| public String getScene() { | |||
| return scene; | |||
| } | |||
| public void setScene(String scene) { | |||
| this.scene = scene; | |||
| } | |||
| public int getWidth() { | |||
| return width; | |||
| } | |||
| public void setWidth(int width) { | |||
| this.width = width; | |||
| } | |||
| public boolean isAutoColor() { | |||
| return autoColor; | |||
| } | |||
| public void setAutoColor(boolean autoColor) { | |||
| this.autoColor = autoColor; | |||
| } | |||
| public WxMaQrcodeService.LineColor getLineColor() { | |||
| return lineColor; | |||
| } | |||
| public void setLineColor(WxMaQrcodeService.LineColor lineColor) { | |||
| this.lineColor = lineColor; | |||
| } | |||
| } | |||
| @@ -10,7 +10,7 @@ public final class ImageBuilder extends BaseBuilder<ImageBuilder> { | |||
| private String mediaId; | |||
| public ImageBuilder() { | |||
| this.msgType = WxMaConstants.KefuMsgType.IMAGE; | |||
| this.msgType = WxMaConstants.KefuMsgType.IMAGE; | |||
| } | |||
| public ImageBuilder mediaId(String media_id) { | |||
| @@ -17,7 +17,7 @@ public interface WxMaMessageInterceptor { | |||
| /** | |||
| * 拦截微信消息 | |||
| * | |||
| * @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个 | |||
| * @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个 | |||
| * @return true代表OK,false代表不OK | |||
| */ | |||
| boolean intercept(WxMaMessage wxMessage, | |||
| @@ -1,6 +1,6 @@ | |||
| package cn.binarywang.wx.miniapp.util.http; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaQrcode; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaQrcodeWrapper; | |||
| import me.chanjar.weixin.common.bean.result.WxError; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import me.chanjar.weixin.common.util.fs.FileUtils; | |||
| @@ -25,7 +25,7 @@ import java.util.UUID; | |||
| /** | |||
| * @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
| */ | |||
| public class QrCodeRequestExecutor implements RequestExecutor<File, WxMaQrcode> { | |||
| public class QrCodeRequestExecutor implements RequestExecutor<File, WxMaQrcodeWrapper> { | |||
| protected RequestHttp<CloseableHttpClient, HttpHost> requestHttp; | |||
| public QrCodeRequestExecutor(RequestHttp requestHttp) { | |||
| @@ -33,7 +33,7 @@ public class QrCodeRequestExecutor implements RequestExecutor<File, WxMaQrcode> | |||
| } | |||
| @Override | |||
| public File execute(String uri, WxMaQrcode ticket) throws WxErrorException, IOException { | |||
| public File execute(String uri, WxMaQrcodeWrapper ticket) throws WxErrorException, IOException { | |||
| HttpPost httpPost = new HttpPost(uri); | |||
| if (requestHttp.getRequestHttpProxy() != null) { | |||
| httpPost | |||
| @@ -14,7 +14,6 @@ import com.google.gson.JsonElement; | |||
| import com.google.gson.JsonObject; | |||
| import com.google.gson.JsonSerializationContext; | |||
| import com.google.gson.JsonSerializer; | |||
| import org.apache.commons.lang3.StringUtils; | |||
| import java.lang.reflect.Type; | |||
| @@ -9,7 +9,8 @@ import com.google.common.collect.Lists; | |||
| import com.google.inject.Inject; | |||
| import me.chanjar.weixin.common.api.WxConsts; | |||
| import me.chanjar.weixin.common.exception.WxErrorException; | |||
| import org.testng.annotations.*; | |||
| import org.testng.annotations.Guice; | |||
| import org.testng.annotations.Test; | |||
| import java.text.SimpleDateFormat; | |||
| import java.util.Date; | |||
| @@ -23,4 +23,16 @@ public class WxMaQrcodeServiceImplTest { | |||
| System.out.println(qrCode); | |||
| } | |||
| @Test | |||
| public void testCreateWxCode() throws Exception { | |||
| final File wxCode = this.wxService.getQrcodeService().createWxCode("111", 122); | |||
| System.out.println(wxCode); | |||
| } | |||
| @Test | |||
| public void testCreateWxCodeLimit() throws Exception { | |||
| final File wxCode = this.wxService.getQrcodeService().createWxCodeLimit("111", null); | |||
| System.out.println(wxCode); | |||
| } | |||
| } | |||
| @@ -1,6 +1,5 @@ | |||
| package cn.binarywang.wx.miniapp.bean; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaMessage; | |||
| import me.chanjar.weixin.common.api.WxConsts; | |||
| import org.testng.annotations.Test; | |||
| @@ -2,8 +2,8 @@ package cn.binarywang.wx.miniapp.demo; | |||
| import cn.binarywang.wx.miniapp.api.WxMaService; | |||
| import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaMessage; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaMessage; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage; | |||
| import cn.binarywang.wx.miniapp.config.WxMaConfig; | |||
| import cn.binarywang.wx.miniapp.constant.WxMaConstants; | |||
| @@ -69,7 +69,7 @@ public class WxMaDemoServer { | |||
| } | |||
| } | |||
| }; | |||
| private static final WxMaMessageHandler qrcodeHandler = new WxMaMessageHandler() { | |||
| @Override | |||
| public void handle(WxMaMessage wxMessage, Map<String, Object> context, | |||