public static BaseMessage processRequest(String msg, ClassService classService) { String respMessage = null; BaseMessage returnMsg = null; try { // 默认返回的文本消息内容 String respContent = "请求处理异常,请稍候尝试!"; // xml请求解析 Map<String, Object> requestMap = MessageUtil.parseXml(msg); System.out.println("Event==" + requestMap.get("Event")); // 发送方帐号(open_id) String fromUserName = requestMap.get("FromUserName").toString(); // 公众帐号 String toUserName = requestMap.get("ToUserName").toString(); // String toUserName = "******"; // 消息类型 String msgType = requestMap.get("MsgType").toString(); // 回复文本消息 /* TextMessage textMessage = new TextMessage(); textMessage.setToUserName(fromUserName); textMessage.setFromUserName(toUserName); textMessage.setCreateTime(new Date().getTime()); textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT); textMessage.setFuncFlag(0); */ // 文本消息 if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) { String content = requestMap.get("Content").toString(); returnMsg = new TextMessage(); returnMsg.setToUserName(toUserName); returnMsg.setFromUserName(fromUserName); returnMsg.setCreateTime(new java.util.Date().getTime()); returnMsg.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT); returnMsg.setFuncFlag(0); ((TextMessage) returnMsg).setContent(content); /** * NewsMessage newsMessage = new NewsMessage(); newsMessage.setToUserName(fromUserName); * newsMessage.setFromUserName(toUserName); newsMessage.setCreateTime(new Date().getTime()); * newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS); newsMessage.setFuncFlag(0); * * <p>List<Article> articleList = new ArrayList<Article>(); Article article = new Article(); * article.setTitle("微信公众帐号开发教程Java版"); article.setTitle("微信公众帐号开发教程Java版"); * article.setDescription("柳峰,80后,微信公众帐号开发经验4个月。为帮助初学者入门,特推出此系列教程,也希望借此机会认识更多同行!"); * article.setPicUrl("http://0.xiaoqrobot.duapp.com/images/avatar_liufeng.jpg"); * article.setUrl("http://blog.csdn.net/lyq8479"); articleList.add(article); // 设置图文消息个数 * newsMessage.setArticleCount(articleList.size()); // 设置图文消息包含的图文集合 * newsMessage.setArticles(articleList); // 将图文消息对象转换成xml字符串 respMessage = * MessageUtil.newsMessageToXml(newsMessage); returnMsg = newsMessage; 8 */ } // 图片消息 else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) { } // 地理位置消息 else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) { // respContent = "Sunlight提示:您发送的是地理位置消息!"; } // 链接消息 else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)) { // respContent = "Sunlight提示:您发送的是链接消息!"; } // 音频消息 else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)) { // respContent = "Sunlight提示:您发送的是音频消息!"; } // 事件推送 else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) { // 事件类型 String eventType = requestMap.get("Event").toString(); // 自定义菜单点击事件 if (eventType.equalsIgnoreCase(MessageUtil.EVENT_TYPE_CLICK)) { // 事件KEY值,与创建自定义菜单时指定的KEY值对应 String eventKey = requestMap.get("EventKey").toString(); System.out.println("EventKey=" + eventKey); UserInfo userif = getUserInfoFromUserID(fromUserName); StringBuffer qrcode_content = new StringBuffer(); if (eventKey.equals("11")) { qrcode_content.append("@英派斯"); } else if (eventKey.equals("12")) { qrcode_content.append("@会员提供场地"); } else if (eventKey.equals("13")) { qrcode_content.append("@宝力豪"); } else if (eventKey.equals("14")) { qrcode_content.append("@和静工作室"); } qrcode_content.append("@" + userif.getName()); qrcode_content.append("@" + fromUserName); // Date currentTime = new Date(System.currentTimeMillis()); qrcode_content.append("@" + System.currentTimeMillis()); // Generate UUID signature UUID uuid = UUID.randomUUID(); String uuidStr = uuid.toString(); qrcode_content.append("@" + uuidStr.replaceAll("-", "")); UploadMediaReturnMessage uploadMediaReturnMsg = null; try { String imgDir = "/root/WXMedia/" + fromUserName; com.wx.util.FileUtils.imgFileDirectoryInitialization(imgDir); QRCodeGenerator.encode(qrcode_content.toString(), "/root/WXMedia/" + fromUserName); uploadMediaReturnMsg = WXUtils.send("image", "/root/WXMedia/" + fromUserName + "/qrcode.jpg"); if ("null".equals(uploadMediaReturnMsg.getErrcode())) { System.out.println("Media_id:" + uploadMediaReturnMsg.getMedia_id()); } else { System.out.println( "Upload error message:" + uploadMediaReturnMsg.getErrmsg() + " error code:" + uploadMediaReturnMsg.getErrcode()); } } catch (Exception ex) { Logger.getLogger(CustomerWXServlet.class.getName()).log(Level.SEVERE, null, ex); } ImageSendMessage imgMsg = new ImageSendMessage(); imgMsg.setImageMedia(uploadMediaReturnMsg.getMedia_id()); imgMsg.setMsgtype("image"); imgMsg.setTouser(fromUserName); // imgMsg.setTouser("anranhui520"); imgMsg.setAgentid((long) 2); sendMessage(imgMsg); TextSendMessage txtMsg = new TextSendMessage(); txtMsg.setMsgtype("text"); txtMsg.setTextContent("如果要删除签课记录,同一签课表扫描两次."); txtMsg.setAgentid((long) 2); txtMsg.setTouser(fromUserName); sendMessage(txtMsg); // respContent = "Sunlight提示:您点击的菜单KEY是"+eventKey; } else if (eventType.equalsIgnoreCase(MessageUtil.EVENT_TYPE_SCAN_CODE)) { String eventKey = requestMap.get("EventKey").toString(); System.out.println("EventKey=" + eventKey); if (eventKey.equals("31")) { Map<String, String> scanCodeInfo = (Map<String, String>) requestMap.get("ScanCodeInfo"); String scanResult = scanCodeInfo.get("ScanResult"); System.out.println("ScanResult:" + scanResult); } } else if (eventType.equalsIgnoreCase(MessageUtil.EVENT_TYPE_SCAN_CODE_WAIT_MSG)) { String eventKey = requestMap.get("EventKey").toString(); System.out.println("EventKey=" + eventKey); if (eventKey.equals("21")) { Map<String, String> scanCodeInfo = (Map<String, String>) requestMap.get("ScanCodeInfo"); String scanResult = scanCodeInfo.get("ScanResult"); System.out.println("ScanResult:" + scanResult); // process scan result String[] results = scanResult.split("@"); System.out.println("place:" + results[1]); System.out.println("coacher:" + results[2]); System.out.println("coacher weixin:" + results[3]); System.out.println("time:" + results[4]); System.out.println("signature:" + results[5]); RegisterClass classInfo = classService.findRegisterClassBySignature(results[5]); StringBuffer replyResult = new StringBuffer(); StringBuffer coacherReplyResult = new StringBuffer(); Date dateTime = new Date(Long.parseLong(results[4].toString())); UserInfo userif = getUserInfoFromUserID(fromUserName); if (classInfo == null) { boolean registerResult = classService.registerClass( results[2], fromUserName, dateTime, results[1], results[5]); if (registerResult) { int allRegisterClass = classService.getCustomerAllRegisterClass(fromUserName); int salesRecord = classService.getSalesRecordNumByCustomer(fromUserName); int remainRecord = salesRecord - allRegisterClass; replyResult.append("恭喜又完成了一次训练!\n"); int weekCount = classService.counterRegisterClassByNameRecentWeek(fromUserName, dateTime); replyResult.append("截至这次,本周共完成训练 " + weekCount + "次。\n"); int monthCount = classService.counterRegisterClassByNameRecentMonth(fromUserName, dateTime); replyResult.append("本月共完成训练 " + monthCount + "次。\n"); replyResult.append("剩余使用次数 " + remainRecord + "次。\n"); // int yearCount = classService.counterRegisterClassByNameRecentYear(fromUserName, // dateTime); // replyResult.append("今年总共完成训练 " + yearCount + "次。"); coacherReplyResult.append(userif.getName() + "截至这次,本周共完成训练 " + weekCount + "次。\n"); coacherReplyResult.append("本月共完成训练 " + monthCount + "次。\n"); coacherReplyResult.append("剩余使用次数 " + remainRecord + "次。\n"); } } else { System.out.println("删除签课表。"); classService.deleteRegisterClassBySignature(results[5]); replyResult.append( "成功删除签课记录:" + "场地:" + results[1] + " 教练:" + results[2] + " 时间:" + dateTime); coacherReplyResult.append( "成功删除签课记录:" + "场地:" + results[1] + " 教练:" + results[2] + " 时间:" + dateTime); } System.out.println("scanResult:" + scanCodeInfo.get("ScanResult")); TextSendMessage txtMsg = new TextSendMessage(); txtMsg.setMsgtype("text"); txtMsg.setTextContent(replyResult.toString()); txtMsg.setAgentid((long) 1); txtMsg.setTouser(fromUserName); sendMessage(txtMsg); TextSendMessage txtMsg1 = new TextSendMessage(); txtMsg1.setMsgtype("text"); txtMsg1.setTextContent(coacherReplyResult.toString()); txtMsg1.setAgentid((long) 2); txtMsg1.setTouser(results[3]); sendMessage(txtMsg1); } } } // textMessage.setContent(respContent); // respMessage = MessageUtil.textMessageToXml(textMessage); } catch (Exception e) { e.printStackTrace(); System.out.println(e); respMessage = "有异常了。。。"; } return returnMsg; }