微信开发笔记

带参数二维码的接口

https://developers.weixin.qq.com/doc/offiaccount/Account_Management/Generating_a_Parametric_QR_Code.html

可以通过识别扫码的用户生成对应图片

实现参考:https://blog.csdn.net/qq_23543983/article/details/80228558

下面为代码参考:

private final static String MEDIATYPE_CHARSET_JSON_UTF8 = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8";
    private static Logger log = LoggerFactory.getLogger(WxController.class);
 
    @RequestMapping(value = "/chat", method = {RequestMethod.GET, RequestMethod.POST}, produces = MEDIATYPE_CHARSET_JSON_UTF8)
    public void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //如果为get请求,则为开发者模式验证
        if ("get".equals(request.getMethod().toLowerCase())) {
            doGet();//在开发者模式验证中已处理,在此省略
        } else {
            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");
                request.getSession().setAttribute("openid",FromUserName);
                String CreateTime = map.get("CreateTime");
                String MsgType = map.get("MsgType");
                String message = null;
                if (MsgType.equals(WXConstants.MESSAGE_EVENT)) {
                    //从集合中,获取是哪一种事件传入
                    String eventType = map.get("Event");
                    //对获取到的参数进行处理
                    String eventKey = map.get("EventKey");
                    String[] params = eventKey.split("_");
                    String code = "";
                    if (params.length==2){
                        log.info("二维码参数为-----name:"+params[0]+",code:"+params[1]);
                        if (params[0].equalsIgnoreCase("bookshelf")){
                            code = params[1];
                            request.getSession().setAttribute(WXConstants.SCAN_NAME_SHELF,code);
                        }
                    }
                    //扫描带参数的二维码,如果用户未关注,则可关注公众号,事件类型为subscribe;用户已关注,则事件类型为SCAN
                    if (eventType.equals(WXConstants.MESSAGE_SUBSCRIBE)) {
                        //返回注册图文消息(在上一节关注并返回图文消息中已讲解)
                        message = MessageUtil.initNewsMessage(ToUserName, FromUserName);
                    } else if (eventType.equals(WXConstants.MESSAGE_SCAN)) {
                        //TODO 你自己的业务需求
                    }
                }
                out.print(message); //返回转换后的XML字符串
            } catch (DocumentException e) {
                e.printStackTrace();
            }
            out.close();
        }
    }

参考二:https://www.cnblogs.com/hetring/p/4054546.html

参考三:https://www.cnblogs.com/zhangchaoran/p/11010862.html  日期:2019-06-12 

原文地址:https://www.cnblogs.com/wu-xin/p/12060419.html