Skip to content

Commit c0f1632

Browse files
committed
Merge pull request #2 from kareanyi/develop
消息群发的预览接口
2 parents 46b3eba + 83e443b commit c0f1632

File tree

5 files changed

+146
-11
lines changed

5 files changed

+146
-11
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public interface WxMpService {
351351
* @throws WxErrorException
352352
*/
353353
public void menuDelete(String menuid) throws WxErrorException;
354-
354+
355355
/**
356356
* <pre>
357357
* 自定义菜单查询接口
@@ -361,7 +361,7 @@ public interface WxMpService {
361361
* @throws WxErrorException
362362
*/
363363
public WxMenu menuGet() throws WxErrorException;
364-
364+
365365
/**
366366
* <pre>
367367
* 测试个性化菜单匹配结果
@@ -723,7 +723,7 @@ public interface WxMpService {
723723
/**
724724
* 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1)
725725
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识"
726-
*
726+
*
727727
* @param parameters
728728
* All required/optional parameters for weixin payment
729729
* @return
@@ -737,10 +737,10 @@ public interface WxMpService {
737737
* @param parameters
738738
* the required or optional parameters
739739
* @return
740-
* @throws WxErrorException
740+
* @throws WxErrorException
741741
*/
742-
Map<String, String> getJSSDKPayInfo(Map<String, String> parameters) throws WxErrorException;
743-
742+
Map<String, String> getJSSDKPayInfo(Map<String, String> parameters) throws WxErrorException;
743+
744744
/**
745745
* 该接口调用“统一下单”接口,并拼装JSSDK发起支付请求需要的参数
746746
* 详见http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#.E5.8F.91.E8.B5.B7.E4.B8.80.E4.B8.AA.E5.BE.AE.E4.BF.A1.E6.94.AF.E4.BB.98.E8.AF.B7.E6.B1.82
@@ -752,7 +752,7 @@ public interface WxMpService {
752752
* @param ip 发起支付的客户端IP
753753
* @param notifyUrl 通知地址
754754
* @return
755-
* @throws WxErrorException
755+
* @throws WxErrorException
756756
* @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getJSSDKPayInfo(Map<String, String>) instead
757757
*/
758758
@Deprecated
@@ -773,7 +773,7 @@ public interface WxMpService {
773773
* @return
774774
*/
775775
WxMpPayCallback getJSSDKCallbackData(String xmlData);
776-
776+
777777
/**
778778
* 微信支付-申请退款
779779
* 详见 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
@@ -784,10 +784,10 @@ public interface WxMpService {
784784
* <li/> total_fee
785785
* <li/> refund_fee
786786
* @return 退款操作结果
787-
* @throws WxErrorException
787+
* @throws WxErrorException
788788
*/
789789
public WxMpPayRefundResult refundPay(Map<String, String> parameters) throws WxErrorException;
790-
790+
791791
/**
792792
* <pre>
793793
* 计算Map键值对是否和签名相符,
@@ -914,7 +914,7 @@ public WxMpCardResult queryCardCode(String cardId, String code, boolean checkCon
914914
*/
915915
public void markCardCode(String code, String cardId, String openId, boolean isMark) throws
916916
WxErrorException;
917-
917+
918918
/**
919919
* 查看卡券详情接口
920920
* 详见 https://mp.weixin.qq.com/wiki/14/8dd77aeaee85f922db5f8aa6386d385e.html#.E6.9F.A5.E7.9C.8B.E5.8D.A1.E5.88.B8.E8.AF.A6.E6.83.85
@@ -925,4 +925,15 @@ public void markCardCode(String code, String cardId, String openId, boolean isMa
925925
* @throws WxErrorException
926926
*/
927927
public String getCardDetail(String cardId) throws WxErrorException;
928+
929+
/*
930+
* <pre>
931+
* 预览接口
932+
* 详情请见:http://mp.weixin.qq.com/wiki/15/40b6865b893947b764e2de8e4a1fb55f.html#.E9.A2.84.E8.A7.88.E6.8E.A5.E5.8F.A3.E3.80.90.E8.AE.A2.E9.98.85.E5.8F.B7.E4.B8.8E.E6.9C.8D.E5.8A.A1.E5.8F.B7.E8.AE.A4.E8.AF.81.E5.90.8E.E5.9D.87.E5.8F.AF.E7.94.A8.E3.80.91
933+
* </pre>
934+
* @param wxMpMassPreviewMessage
935+
* @return wxMpMassSendResult
936+
* @throws WxErrorException
937+
*/
938+
public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception;
928939
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpServiceImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,4 +1259,11 @@ public String getCardDetail(String cardId) throws WxErrorException {
12591259
return responseContent;
12601260
}
12611261

1262+
@Override
1263+
public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception {
1264+
String url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview";
1265+
String responseContent = execute(new SimplePostRequestExecutor(), url, wxMpMassPreviewMessage.toJson());
1266+
return WxMpMassSendResult.fromJson(responseContent);
1267+
}
1268+
12621269
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package me.chanjar.weixin.mp.bean;
2+
3+
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
4+
5+
import java.io.Serializable;
6+
7+
/**
8+
* @author miller
9+
*/
10+
public class WxMpMassPreviewMessage implements Serializable {
11+
private String toWxUsername;
12+
private String msgType;
13+
private String content;
14+
private String mediaId;
15+
16+
public WxMpMassPreviewMessage() {
17+
super();
18+
}
19+
20+
public String getToWxUsername() {
21+
return toWxUsername;
22+
}
23+
24+
public void setToWxUsername(String toWxUsername) {
25+
this.toWxUsername = toWxUsername;
26+
}
27+
28+
public String getMsgType() {
29+
return msgType;
30+
}
31+
32+
/**
33+
* <pre>
34+
* 请使用
35+
* {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_IMAGE}
36+
* {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_NEWS}
37+
* {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_TEXT}
38+
* {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_VIDEO}
39+
* {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_VOICE}
40+
* 如果msgtype和media_id不匹配的话,会返回系统繁忙的错误
41+
* </pre>
42+
*
43+
* @param msgType
44+
*/
45+
public void setMsgType(String msgType) {
46+
this.msgType = msgType;
47+
}
48+
49+
public String getContent() {
50+
return content;
51+
}
52+
53+
public void setContent(String content) {
54+
this.content = content;
55+
}
56+
57+
public String getMediaId() {
58+
return mediaId;
59+
}
60+
61+
public void setMediaId(String mediaId) {
62+
this.mediaId = mediaId;
63+
}
64+
65+
public String toJson() {
66+
return WxMpGsonBuilder.INSTANCE.create().toJson(this);
67+
}
68+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class WxMpGsonBuilder {
4040
INSTANCE.registerTypeAdapter(WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem.class, new WxMpMaterialFileBatchGetGsonItemAdapter());
4141
INSTANCE.registerTypeAdapter(WxMpCardResult.class, new WxMpCardResultGsonAdapter());
4242
INSTANCE.registerTypeAdapter(WxMpCard.class, new WxMpCardGsonAdapter());
43+
INSTANCE.registerTypeAdapter(WxMpMassPreviewMessage.class, new WxMpMassPreviewMessageGsonAdapter());
4344
}
4445

4546
public static Gson create() {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package me.chanjar.weixin.mp.util.json;
2+
3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonObject;
5+
import com.google.gson.JsonSerializationContext;
6+
import com.google.gson.JsonSerializer;
7+
import me.chanjar.weixin.common.api.WxConsts;
8+
import me.chanjar.weixin.mp.bean.WxMpMassPreviewMessage;
9+
10+
import java.lang.reflect.Type;
11+
12+
/**
13+
* @author miller
14+
*/
15+
public class WxMpMassPreviewMessageGsonAdapter implements JsonSerializer<WxMpMassPreviewMessage> {
16+
@Override
17+
public JsonElement serialize(WxMpMassPreviewMessage wxMpMassPreviewMessage, Type type, JsonSerializationContext jsonSerializationContext) {
18+
JsonObject jsonObject = new JsonObject();
19+
jsonObject.addProperty("towxname", wxMpMassPreviewMessage.getToWxUsername());
20+
if (WxConsts.MASS_MSG_NEWS.equals(wxMpMassPreviewMessage.getMsgType())) {
21+
JsonObject news = new JsonObject();
22+
news.addProperty("media_id", wxMpMassPreviewMessage.getMediaId());
23+
jsonObject.add(WxConsts.MASS_MSG_NEWS, news);
24+
}
25+
if (WxConsts.MASS_MSG_TEXT.equals(wxMpMassPreviewMessage.getMsgType())) {
26+
JsonObject sub = new JsonObject();
27+
sub.addProperty("content", wxMpMassPreviewMessage.getContent());
28+
jsonObject.add(WxConsts.MASS_MSG_TEXT, sub);
29+
}
30+
if (WxConsts.MASS_MSG_VOICE.equals(wxMpMassPreviewMessage.getMsgType())) {
31+
JsonObject sub = new JsonObject();
32+
sub.addProperty("media_id", wxMpMassPreviewMessage.getMediaId());
33+
jsonObject.add(WxConsts.MASS_MSG_VOICE, sub);
34+
}
35+
if (WxConsts.MASS_MSG_IMAGE.equals(wxMpMassPreviewMessage.getMsgType())) {
36+
JsonObject sub = new JsonObject();
37+
sub.addProperty("media_id", wxMpMassPreviewMessage.getMediaId());
38+
jsonObject.add(WxConsts.MASS_MSG_IMAGE, sub);
39+
}
40+
if (WxConsts.MASS_MSG_VIDEO.equals(wxMpMassPreviewMessage.getMsgType())) {
41+
JsonObject sub = new JsonObject();
42+
sub.addProperty("media_id", wxMpMassPreviewMessage.getMediaId());
43+
jsonObject.add(WxConsts.MASS_MSG_VIDEO, sub);
44+
}
45+
jsonObject.addProperty("msgtype", wxMpMassPreviewMessage.getMsgType());
46+
return jsonObject;
47+
}
48+
}

0 commit comments

Comments
 (0)