NutzCN Logo
问答 nutzWk 微信开发
发布于 593天前 作者 1037424761 512 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

我想做个拦截器,当用户点击某个菜单时校验他的基本信息,当他没有完善信息时,在微信上发个链接,让他点击进入完善信息界面!

/**
	 * 用一个wxHandler处理对应的用户请求
	 */
	public static View handle(WxHandler wxHandler, HttpServletRequest req, String key) throws IOException {
		if (wxHandler == null) {
			log.info("WxHandler is NULL");
			return HttpStatusView.HTTP_502;
		}
		String signature = req.getParameter("signature");
		String timestamp = req.getParameter("timestamp");
		String nonce = req.getParameter("nonce");
		String msg_signature = req.getParameter("msg_signature");
		String encrypt_type = req.getParameter("encrypt_type");
		if (!wxHandler.check(signature, timestamp, nonce, key)) {
			log.info("token is invalid");
			return HttpStatusView.HTTP_502;
		}
		if ("GET".equalsIgnoreCase(req.getMethod())) {
		    String echostr = req.getParameter("echostr");
			log.info("GET? return echostr=" + echostr);
			return new ViewWrapper(new RawView(null), echostr);
		}
		String postData = Streams.readAndClose(new InputStreamReader(req.getInputStream(), Encoding.CHARSET_UTF8));

		if ("aes".equals(encrypt_type)) {
            WXBizMsgCrypt msgCrypt = wxHandler.getMsgCrypt();
		    try {
		        // 若抛出Illegal key size,请更新JDK的加密库为不限制长度
                postData = msgCrypt.decryptMsg(msg_signature, timestamp, nonce, postData);
            }
            catch (AesException e) {
                return new HttpStatusView(403);
            }
		}
		WxInMsg in = Wxs.convert(postData);
		in.setExtkey(key);
		WxOutMsg out = wxHandler.handle(in);
		if (out != null) {
			Wxs.fix(in, out);
		}
		return new ViewWrapper(WxView.me, out);
	}

参考了这个类,写了

public class UserFilter implements ActionFilter {
	private String path;
	
	private CloudBindService cloudBindService= Mvcs.ctx().getDefaultIoc().get(CloudBindService.class);
	
	private WxHandler wxHandler= Mvcs.ctx().getDefaultIoc().get(WxHandler.class);
	
	public UserFilter(String path) {
		this.path = path;
	}
	/**
	 * return MyBeetlViewMarker.make(path);返回Beetl渲染頁面
	 */
	@Override
	public View match(ActionContext context) {
		String openid = Strings.sNull(context.getRequest().getSession().getAttribute("openid"));
		NutMap nutMap = cloudBindService.searchById(openid);
        if (nutMap.get("data")==null) {
        	WxInMsg in = new WxInMsg();
    		in.setContent("1");
    		in.setMsgType("text");
    		WxOutMsg out = wxHandler.handle(in);
    		if (out != null) {
    			Wxs.fix(in, out);
    		}
    		return new ViewWrapper(WxView.me, out);
        }
        JSONObject json = JSONObject.parseObject(nutMap.get("data").toString());
        context.getRequest().getSession().setAttribute("unitid",json.getString("unitid"));
		return null;
	}

}

怎么返回的是xml格式的报文

5 回复

在wxHandler里面判断, ActionFilter 是不行的

我已开始也是这么做的

@At({"/api", "/api/?"})
    @Fail("http:200")
    public View msgIn(String key, HttpServletRequest req) throws IOException {
        return Wxs.handle(wxHandler, req, key);
    }

进入

public WxOutMsg text(WxInMsg msg) {
       /* Wx_reply reply = wxReplyService.fetch(Cnd.where("wxid", "=", msg.getExtkey()).and("type", "=", "keyword").and("keyword", "=", msg.getContent()));*/
    	Wx_reply reply = wxReplyService.fetch(Cnd.where("type", "=", "keyword").and("keyword", "=", msg.getContent()));
        if (reply != null) {
            if ("txt".equals(reply.getMsgType())) {
                String txtId = reply.getContent();
                Wx_reply_txt txt = wxReplyTxtService.fetch(txtId);
                return Wxs.respText(null, txt == null ? "" : txt.getContent());
            } else if ("news".equals(reply.getMsgType())) {
                String[] newsIds = Strings.sBlank(reply.getContent()).split(",");
                List<WxArticle> list = new ArrayList<>();
                List<Wx_reply_news> newsList = wxReplyNewsService.query(Cnd.where("id", "in", newsIds).asc("location"));
                for (Wx_reply_news news : newsList) {
                    WxArticle wxArticle = new WxArticle();
                    wxArticle.setDescription(news.getDescription());
                    wxArticle.setPicUrl(news.getPicUrl());
                    wxArticle.setTitle(news.getTitle());
                    wxArticle.setUrl(news.getUrl());
                    list.add(wxArticle);
                }
                return Wxs.respNews(null, list);
            }
        }
        Wx_user usr = wxUserService.fetch(Cnd.where("openid", "=", msg.getFromUserName()));
        Wx_msg wxMsg = new Wx_msg();
        wxMsg.setOpenid(msg.getFromUserName());
        wxMsg.setContent(EmojiParser.parseToAliases(msg.getContent(), EmojiParser.FitzpatrickAction.REMOVE));
        wxMsg.setWxid(msg.getExtkey());
        wxMsg.setType("txt");
        wxMsg.setNickname(usr == null ? "匿名" : usr.getNickname());
        wxMsg.setDelFlag(false);
        wxMsgService.insert(wxMsg);
        return Wxs.respText(null, "您的留言已收到!");
    }

但是我发现,text里面我加了断点,但是,这个方法没走完,页面就刷出来了?
但是

 @At({"/gotoOrder"})
    @Fail("http:200")
    //@Filters({@By(type = UserFilter.class,args={"/platform/yd/wx/business_info.html"})})
    @Ok("beetl:/platform/yd/wx/order.html")
    public void gotoOrder(HttpServletRequest req) throws IOException {
    	
    }

在这个方法入口加断点,就可以先走完text再走gotoOrder,不知道为什么?

微信5秒超时

https://github.com/Wizzercn/NutzWk/blob/modular/wk-app/wk-web/src/main/java/cn/wizzer/app/web/modules/controllers/front/wx/WechatController.java

通过这个类获取openid,然后用openid查询业务表,若没有业务数据则跳转到指定页面:

    @At("/user")
    @Ok("re")
    public Object user(HttpServletRequest req, HttpSession session) {
        String wxid = Strings.sNull(session.getAttribute("wxid"));
        String openid = Strings.sNull(session.getAttribute("openid"));
        Wx_user user = wxUserService.fetch(Cnd.where("wxid", "=", wxid).and("openid", "=", openid));
        if (user == null || user.isBinded() == null || !user.isBinded() || Strings.isBlank(user.getMobile())) {
            return "redirect:/public/wx/yxb/bind/user";
        }
        req.setAttribute("date", DateUtil.format(new Date(), "yyyy年MM月dd日"));
        req.setAttribute("time", DateUtil.format(new Date(), "HH:mm"));
        req.setAttribute("obj", user);
        return "beetl:/public/wx/yxb/user.html";
    }
添加回复
请先登陆
回到顶部