当前位置:   article > 正文

生成微信二维码注意事项_qrscene

qrscene

1.   scene_id必须为整数



1.   scene_id必须为整数

2.   微信推送事件

用户未关注时,进行关注后的事件推送

<xml><ToUserName><![CDATA[toUser]]></ToUserName>        //开发者微信号

<FromUserName><![CDATA[FromUser]]></FromUserName>       //发送者账号(openid)

<CreateTime>123456789</CreateTime>                //消息创建时间(整型)

<MsgType><![CDATA[event]]></MsgType>              //消息类型 event

<Event><![CDATA[subscribe]]></Event>              //事件类型(subscribe)

<EventKey><![CDATA[qrscene_123 ]]></EventKey>        //事件KEY值,qrscene_为前缀,后面为二维码参数值

<Ticket><![CDATA[TICKET]]></Ticket>               //二维码ticke值,可以用来换取二维码图片

</xml>

 红色背景的值和上面红线中scene_id的值一样

3接收微信推送事件并解析xml字符串

@RequestMapping(value = "/",method = RequestMethod.POST)
    public String getMessage(HttpServletRequest request,HttpServletResponse response,Model m){
        FileOutputStream fileOutputStream=null;

        BufferedReader buffer=null;
         System.out.println("log................");
        try {


              ///var/ww/tmp/1.txt
            fileOutputStream=new FileOutputStream(new File("d:/2.txt"));
            buffer=request.getReader();

            String line;
            String result=null;
            while ((line = buffer.readLine())!= null) {
                result += line;
            }
            System.out.println(result);


            Document doc=DocumentHelper.parseText(result.substring(4));
            Element rescode = doc.getRootElement();
            String ToUserName = rescode.elementText("ToUserName");
            String FromUserName = rescode.elementText("FromUserName");
            String MsgType = rescode.elementText("MsgType");
            String Event = rescode.elementText("Event");
            String EventKey = rescode.elementText("EventKey");
            String Ticket = rescode.elementText("Ticket");

             System.out.println("Event"+Event);
            String str="log......";
            str=str+result+ToUserName+FromUserName+MsgType+Event+"//evnetkey>>"+EventKey;
//            if(("SCAN").equals(Event)) {
              fileOutputStream.write(str.getBytes());
//            }
//            System.out.println("result="+result.length());
            System.out.println("log................1"+ToUserName+FromUserName);

            if (null != EventKey&&!EventKey.equals("")) {
                //System.out.println(EventKey.substring(0, 4));
                if (EventKey.substring(0,8).equals("qrscene_")) {
                    Integer uid = Integer.parseInt(EventKey.substring(8));
                    Users user = usersService.selectByPrimaryKey(uid);
                    Association association = new Association();
                    association.setHostid(user.getOpenid());
                    association.setSubid(FromUserName);
                    if (null==associationService.selectsubId(FromUserName)) {
                         associationService.insert(association);

                            //m.addAttribute("hostId",user.getOpenid());

                        }
                    //return "redirect:showAssociation/"+user.getOpenid();

                }
            }
        }catch(Exception e){
            e.printStackTrace();
        logger.error(e);
        }finally {
            try {
                if (buffer != null) {
                    buffer.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            }catch (Exception e){
                logger.error(e);
            }
        }

     return null;
    }

4.在jsp页面显示二维码

<img src="data:image/jpg;base64,${img}" class="erma" alt="二维码">

 

5工具类代码

public class WeixinUtil {
    public static String httpPostRequest( String token,String scene_id)
    { String result="";
        try {

        String Post_URL = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+token;
        URL url =new URL(Post_URL);
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        Map map1=new HashMap();
        map1.put("scene_id",scene_id);
        Map map=new HashMap();
        map.put("scene",map1);

        JSONObject jo=new JSONObject();
        jo.put("expire_seconds", 3600);
        jo.put("action_name", "QR_SCENE");

        jo.put("action_info", map);

//POST请求
        DataOutputStream out = new DataOutputStream(
                connection.getOutputStream());
        out.writeBytes(jo.toJSONString());
        BufferedReader in = new BufferedReader(
                new InputStreamReader(connection.getInputStream()));

        String line;
        while ((line = in.readLine()) != null) {
            result += line;
        }
         System.out.println(result);

        out.flush();
        out.close();
        } catch (Exception e) {

            e.printStackTrace();
        }
        return result;
    }
    public static String  getImageQRCode(String ticket){
        String requestUrl="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+ticket;
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();

        try{
            URL url = new URL(requestUrl);
            HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();


            httpUrlConn.setDoOutput(true);
            httpUrlConn.setDoInput(true);
            httpUrlConn.setUseCaches(false);
            // 设置请求方式(GET/POST)
            httpUrlConn.setRequestMethod("GET");
            httpUrlConn.connect();

            // 将返回的输入流转换成字符串
            InputStream inputStream = httpUrlConn.getInputStream();
            BufferedInputStream bis=new BufferedInputStream(inputStream);


            byte[] buffer = new byte[1024*4];
            int len = 0;
            while( (len=bis.read(buffer)) != -1 ){
                outStream.write(buffer, 0, len);
            }
            bis.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpUrlConn.disconnect();
        }catch(Exception e){
            e.printStackTrace();
        }
        return encode(outStream.toByteArray());
    }
public static byte[] readInputStream(InputStream inStream) throws Exception{
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    //创建一个Buffer字符串
    byte[] buffer = new byte[1024];
    //每次读取的字符串长度,如果为-1,代表全部读取完毕
    int len = 0;
    //使用一个输入流从buffer里把数据读取出来
    while( (len=inStream.read(buffer)) != -1 ){
        //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
        outStream.write(buffer, 0, len);
    }
    //关闭输入流
    inStream.close();
    //outStream里的数据写入内存
    return outStream.toByteArray();
}
public static String encode(final byte[] bytes) {
    return new String(Base64.encodeBase64(bytes));
}


.在jsp页面显示二维码

<img src="data:image/jpg;base64,${img}" class="erma" alt="二维码">

 


 [X1]和上面红线中scene_id的值一样

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/280655
推荐阅读
相关标签
  

闽ICP备14008679号