dev --- 3.8.0.A版本, openProject引用 ; formao-live --- 3.7.0.B 版本, formallProject引用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.3 KiB

  1. /*
  2. * KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved.
  3. *
  4. * This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended
  5. * only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction
  6. * arose from modification of the original source, or other redistribution of this source
  7. * is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD.
  8. */
  9. package chanjarster.weixin.util;
  10. import java.lang.reflect.Type;
  11. import chanjarster.weixin.out.WxCustomMessage;
  12. import chanjarster.weixin.out.WxCustomMessage.WxArticle;
  13. import chanjarster.weixin.service.WxMsgType;
  14. import com.google.gson.JsonArray;
  15. import com.google.gson.JsonElement;
  16. import com.google.gson.JsonObject;
  17. import com.google.gson.JsonSerializationContext;
  18. import com.google.gson.JsonSerializer;
  19. /**
  20. *
  21. * @author qianjia
  22. *
  23. */
  24. public class WxCustomMessageGsonAdapter implements JsonSerializer<WxCustomMessage> {
  25. public JsonElement serialize(WxCustomMessage message, Type typeOfSrc, JsonSerializationContext context) {
  26. JsonObject messageJson = new JsonObject();
  27. messageJson.addProperty("touser", message.getTouser());
  28. messageJson.addProperty("msgtype", message.getMsgtype());
  29. if (WxMsgType.TEXT.equals(message.getMsgtype())) {
  30. JsonObject text = new JsonObject();
  31. text.addProperty("content", message.getContent());
  32. messageJson.add("text", text);
  33. }
  34. if (WxMsgType.IMAGE.equals(message.getMsgtype())) {
  35. JsonObject image = new JsonObject();
  36. image.addProperty("media_id", message.getMedia_id());
  37. messageJson.add("image", image);
  38. }
  39. if (WxMsgType.VOICE.equals(message.getMsgtype())) {
  40. JsonObject voice = new JsonObject();
  41. voice.addProperty("media_id", message.getMedia_id());
  42. messageJson.add("voice", voice);
  43. }
  44. if (WxMsgType.VIDEO.equals(message.getMsgtype())) {
  45. JsonObject video = new JsonObject();
  46. video.addProperty("media_id", message.getMedia_id());
  47. video.addProperty("thumb_media_id", message.getThumb_media_id());
  48. video.addProperty("title", message.getTitle());
  49. video.addProperty("description", message.getDescription());
  50. messageJson.add("video", video);
  51. }
  52. if (WxMsgType.MUSIC.equals(message.getMsgtype())) {
  53. JsonObject music = new JsonObject();
  54. music.addProperty("title", message.getTitle());
  55. music.addProperty("description", message.getDescription());
  56. music.addProperty("thumb_media_id", message.getThumb_media_id());
  57. music.addProperty("musicurl", message.getMusicurl());
  58. music.addProperty("hqmusicurl", message.getHqmusicurl());
  59. messageJson.add("music", music);
  60. }
  61. if (WxMsgType.NEWS.equals(message.getMsgtype())) {
  62. JsonArray articleJsonArray = new JsonArray();
  63. for (WxArticle article : message.getArticles()) {
  64. JsonObject articleJson = new JsonObject();
  65. articleJson.addProperty("title", article.getTitle());
  66. articleJson.addProperty("description", article.getDescription());
  67. articleJson.addProperty("url", article.getUrl());
  68. articleJson.addProperty("picurl", article.getPicurl());
  69. articleJsonArray.add(articleJson);
  70. }
  71. messageJson.add("articles", articleJsonArray);
  72. }
  73. return messageJson;
  74. }
  75. }