E企盈营销工具技术服务商 热线:4006-838-530

微信公众号开发之生成并扫描带参数的二维码

E企盈直播平台营销卖货系统

生成带参数二维码详见微信公众平台之生成带参数的二维码具体步骤:可在微信测试平台https://mp.weixin.qq.com/debug进行生成生成结果如下:拿到ticket 请求 https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET(用获取到的ticket替换掉TICKET)直接生成二维码图片,如下图所示或者百度搜索二维码生成器,通过URL生成二维码,如图把二维码下载下来就可以了。用户扫描带场景值二维码时,可能推送以下两种事件:如果用户还未关注公众号,则用户可以关注公众号,关注后微信会将带场景值关注事件推送给开发者。如果用户已经关注公众号,则微信会将带场景值扫描事件推送给开发者。1. 用户未关注时,进行关注后的事件推送推送XML数据包示例:xmlToUserName ![CDATA[toUser] ]/ToUserNameFromUserName ![CDATA[FromUser] ]/FromUserNameCreateTime123456789/CreateTimeMsgType ![CDATA[event] ]/MsgTypeEvent ![CDATA[subscribe] ]/EventEventKey ![CDATA[qrscene_123123] ]/EventKeyTicket ![CDATA[TICKET] ]/Ticket/xml参数说明:参数描述ToUserName开发者微信号FromUserName发送方帐号(一个OpenID)CreateTime消息创建时间 (整型)MsgType消息类型,eventEvent事件类型,subscribeEventKey事件KEY值,qrscene_为前缀,后面为二维码的参数值Ticket二维码的ticket,可用来换取二维码图片2. 用户已关注时的事件推送推送XML数据包示例:xml ToUserName ![CDATA[toUser] ]/ToUserName FromUserName ![CDATA[FromUser] ]/FromUserName CreateTime123456789/CreateTime MsgType ![CDATA[event] ]/MsgType Event ![CDATA[SCAN] ]/Event EventKey ![CDATA[SCENE_VALUE] ]/EventKey Ticket ![CDATA[TICKET] ]/Ticket /xml参数说明:参数描述ToUserName开发者微信号FromUserName发送方帐号(一个OpenID)CreateTime消息创建时间 (整型)MsgType消息类型,eventEvent事件类型,SCANEventKey事件KEY值,是一个32位无符号整数,即创建二维码时的二维码scene_idTicket二维码的ticket,可用来换取二维码图片代码如下:@Controller@RequestMapping(“/wechat”)public class WxController {    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 {                MapString, 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();        }    }}

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » 微信公众号开发之生成并扫描带参数的二维码
分享到: 更多 (0)
E企盈小程序直播营销卖货系统
E企盈直播平台营销卖货系统

评论 抢沙发

E企盈小程序开发

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏