Skip to content

Commit ebf9baa

Browse files
committed
#534 针对公众号发送模版消息中的小程序path和pagepath问题增加可选参数,如果想让请求使用path作为参数名,设置usePath属性为true即可,否则会使用pagepath。
1 parent a24b746 commit ebf9baa

File tree

6 files changed

+37
-13
lines changed

6 files changed

+37
-13
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/subscribe/WxMpSubscribeMessage.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package me.chanjar.weixin.mp.bean.subscribe;
22

3+
import java.io.Serializable;
4+
35
import lombok.AllArgsConstructor;
46
import lombok.Builder;
57
import lombok.Data;
68
import lombok.NoArgsConstructor;
7-
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
89
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
910

10-
import java.io.Serializable;
11-
1211
/**
1312
* @author Mklaus
1413
* @date 2018-01-22 下午12:18
@@ -78,5 +77,11 @@ public static class MiniProgram implements Serializable {
7877

7978
private String appid;
8079
private String pagePath;
80+
81+
/**
82+
* 是否使用path,否则使用pagepath.
83+
* 加入此字段是基于微信官方接口变化多端的考虑
84+
*/
85+
private boolean usePath = false;
8186
}
8287
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/template/WxMpTemplateMessage.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ public static class MiniProgram implements Serializable {
7777

7878
private String appid;
7979
private String pagePath;
80+
81+
/**
82+
* 是否使用path,否则使用pagepath.
83+
* 加入此字段是基于微信官方接口变化多端的考虑
84+
*/
85+
private boolean usePath = true;
8086
}
8187

8288
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpSubscribeMessageGsonAdapter.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package me.chanjar.weixin.mp.util.json;
22

3+
import java.lang.reflect.Type;
4+
35
import com.google.gson.JsonElement;
46
import com.google.gson.JsonObject;
57
import com.google.gson.JsonSerializationContext;
68
import com.google.gson.JsonSerializer;
79
import me.chanjar.weixin.mp.bean.subscribe.WxMpSubscribeMessage;
810

9-
import java.lang.reflect.Type;
10-
1111
/**
1212
* @author Mklaus
1313
* @date 2018-01-22 下午12:31
@@ -24,10 +24,15 @@ public JsonElement serialize(WxMpSubscribeMessage message, Type type, JsonSerial
2424
messageJson.addProperty("url", message.getUrl());
2525
}
2626

27-
if (message.getMiniProgram() != null) {
27+
final WxMpSubscribeMessage.MiniProgram miniProgram = message.getMiniProgram();
28+
if (miniProgram != null) {
2829
JsonObject miniProgramJson = new JsonObject();
29-
miniProgramJson.addProperty("appid", message.getMiniProgram().getAppid());
30-
miniProgramJson.addProperty("pagepath", message.getMiniProgram().getPagePath());
30+
miniProgramJson.addProperty("appid", miniProgram.getAppid());
31+
if (miniProgram.isUsePath()) {
32+
miniProgramJson.addProperty("path", miniProgram.getPagePath());
33+
} else {
34+
miniProgramJson.addProperty("pagepath", miniProgram.getPagePath());
35+
}
3136
messageJson.add("miniprogram", miniProgramJson);
3237
}
3338

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/json/WxMpTemplateMessageGsonAdapter.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
1010
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
1111

12+
/**
13+
* @author chanjarster
14+
*/
1215
public class WxMpTemplateMessageGsonAdapter implements JsonSerializer<WxMpTemplateMessage> {
1316

1417
@Override
@@ -20,10 +23,15 @@ public JsonElement serialize(WxMpTemplateMessage message, Type typeOfSrc, JsonSe
2023
messageJson.addProperty("url", message.getUrl());
2124
}
2225

23-
if (message.getMiniProgram() != null) {
26+
final WxMpTemplateMessage.MiniProgram miniProgram = message.getMiniProgram();
27+
if (miniProgram != null) {
2428
JsonObject miniProgramJson = new JsonObject();
25-
miniProgramJson.addProperty("appid", message.getMiniProgram().getAppid());
26-
miniProgramJson.addProperty("pagepath", message.getMiniProgram().getPagePath());
29+
miniProgramJson.addProperty("appid", miniProgram.getAppid());
30+
if (miniProgram.isUsePath()) {
31+
miniProgramJson.addProperty("path", miniProgram.getPagePath());
32+
} else {
33+
miniProgramJson.addProperty("pagepath", miniProgram.getPagePath());
34+
}
2735
messageJson.add("miniprogram", miniProgramJson);
2836
}
2937

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/bean/subscribe/WxMpSubscribeMessageTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void testToJson() {
3333
.toUser("OPENID")
3434
.templateId("TEMPLATE_ID")
3535
.url("URL")
36-
.miniProgram(new WxMpSubscribeMessage.MiniProgram("xiaochengxuappid12345", "index?foo=bar"))
36+
.miniProgram(new WxMpSubscribeMessage.MiniProgram("xiaochengxuappid12345", "index?foo=bar",false))
3737
.scene("SCENE")
3838
.title("TITLE")
3939
.contentValue("VALUE")

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/bean/template/WxMpTemplateMessageTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void testToJson() {
1717
WxMpTemplateMessage tm = WxMpTemplateMessage.builder()
1818
.toUser("OPENID")
1919
.templateId("ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY")
20-
.miniProgram(new WxMpTemplateMessage.MiniProgram("xiaochengxuappid12345", "index?foo=bar"))
20+
.miniProgram(new WxMpTemplateMessage.MiniProgram("xiaochengxuappid12345", "index?foo=bar",true))
2121
.url("http://weixin.qq.com/download")
2222
.build();
2323

0 commit comments

Comments
 (0)