微信登录(第二种)

第二种登录,当用户扫描成功后,只有关注公众号,才能跳转到其他页面

第一步:公众号的配置

a  :   拿到AppID ,AppSecret 以及 配置  阿里云服务器的 内网IP 地址

开发--->基本配置

 b:服务器配置【说明:用户扫描成功后,微信给你服务器推送消息的 url 地址】

   

    第二步:生成场景图片【也就是二维码】

@RequestMapping(value = "/generateImages")
    @ResponseBody
    public WebResultInfo generateImages(HttpServletRequest request){
        HttpClientUtil tttpClientUtil = new HttpClientUtil();
            String accessToken = getAccessToken().getResults().get("accessToken").toString();
            logger.info("--------------Access_token=="+accessToken);
            JSONObject param = new JSONObject();
            param.put("action_name","QR_SCENE");
            JSONObject action_info = new JSONObject();
            JSONObject scene = new JSONObject();
            String uuid = WeChatUtil.getUUID_9();
            scene.put("scene_id",uuid);
            scene.put("scene_str",uuid);
            action_info.put("scene",scene);
            param.put("action_info",action_info);
            String qrcode_url = wechatUrl.qrcode_url.replace("{{ACCESS_TOKEN}}",accessToken);
            String result =  HttpClientUtil.httpClientPost(qrcode_url,param.toString());
            logger.info("------s1------");
            logger.info(result);
            JSONObject ticketJson =  JSONObject.fromObject(result);
            Object ticket =ticketJson.get("ticket");
            if(ticket!=null){
                /*String ticketStr = ticket.toString();
                String showqrcode = wechatUrl.showqrcode.replace("{{TICKET}}",ticketStr);
                WebResultInfo webResultInfo = WebResultInfo.success();
                webResultInfo.getResults().put("imgPath",showqrcode);
                //图片场景
                webResultInfo.getResults().put("uuid",uuid);
                return webResultInfo;*/
                String ticketStr = ticket.toString();
                String showqrcode = wechatUrl.showqrcode.replace("{{TICKET}}",ticketStr);
                try {
                    byte[] qrcode = tttpClientUtil.HttpClientGet(showqrcode);

                    File file = new File(wechatUrl.getZcsjwBaseUrl());
                    if(!file.exists()){
                        file.mkdirs();
                    }
                    String imgPath = wechatUrl.getZcsjwBaseUrl()+System.currentTimeMillis()+".jpg";
                    OutputStream os = new FileOutputStream(imgPath);
                    logger.info("------------ewm路径:---"+imgPath);
                    os.write(qrcode);
                    os.flush();
                    os.close();
                   
                    webResultInfo.getResults().put("imgPath",imgPath);
                    webResultInfo.getResults().put("uuid",uuid);
                    return webResultInfo;
                }catch(Exception e){
                    e.printStackTrace();
                }
        }
        return WebResultInfo.fail();
    }

第三步:web 端轮训 用户是否扫描【思路: 当用户扫描二维码,微信会给服务器推送消息报文,每一个二维码会对应一个独一无二的场景id【uuid】】

<script type="text/javascript">
    $(document).ready(function () {
        setInterval("wechatCheckLogin()", 2000);
    });

    function wechatCheckLogin(){
        $.ajax({
            url : "${ctx}/weixin/checkLogin.json",
            type : "POST",
            data : {
                "uuid":"${uuid}"
            },
            dataType : "json",
            success : function(data){
                if(data.status==200){
                    var newUUid = data.newUUid;
                    window.location.href="${ctx}/weixin/loginSuccess.json?uuid="+newUUid;
                }else{
                    console.info("轮训失败");
                }
            }
        });
    }
</script>

第四步: 返回扫描的用户信息

【思路: 当用户扫描成功后,推送报文过来,我们可以根据uuid 为主键,把用户的openid  存入到缓存中去,然后web 端轮训的时候,可以根据uuid 去拿缓存中的值。如果有值代表用户扫描成功。如果没有值,则反之。】

@RequestMapping(value = "/checkLogin.json")
    @ResponseBody
    public WebResultInfo checkLogin(@RequestBody ParamsData params){
        try{
            Map<String,Object> paramMap = params.getInmap();
            Object uuidObj = paramMap.get("uuid");
            if(uuidObj==null){
                return  WebResultInfo.fail(1002,"uuid is null");
            }
            String uuid = uuidObj.toString();
            FansInfo fansInfo = CacheManager.getData(uuid);
            if(fansInfo==null){
                //登录失败
                return  WebResultInfo.fail(1003,uuid+" no fansInfo");
            }else{
                logger.info(uuidObj+"检测到数据:"+fansInfo.getOpenid()+"---"+fansInfo.getNickname());
                CacheManager.clear(uuid);
                String newUUid = UUID.randomUUID().toString();
                WebResultInfo webResultInfo =  WebResultInfo.success();
                webResultInfo.getResults().put("newUUid",newUUid);
                webResultInfo.getResults().put("fansInfo",fansInfo);
                return  webResultInfo;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return WebResultInfo.fail();
    }

 

                    

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章