Search in sources :

Example 1 with Message

use of com.aixuexiao.model.Message in project aixuexiao by zhiyuncloud.

the class WeixinUtil method mapToMessage.

/**
	 * 存储数据的Map转换为对应的Message对象
	 * @param map 存储数据的map
	 * @return 返回对应Message对象
	 */
public static Message mapToMessage(Map<String, String> map) {
    if (map == null)
        return null;
    String msgType = map.get("MsgType");
    Message message = new Message();
    message.setToUserName(map.get("ToUserName"));
    message.setFromUserName(map.get("FromUserName"));
    message.setCreateTime(new Date(Long.parseLong(map.get("CreateTime"))));
    message.setMsgType(msgType);
    message.setMsgId(map.get("MsgId"));
    if (msgType.equals(Message.TEXT)) {
        message.setContent(map.get("Content"));
    } else if (msgType.equals(Message.IMAGE)) {
        message.setPicUrl(map.get("PicUrl"));
    } else if (msgType.equals(Message.LINK)) {
        message.setTitle(map.get("Title"));
        message.setDescription(map.get("Description"));
        message.setUrl(map.get("Url"));
    } else if (msgType.equals(Message.LOCATION)) {
        message.setLocationX(map.get("Location_X"));
        message.setLocationY(map.get("Location_Y"));
        message.setScale(map.get("Scale"));
        message.setLabel(map.get("Label"));
    } else if (msgType.equals(Message.EVENT)) {
        message.setEvent(map.get("Event"));
        message.setEventKey(map.get("EventKey"));
    }
    return message;
}
Also used : Message(com.aixuexiao.model.Message) Date(java.util.Date)

Example 2 with Message

use of com.aixuexiao.model.Message in project aixuexiao by zhiyuncloud.

the class WeixinController method listMessage.

/**
	 * 收到消息列表页面
	 */
@RequestMapping(value = "/manager/messages", method = RequestMethod.GET)
public ModelAndView listMessage(String pagenum) {
    ModelAndView mv = new ModelAndView();
    mv.addObject("sidebar", "messages");
    mv.setViewName("messages");
    int num = 1;
    if (null != pagenum) {
        num = Integer.parseInt(pagenum);
    }
    List<Message> list = weixinService.listMessage((num - 1) * pagesize, pagesize);
    mv.addObject("messageList", list);
    mv.addObject("pagenum", num);
    mv.addObject("length", list.size());
    return mv;
}
Also used : Message(com.aixuexiao.model.Message) ModelAndView(org.springframework.web.servlet.ModelAndView) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with Message

use of com.aixuexiao.model.Message in project aixuexiao by zhiyuncloud.

the class WeixinController method replyMessage.

//接收微信公众号接收的消息,处理后再做相应的回复
@RequestMapping(value = "/weixin", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
@ResponseBody
public String replyMessage(HttpServletRequest request) {
    //仅处理微信服务端发的请求
    if (checkWeixinReques(request)) {
        Map<String, String> requestMap = WeixinUtil.parseXml(request);
        Message message = WeixinUtil.mapToMessage(requestMap);
        //保存接受消息到数据库
        weixinService.addMessage(message);
        String replyContent = Reply.WELCOME_CONTENT;
        String type = message.getMsgType();
        if (type.equals(Message.TEXT)) {
            //仅处理文本回复内容
            //消息内容
            String content = message.getContent();
            //消息内容都以下划线_分隔
            String[] cs = content.split("_");
            if (cs.length == 2) {
                //学生编号
                int studentid;
                //操作
                String process = cs[1];
                try {
                    studentid = Integer.parseInt(cs[0]);
                    if ("考试".equals(process)) {
                        replyContent = weixinService.getSingleExamMarkStringByStudentId(studentid);
                    } else if ("考试历史".equals(process)) {
                        replyContent = weixinService.getExamMarkHistoryStringByStudentId(studentid);
                    } else if ("留言".equals(process)) {
                        replyContent = weixinService.getSingleStudentMessageByStudentId(studentid);
                    } else if ("留言历史".equals(process)) {
                        replyContent = weixinService.getStudentMessageHistoryByStudentId(studentid);
                    } else if ("动态".equals(process)) {
                        replyContent = weixinService.getSingleClassesNewsByStudentId(studentid);
                    } else if ("动态历史".equals(process)) {
                        replyContent = weixinService.getClassesNewsHistoryByStudentId(studentid);
                    }
                } catch (NumberFormatException e) {
                    replyContent = Reply.ERROR_CONTENT;
                }
            }
        }
        //拼装回复消息
        Reply reply = new Reply();
        reply.setToUserName(message.getFromUserName());
        reply.setFromUserName(message.getToUserName());
        reply.setCreateTime(new Date());
        reply.setMsgType(Reply.TEXT);
        reply.setContent(replyContent);
        //保存回复消息到数据库
        weixinService.addReply(reply);
        //将回复消息序列化为xml形式
        String back = WeixinUtil.replyToXml(reply);
        System.out.println(back);
        return back;
    } else {
        return "error";
    }
}
Also used : Message(com.aixuexiao.model.Message) Reply(com.aixuexiao.model.Reply) Date(java.util.Date) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

Message (com.aixuexiao.model.Message)3 Date (java.util.Date)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Reply (com.aixuexiao.model.Reply)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1