微信登錄(第二種)

第二種登錄,當用戶掃描成功後,只有關注公衆號,才能跳轉到其他頁面

第一步:公衆號的配置

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();
    }

 

                    

 

 

 

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