import com.thoughtworks.xstream.XStream; import com.bean.TextMessage; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import javax.servlet.http.HttpServletRequest; import java.io.InputStream; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; public class MessageUtils { public static final String MESSAGE_TEXT = "text"; public static final String MESSAGE_IMAGE = "image"; public static final String MESSAGE_VOICE = "voice"; public static final String MESSAGE_VIDEO = "video"; public static final String MESSAGE_LINK = "link"; public static final String MESSAGE_LOCATION = "location"; public static final String MESSAGE_EVENT = "event"; public static final String MESSAGE_SUBSCRIBE = "subscribe";//关注事件 public static final String MESSAGE_UNSUBSCRIBE = "unsubscribe"; public static final String MESSAGE_CLICK = "CLICK"; public static final String MESSAGE_VIEW = "VIEW"; /** * xml转map * */ public static Map<String, String> xmlToMap(HttpServletRequest request) throws Exception { Map<String, String> map = new HashMap<>(); SAXReader reader = new SAXReader(); InputStream ins = request.getInputStream(); Document doc = reader.read(ins); Element root = doc.getRootElement(); List<Element> list = root.elements(); for (Element element : list) { map.put(element.getName(), element.getText()); } ins.close(); return map; } /** * 文本转xml * */ public static String testMessageToXml(TextMessage textMessage) { XStream xStream = new XStream(); xStream.alias("xml", textMessage.getClass()); return xStream.toXML(textMessage); } /** * 拼接文本消息 * * @param toUserName * @param fromUserName * @param content * @return */ public static String initText(String toUserName, String fromUserName, String content) { TextMessage text = new TextMessage(); text.setFromUserName(toUserName); text.setToUserName(fromUserName); text.setMsgType("text"); text.setCreateTime(new Date().getTime() + ""); text.setContent(content); return MessageUtils.testMessageToXml(text); } }
MessageUtils
最新推荐文章于 2024-05-06 22:53:12 发布