/src/main/java/weixin/idea/extend/function/impl/TianQiKeyService.java

https://gitlab.com/spiderworts/ab_weixin_cms · Java · 132 lines · 107 code · 14 blank · 11 comment · 3 complexity · 428aad0b0d3d8ca562e43799a221143a MD5 · raw file

  1. package weixin.idea.extend.function.impl;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import javax.servlet.http.HttpServletRequest;
  8. import weixin.guanjia.core.entity.message.resp.Article;
  9. import weixin.guanjia.core.entity.message.resp.NewsMessageResp;
  10. import weixin.guanjia.core.entity.message.resp.TextMessageResp;
  11. import weixin.guanjia.core.util.MessageUtil;
  12. import weixin.guanjia.core.util.weather.Weather;
  13. import weixin.idea.extend.function.KeyServiceI;
  14. /**
  15. * 天气功能类
  16. * 关键字:天气,7
  17. * 功能类:weixin.idea.key.function.impl.TianQiKeyService
  18. * 功能:天气预报
  19. * @author zhangdaihao
  20. *
  21. */
  22. public class TianQiKeyService implements KeyServiceI {
  23. @Override
  24. public String getKey() {
  25. return "天气,7";
  26. }
  27. @Override
  28. public String excute(String content,TextMessageResp defaultMessage,HttpServletRequest request) {
  29. String respMessage = null;
  30. String keyWord = content.replaceAll("天气", "").trim();
  31. if ("".equals(keyWord) || "7".equals(keyWord)) {
  32. StringBuffer buffer = new StringBuffer();
  33. // 接收用户发送的文本消息内容
  34. buffer.append("发送天气+城市,例如'天气广州'").append("\n");
  35. defaultMessage.setContent(buffer.toString());
  36. respMessage = MessageUtil.textMessageToXml(defaultMessage);
  37. } else {
  38. Map<String, Object> map = new HashMap<String, Object>();
  39. Map<String, Object> map2 = new HashMap<String, Object>();
  40. try {
  41. String filepach = request.getSession().getServletContext().getRealPath(
  42. "/WEB-INF/classes/weixin");
  43. Weather util = new Weather();
  44. map = util.report(keyWord, filepach);
  45. map2 = util.report2(keyWord, filepach);
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. StringBuffer buffer = new StringBuffer();
  49. // 接收用户发送的文本消息内容
  50. buffer.append("发送天气+城市,例如'天气广州'").append("\n");
  51. defaultMessage.setContent(buffer.toString());
  52. respMessage = MessageUtil
  53. .textMessageToXml(defaultMessage);
  54. return respMessage;
  55. }
  56. List<Article> articleList = new ArrayList<Article>();
  57. // 创建图文消息
  58. NewsMessageResp newsMessage = new NewsMessageResp();
  59. newsMessage.setToUserName(defaultMessage.getToUserName());
  60. newsMessage.setFromUserName(defaultMessage.getFromUserName());
  61. newsMessage.setCreateTime(new Date().getTime());
  62. newsMessage
  63. .setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
  64. Article article1 = new Article();
  65. article1.setTitle("【" + content.substring(2)
  66. + "】天气实况 气温:" + map2.get("temp") + "度 风速:"
  67. + map2.get("WD") + " " + map2.get("WS")
  68. + " 湿度:" + map2.get("SD"));
  69. article1.setDescription("");
  70. article1.setPicUrl("");
  71. article1.setUrl("");
  72. Article article2 = new Article();
  73. article2.setTitle("建议:" + map.get("index_d"));
  74. article2.setDescription("");
  75. article2.setPicUrl("");
  76. article2.setUrl("");
  77. Article article3 = new Article();
  78. article3.setTitle(map.get("mydate1") + " "
  79. + map.get("myweek1") + " " + map.get("temp1")
  80. + " " + map.get("weather1") + " "
  81. + map.get("wind1") + " " + map.get("fl1"));
  82. article3.setDescription("");
  83. article3.setPicUrl("http://m.weather.com.cn/img/b"
  84. + map.get("img1") + ".gif");
  85. article3.setUrl("");
  86. Article article4 = new Article();
  87. article4.setTitle(map.get("mydate2") + " "
  88. + map.get("myweek2") + " " + map.get("temp2")
  89. + " " + map.get("weather2") + " "
  90. + map.get("wind2") + " " + map.get("fl2"));
  91. article4.setDescription("");
  92. article4.setPicUrl("http://m.weather.com.cn/img/b"
  93. + map.get("img2") + ".gif");
  94. article4.setUrl("");
  95. Article article5 = new Article();
  96. article5.setTitle(map.get("mydate3") + " "
  97. + map.get("myweek3") + " " + map.get("temp3")
  98. + " " + map.get("weather3") + " "
  99. + map.get("wind3") + " " + map.get("fl3"));
  100. article5.setDescription("");
  101. article5.setPicUrl("http://m.weather.com.cn/img/b"
  102. + map.get("img3") + ".gif");
  103. article5.setUrl("");
  104. articleList.add(article1);
  105. articleList.add(article2);
  106. articleList.add(article3);
  107. articleList.add(article4);
  108. articleList.add(article5);
  109. newsMessage.setArticleCount(articleList.size());
  110. newsMessage.setArticles(articleList);
  111. respMessage = MessageUtil.newsMessageToXml(newsMessage);
  112. }
  113. return respMessage;
  114. }
  115. public static void main(String[] args) {
  116. org.jeecgframework.core.util.LogUtil.info(ClassLoader.getSystemResource(""));
  117. }
  118. }