1. 程式人生 > >微信公眾號獲取帶引數的二維碼

微信公眾號獲取帶引數的二維碼

    public void publicQR(
            HttpServletResponse response,HttpServletRequest request,@RequestBody String dataid) {
        ResultMap map = new ResultMap();
        try {
            JSONObject jsondata = JSONObject.fromObject(dataid);
            String udid = jsondata.getString("引數");
            if(MyUtil.isEmpty(“”引數“”)) {
                map.setState(ResultMap.ERROR);
                map.setMsg("引數不能為空");
                return map;
            }
            String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret;
            String resultStr = HttpHelper.httpGet(url,null);
            System.out.println("resultStr"+resultStr);
            JSONObject json = JSONObject.fromObject(resultStr);
            String token = json.getString("access_token");
            System.out.println("access_token"+token);
            String qrUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+token;
            Map<String,String> idMap = new HashMap<String,String>();
                idMap.put("scene_str", "引數");
            Map<String,Object> scenMap = new HashMap<String,Object>();
                scenMap.put("scene", idMap);
            Map<String,Object> param = new HashMap<String,Object>();
            String expire_seconds ="10000";
            String action_name = "QR_STR_SCENE";
                param.put("expire_seconds", expire_seconds);
                param.put("action_name", action_name);
                param.put("action_info", scenMap);
            String data = new Gson().toJson(param);
            String qrResult = HttpHelper.httpPost(qrUrl, data, null);
            System.out.println("qrResult"+qrResult);
            JSONObject qrJson = JSONObject.fromObject(qrResult);
            String qrCode = qrJson.getString("url");
            String ticket = qrJson.getString("ticket");
            System.out.println("ticket"+ticket);
            String qrsUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+ticket; 

           response.getWriter().print(qrsUrl );
        } catch (Exception e) {

        }

    }