mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-11-01 03:25:35 +08:00 
			
		
		
		
	消息群发的预览接口
This commit is contained in:
		| @ -925,4 +925,15 @@ public interface WxMpService { | |||||||
|    * @throws WxErrorException |    * @throws WxErrorException | ||||||
|    */ |    */ | ||||||
|   public String getCardDetail(String cardId) throws WxErrorException; |   public String getCardDetail(String cardId) throws WxErrorException; | ||||||
|  |  | ||||||
|  |   /* | ||||||
|  |      * <pre> | ||||||
|  |      * 预览接口 | ||||||
|  |      * 详情请见: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 | ||||||
|  |      * </pre> | ||||||
|  |      * @param wxMpMassPreviewMessage | ||||||
|  |      * @return wxMpMassSendResult | ||||||
|  |      * @throws WxErrorException | ||||||
|  |      */ | ||||||
|  |   public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception; | ||||||
| } | } | ||||||
|  | |||||||
| @ -1259,4 +1259,11 @@ public class WxMpServiceImpl implements WxMpService { | |||||||
|     return responseContent; |     return responseContent; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   @Override | ||||||
|  |   public WxMpMassSendResult massMessagePreview(WxMpMassPreviewMessage wxMpMassPreviewMessage) throws Exception { | ||||||
|  |     String url = "https://api.weixin.qq.com/cgi-bin/message/mass/preview"; | ||||||
|  |     String responseContent = execute(new SimplePostRequestExecutor(), url, wxMpMassPreviewMessage.toJson()); | ||||||
|  |     return WxMpMassSendResult.fromJson(responseContent); | ||||||
|  |   } | ||||||
|  |  | ||||||
| } | } | ||||||
|  | |||||||
| @ -0,0 +1,68 @@ | |||||||
|  | package me.chanjar.weixin.mp.bean; | ||||||
|  |  | ||||||
|  | import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; | ||||||
|  |  | ||||||
|  | import java.io.Serializable; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * @author miller | ||||||
|  |  */ | ||||||
|  | public class WxMpMassPreviewMessage implements Serializable { | ||||||
|  |   private String toWxUsername; | ||||||
|  |   private String msgType; | ||||||
|  |   private String content; | ||||||
|  |   private String mediaId; | ||||||
|  |  | ||||||
|  |   public WxMpMassPreviewMessage() { | ||||||
|  |     super(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   public String getToWxUsername() { | ||||||
|  |     return toWxUsername; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   public void setToWxUsername(String toWxUsername) { | ||||||
|  |     this.toWxUsername = toWxUsername; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   public String getMsgType() { | ||||||
|  |     return msgType; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * <pre> | ||||||
|  |    * 请使用 | ||||||
|  |    * {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_IMAGE} | ||||||
|  |    * {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_NEWS} | ||||||
|  |    * {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_TEXT} | ||||||
|  |    * {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_VIDEO} | ||||||
|  |    * {@link me.chanjar.weixin.common.api.WxConsts#MASS_MSG_VOICE} | ||||||
|  |    * 如果msgtype和media_id不匹配的话,会返回系统繁忙的错误 | ||||||
|  |    * </pre> | ||||||
|  |    * | ||||||
|  |    * @param msgType | ||||||
|  |    */ | ||||||
|  |   public void setMsgType(String msgType) { | ||||||
|  |     this.msgType = msgType; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   public String getContent() { | ||||||
|  |     return content; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   public void setContent(String content) { | ||||||
|  |     this.content = content; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   public String getMediaId() { | ||||||
|  |     return mediaId; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   public void setMediaId(String mediaId) { | ||||||
|  |     this.mediaId = mediaId; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   public String toJson() { | ||||||
|  |     return WxMpGsonBuilder.INSTANCE.create().toJson(this); | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -40,6 +40,7 @@ public class WxMpGsonBuilder { | |||||||
|     INSTANCE.registerTypeAdapter(WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem.class, new WxMpMaterialFileBatchGetGsonItemAdapter()); |     INSTANCE.registerTypeAdapter(WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem.class, new WxMpMaterialFileBatchGetGsonItemAdapter()); | ||||||
|     INSTANCE.registerTypeAdapter(WxMpCardResult.class, new WxMpCardResultGsonAdapter()); |     INSTANCE.registerTypeAdapter(WxMpCardResult.class, new WxMpCardResultGsonAdapter()); | ||||||
|     INSTANCE.registerTypeAdapter(WxMpCard.class, new WxMpCardGsonAdapter()); |     INSTANCE.registerTypeAdapter(WxMpCard.class, new WxMpCardGsonAdapter()); | ||||||
|  |     INSTANCE.registerTypeAdapter(WxMpMassPreviewMessage.class, new WxMpMassPreviewMessageGsonAdapter()); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   public static Gson create() { |   public static Gson create() { | ||||||
|  | |||||||
| @ -0,0 +1,48 @@ | |||||||
|  | package me.chanjar.weixin.mp.util.json; | ||||||
|  |  | ||||||
|  | import com.google.gson.JsonElement; | ||||||
|  | import com.google.gson.JsonObject; | ||||||
|  | import com.google.gson.JsonSerializationContext; | ||||||
|  | import com.google.gson.JsonSerializer; | ||||||
|  | import me.chanjar.weixin.common.api.WxConsts; | ||||||
|  | import me.chanjar.weixin.mp.bean.WxMpMassPreviewMessage; | ||||||
|  |  | ||||||
|  | import java.lang.reflect.Type; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * @author miller | ||||||
|  |  */ | ||||||
|  | public class WxMpMassPreviewMessageGsonAdapter implements JsonSerializer<WxMpMassPreviewMessage> { | ||||||
|  |   @Override | ||||||
|  |   public JsonElement serialize(WxMpMassPreviewMessage wxMpMassPreviewMessage, Type type, JsonSerializationContext jsonSerializationContext) { | ||||||
|  |     JsonObject jsonObject = new JsonObject(); | ||||||
|  |     jsonObject.addProperty("towxname", wxMpMassPreviewMessage.getToWxUsername()); | ||||||
|  |     if (WxConsts.MASS_MSG_NEWS.equals(wxMpMassPreviewMessage.getMsgType())) { | ||||||
|  |       JsonObject news = new JsonObject(); | ||||||
|  |       news.addProperty("media_id", wxMpMassPreviewMessage.getMediaId()); | ||||||
|  |       jsonObject.add(WxConsts.MASS_MSG_NEWS, news); | ||||||
|  |     } | ||||||
|  |     if (WxConsts.MASS_MSG_TEXT.equals(wxMpMassPreviewMessage.getMsgType())) { | ||||||
|  |       JsonObject sub = new JsonObject(); | ||||||
|  |       sub.addProperty("content", wxMpMassPreviewMessage.getContent()); | ||||||
|  |       jsonObject.add(WxConsts.MASS_MSG_TEXT, sub); | ||||||
|  |     } | ||||||
|  |     if (WxConsts.MASS_MSG_VOICE.equals(wxMpMassPreviewMessage.getMsgType())) { | ||||||
|  |       JsonObject sub = new JsonObject(); | ||||||
|  |       sub.addProperty("media_id", wxMpMassPreviewMessage.getMediaId()); | ||||||
|  |       jsonObject.add(WxConsts.MASS_MSG_VOICE, sub); | ||||||
|  |     } | ||||||
|  |     if (WxConsts.MASS_MSG_IMAGE.equals(wxMpMassPreviewMessage.getMsgType())) { | ||||||
|  |       JsonObject sub = new JsonObject(); | ||||||
|  |       sub.addProperty("media_id", wxMpMassPreviewMessage.getMediaId()); | ||||||
|  |       jsonObject.add(WxConsts.MASS_MSG_IMAGE, sub); | ||||||
|  |     } | ||||||
|  |     if (WxConsts.MASS_MSG_VIDEO.equals(wxMpMassPreviewMessage.getMsgType())) { | ||||||
|  |       JsonObject sub = new JsonObject(); | ||||||
|  |       sub.addProperty("media_id", wxMpMassPreviewMessage.getMediaId()); | ||||||
|  |       jsonObject.add(WxConsts.MASS_MSG_VIDEO, sub); | ||||||
|  |     } | ||||||
|  |     jsonObject.addProperty("msgtype", wxMpMassPreviewMessage.getMsgType()); | ||||||
|  |     return jsonObject; | ||||||
|  |   } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user
	 miller
					miller