nutz servlet添加 并 获取service 博客分类: nutzweichat nutzservlet添加获取service
程序员文章站
2024-03-17 08:44:22
...
web.xml
<servlet> <servlet-name>weichatServlet</servlet-name> <servlet-class>cn.hy.weixin.controller.WeichatServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>weichatServlet</servlet-name> <url-pattern>/weichat.do</url-pattern> </servlet-mapping>
servlet
package cn.hy.weixin.controller; import java.io.IOException; import java.io.PrintWriter; import java.util.Map; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.nutz.ioc.loader.annotation.Inject; import org.nutz.mvc.Mvcs; import cn.hy.activity.data.Activity; import cn.hy.activity.service.ActivityService; import cn.hy.weixin.entity.AccessToken; import cn.hy.weixin.util.AccessTokenUtil; import cn.hy.weixin.util.CheckUtil; import cn.hy.weixin.util.MessageUtil; import net.sf.json.JSONObject; public class WeichatServlet extends HttpServlet{ private static final long serialVersionUID = 3264201758083219979L; public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{ String signature = request.getParameter("signature"); String timestamp = request.getParameter("timestamp"); String nonce = request.getParameter("nonce"); String echostr = request.getParameter("echostr"); System.out.println("signature="+signature); System.out.println("timestamp="+timestamp); System.out.println("nonce"+nonce); PrintWriter out = response.getWriter(); if(CheckUtil.checkSignature(signature, timestamp, nonce)){ out.print(echostr); } } public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException{ request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); try { Map<String,String> map = MessageUtil.xmlToMap(request); String toUserName = map.get("ToUserName"); String fromUserName = map.get("FromUserName"); String msgType = map.get("MsgType"); String content = map.get("Content"); for(String str : map.keySet()) { System.out.println("key..." + str + " " + map.get(str)); } String message = null; if(MessageUtil.MESSAGE_EVENT.equals(msgType)){ String eventType = map.get("Event"); System.out.println("msgType" +msgType); System.out.println("scene_id = " + map.get("EventKey"));//获取二维码自定义参数 if(MessageUtil.MESSAGE_EVENT_SUBSCRIBE.equals(eventType)){//关注事件 //创建自定义菜单 AccessToken token = AccessTokenUtil.getAccessToken(); String menu = JSONObject.fromObject(MessageUtil.initMenu()).toString(); int result = MessageUtil.createMenu(token.getToken(), menu); System.out.println("-------------------------------------"); System.out.println(token.getToken()); System.out.println(menu +"\r\n"+menu.getBytes().length); System.out.println("--crate menu--"+result +"--------------"); System.out.println("-------------------------------------"); int activityId =0; String sceneId = map.get("EventKey"); if(sceneId != null && sceneId !=""){ try{ activityId =Integer.parseInt(sceneId.split("_")[1]); }catch(Exception e){ activityId =0; } } if(activityId == 0 ){ message = MessageUtil.initText(toUserName, fromUserName, MessageUtil.initMenuMsaage()); }else{ ActivityService activityService = (ActivityService) Mvcs.ctx().getDefaultIoc().get(ActivityService.class); System.out.println("activityService"+activityService); Activity activity = activityService.getActivityDetail(activityId); StringBuffer sb =new StringBuffer(); sb.append("名称: "+ activity.getName()+"\n\n"); sb.append("活动代码: "+ activity.getCode()+"\n\n"); message = MessageUtil.initText(toUserName, fromUserName, MessageUtil.initMenuMsaage(sb)); } } if(MessageUtil.MESSAGE_EVENT_UNSUBSCRIBE.equals(eventType)){//取消关注 //todo } if(MessageUtil.MESSAGE_EVENT_VIEW.equals(eventType)) { System.out.println("here...." + eventType); } } else if(MessageUtil.MESSAGE_TEXT.equals(msgType)){ if(content.equals("1")){ System.out.println("toUserName------------------------" +toUserName); message = MessageUtil.initText(toUserName, fromUserName, MessageUtil.fristKey()); System.out.println("message------------------------" +message); }else if(content.equals("2")){ System.out.println("toUserName------------------------" +toUserName); message = MessageUtil.initText(toUserName, fromUserName, MessageUtil.secKey()); }else if(content.equals("3")){ System.out.println("toUserName------------------------" +toUserName); message = MessageUtil.initNews(toUserName, fromUserName); }else if(content.equals("?") || content.equals("?")){ message = MessageUtil.initText(toUserName, fromUserName, MessageUtil.initMenuMsaage()); } } out.print(message); } catch (Exception e) { e.printStackTrace(); }finally{ out.close(); } } }
service 获取
ActivityService activityService = (ActivityService) Mvcs.ctx().getDefaultIoc().get(ActivityService.class);
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(支持支付宝和微信 以及扣扣群),没钱捧个人场,谢谢各位。
个人主页:http://knight-black-bob.iteye.com/
谢谢您的赞助,我会做的更好!
上一篇: sort