微信公众号接收用户发送的特殊消息回复固定内容

1.验证用户消息

@RequestMapping(value = "/verifier",method = {RequestMethod.POST})
    public void processPost(HttpServletRequest request, HttpServletResponse response) throws Exception{
        PrintWriter out=response.getWriter();
        Map<String, String> msgMap = WechatUtils.xmlToMap(request);
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        /**接收消息*/
        if (StringUtils.equals("text",msgMap.get("MsgType")) && StringUtils.equals(StringUtils.trim(msgMap.get("Content")),"消息内容")){
            String xml = pushBindingInfo(msgMap);
            out.println(xml);
        }
    }

处理推送给用户的消息,转为xml,我这里推送的是一个链接,用户点击消息可以跳转到自定义的页面;

public String pushBindingInfo(Map<String, String> msgMap){
        TextMessage textMessage = new TextMessage();
        textMessage.setToUserName(msgMap.get("FromUserName"));
        textMessage.setFromUserName(msgMap.get("ToUserName"));
        textMessage.setCreateTime(msgMap.get("CreateTime"));
        textMessage.setMsgType("text");
        String appId = "APPID";
        String url = "链接";
        textMessage.setContent("<a href="+url+">推送的消息</a>");
        XStream xstream = new XStream();
        xstream.alias("xml", textMessage.getClass());
        String xml = xstream.toXML(textMessage);
        return xml;
    }

2.网页授权获取用户基本信息

微信公众号配置:

在这里插入图片描述在这里插入图片描述

/**
     *获取关注公众号用户的openId
     */
    public String getWxUserOpenId(HttpServletRequest request){
        String appId = sysParamService.getValue(Constants.WX_APPID);
        String appsecret = sysParamService.getValue(Constants.WX_APPSECRET);
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appId+"&secret="+appsecret+"&code=CODE&grant_type=authorization_code";
        String code = request.getParameter("code");
        if (StringUtils.isNotBlank(code)){
            String urlRe = StringUtils.replace(url, "CODE", code);
            String info = WechatUtils.get(urlRe);
            JSONObject jsonObject = JSONObject.fromObject(info);
            Object openid = jsonObject.get("openid");
            if (null != openid){
                String openId = openid.toString();
                return openId;
            }
        }
        return StringUtils.EMPTY;
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值