예제 #1
0
 /** 根据token计算signature验证是否为weixin服务端发送的消息 */
 private static boolean checkWeixinReques(HttpServletRequest request) {
   String signature = request.getParameter("signature");
   String timestamp = request.getParameter("timestamp");
   String nonce = request.getParameter("nonce");
   if (signature != null && timestamp != null && nonce != null) {
     String[] strSet = new String[] {TOKEN, timestamp, nonce};
     java.util.Arrays.sort(strSet);
     String key = "";
     for (String string : strSet) {
       key = key + string;
     }
     String pwd = WeixinUtil.sha1(key);
     return pwd.equals(signature);
   } else {
     return false;
   }
 }
예제 #2
0
  // 接收微信公众号接收的消息,处理后再做相应的回复
  @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();
      // 发送方帐号(open_id)
      String fromUserName = requestMap.get("FromUserName");

      if (type.equals(Message.TEXT)) { // 仅处理文本回复内容
        String content = message.getContent(); // 消息内容
        String[] cs = content.split("_"); // 消息内容都以下划线_分隔
        if (cs.length == 2) {
          int employeeid; // 员工编号
          String process = cs[1]; // 操作
          try {
            employeeid = Integer.parseInt(cs[0]);
            if ("出勤".equals(process)) {
              replyContent = weixinService.getSingleAttendStringByemployeeId(employeeid);
            } else if ("出勤历史".equals(process)) {
              replyContent = weixinService.getAttendHistoryStringByemployeeId(employeeid);
            } else if ("留言".equals(process)) {
              replyContent = weixinService.getSingleEmployeeMessageByemployeeId(employeeid);
            } else if ("留言历史".equals(process)) {
              replyContent = weixinService.getEmployeeMessageHistoryByEmployeeId(employeeid);
            } else if ("动态".equals(process)) {
              replyContent = weixinService.getSingleDeptNewsByemployeeId(employeeid);
            } else if ("动态历史".equals(process)) {
              replyContent = weixinService.getDeptNewsHistoryByemployeeId(employeeid);
            }
          } catch (NumberFormatException e) {
            replyContent = Reply.ERROR_CONTENT;
          }
        }
      } else if (type.equals(Message.SCAN)) {
        String eventKey = requestMap.get("EventKey");
        if (eventKey.equals("1")) {
          replyContent = "扫码签到被点击";
        }
      }
      // 拼装回复消息
      Reply reply = new Reply();
      reply.setToUserName(message.getFromUserName());
      reply.setFromUserName(message.getToUserName());
      DateUtils dateUtils = new DateUtils();
      Timestamp timestamp = dateUtils.converIntoNormal(new Date());
      reply.setCreateTime(timestamp);
      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";
    }
  }