韩震博客|关注于学习java&php与交流

手机实现微信的免登陆

309882580 2017年12月25日 Java知识 734 0

其实微信的免登陆和登录时写的代码差不多,只是实现微信登录免登陆是根据用户名进行判断数据库中是否存在,我这里的用的例子是手机号唯一验证是否可以免登陆,我这里举个例子:我的是实现买家微信的免登陆,首先判断其手机号是否是本省的手机号,这里用的重定向,如果不是跳到到未登录前的首页

y

image.png

实现买家免登陆的逻辑:举例说明数据库中存在实现方式

 String proCode = "091";//初始化可以自定义
String citCode = "999";//初始化可以自定义
List<TWlmBuyerDTO> buyerList = tWlmUserService.queryBuyerList(limit," and loginCode = '"+tel+"'");
StringBuffer sbf=new StringBuffer();
TWlmShopDTO shop=null;
if(shopid!=null && !shopid.equals("") && !shopid.equals("null")){
    shop = tWlmShopService.get(shopid);
}
if(buyerList != null && buyerList.size() > 0){
if(buyerList.get(0).getProCode() != null && buyerList.get(0).getProCode().equals("")){
proCode=buyerList.get(0).getProCode();
}
if(buyerList.get(0).getCitCode() != null && buyerList.get(0).getCitCode() ==""){
citCode=buyerList.get(0).getCitCode();
}
buyerList.get(0).setCitCode(citCode);
buyerList.get(0).setProCode(proCode);
request.getSession().setAttribute(Consts.SESSION_USER, buyerList.get(0));
request.getSession().removeAttribute(Consts.SESSION_USER_INFO);
//返回cookie
Cookie cookie = new Cookie("noLoginCookie","buyerLoginCookie");
cookie.setMaxAge(3*24*60*60);
cookie.setPath("/");
response.addCookie(cookie);
if(shopid==null||shopid=="" || shopid.equals("null")){
    sbf.append("and indexcode='wlm_shop_relation'");
                    StringBuffer sbf1=new StringBuffer();
                    String citycode="";
                    String shopIds="";
                    for (TWlmBuyerDTO tWlmBuyerDTO : buyerList) {
                        citycode=tWlmBuyerDTO.getAreaCode();
                    }
                    List<TDictionaryindexDTO> indexdto=tDictionaryindexService.list(limit, sbf.toString());
                    for (TDictionaryindexDTO tDictionaryindexDTO : indexdto) {
                        sbf1.append("and indexcode='"+tDictionaryindexDTO.getId()+"' and code='"+citycode+"'");
                        List<TDictionaryDTO> dto=tDictionaryService.list(limit, sbf1.toString());
                        for (TDictionaryDTO tDictionaryDTO : dto) {
                            shopIds=tDictionaryDTO.getValue();
                        }
                    }
                    log.info("shopIds:"+shopIds);
                    if(shopIds==null ||"".equals(shopIds.trim())){
                    
                    for (TWlmBuyerDTO tWlmBuyerDTO : buyerList) {
                        citycode=tWlmBuyerDTO.getAreaCode();
                    }
                    List<TDictionaryindexDTO> indexdto_w=tDictionaryindexService.list(limit, sbf.toString());
                    for (TDictionaryindexDTO tDictionaryindexDTO : indexdto_w) {
                        sbf1.append("and indexcode='"+tDictionaryindexDTO.getId()+"' and code='999'");
                        List<TDictionaryDTO> dto=tDictionaryService.list(limit, sbf1.toString());
                        for (TDictionaryDTO tDictionaryDTO : dto) {
                            shopIds=tDictionaryDTO.getValue();
                        }
                    }
                    
                    }
                    shop = tWlmShopService.get(shopIds);
                    
}
shop = isShopNull(shop);//判断店铺是否冻结、是否空,如果是走省店铺
if(shop != null){
shopid = shop.getShopId();
log.info("免登陆买家shopid"+shopid);
}
                request.getSession().setAttribute("session_shopinfo", shop);
request.getSession().setAttribute(Consts.session_shop, shop);
request.getSession().setAttribute("user_Type","1");
Date  loginTime = new Date();
addUserLog(request, response, buyerList.get(0).getUserId(), "登录成功!", "3", "0", "1",loginTime,buyerList.get(0).getLoginCode());
//h5买家表里有登录成功
toSetRedis(buyerList.get(0).getUserNickName(),"1","","",buyerList.get(0).getUserId(),tel,request.getSession().getId(),"","","",response,request);
response.sendRedirect(jumpUrlLogin+shopid);
//}
}

这里用到了往redis中存储,每个公司实现可能有点区别,不过大同小异 toSetRedis()这个方法是往缓存里放的,里面的参数定义根据实际需要进行定义

public void toSetRedis(String userName,String type,String appId,String openId,String subject,String mobile,String jti,String orgId,String shopId,String cityId,HttpServletResponse response,HttpServletRequest request){
    //Map<String, String> map = new HashMap<String, String>();
    
    /*String signString = "un="+userName+"&ty="+type+"&ai="+appId+"&oi="+openId+"&sub="+subject+"&mob="+mobile+"&jti="+
    jti+"&org="+orgId+"&sp="+shopId+"&ci"+cityId;*/
    String str = "{\"un\":\""+userName+"\",\"ty\":\""+type+"\",\"ai\":\""+appId+"\","
     + "\"oi\":\""+openId+"\",\"sub\":\""+subject+"\",\"mob\":\""+mobile+"\","
     + "\"jti\":\""+jti+"\",\"org\":\""+orgId+"\",\"sp\":\""+shopId+"\",\"ci\":\""+cityId+"\"}";  
    
    String uuid = UUID.randomUUID().toString();// 用来生成唯一字符串
    //用户登录信息放入redis
    jedisHelper.setWithOutTime(uuid, str, 24*60*60);
    log.info("存入redis的用户信息------"+jedisHelper.get(uuid));
    //用户信息放入cookie
    Cookie cookie = new Cookie("lnwd_uid",uuid);
    cookie.setPath("/");
        response.addCookie(cookie);
        
    }
}


如果表中没有此号码数据就记录数据到数据库中,再将信息存入缓存中。举例说明:

String pNumSub = this.tWlmUserService.getAreaCode(tel.substring(0,7));
TWlmBuyerDTO newBuyer = new TWlmBuyerDTO();
newBuyer.setUserNickName("L"+tel);
newBuyer.setUserPhone(tel);
newBuyer.setUserAddress("");
newBuyer.setUserIdNum("");
newBuyer.setUserHeadUrl("");
newBuyer.setUserRealName("");
newBuyer.setLoginCode(tel);
newBuyer.setProCode(proCode);
newBuyer.setCitCode(citCode);
newBuyer.setAreaCode(pNumSub);
newBuyer.setOpenId(openid);
tWlmUserService.addBuyerTX(newBuyer);
List<TWlmBuyerDTO> bList = tWlmUserService.queryBuyerList(limit," and loginCode = '"+tel+"'");
request.getSession().setAttribute(Consts.SESSION_USER, newBuyer);
sbf.append("and indexcode='wlm_shop_relation'");
StringBuffer sbf1=new StringBuffer();
                String shopIds="";
                List<TDictionaryindexDTO> indexdto=tDictionaryindexService.list(limit, sbf.toString());
                for (TDictionaryindexDTO tDictionaryindexDTO : indexdto) {
                    sbf1.append("and indexcode='"+tDictionaryindexDTO.getId()+"' and code='"+pNumSub+"'");
                    List<TDictionaryDTO> dto=tDictionaryService.list(limit, sbf1.toString());
                    for (TDictionaryDTO tDictionaryDTO : dto) {
                        shopIds=tDictionaryDTO.getValue();
                    }
                }
                shop = tWlmShopService.get(shopIds);
                
                shop = isShopNull(shop);//判断店铺是否冻结、是否空,如果是走省店铺
                if(shop != null){
shopid = shop.getShopId();
log.info("免登陆买家shopid"+shopid);
}
//返回cookie
                Cookie cookie = new Cookie("noLoginCookie","buyerLoginCookie");
cookie.setPath("/");
cookie.setMaxAge(3*24*60*60);
response.addCookie(cookie);
request.getSession().setAttribute("session_shopinfo", shop);
request.getSession().setAttribute(Consts.session_shop, shop);
Date loginTime = new  Date();
addUserLog(request, response, bList.get(0).getUserId(), "登录成功!", "3", "0", "1",loginTime,bList.get(0).getLoginCode());
//h5买家表没有登录成功
toSetRedis(newBuyer.getUserNickName(),"1","","",bList.get(0).getUserId(),tel,request.getSession().getId(),"","","",response,request);
response.sendRedirect(jumpUrlLogin+shopid);
}















转载的时候记得写明出处哦!红色火线加油站!http://hongsehuoxian.com
看到这里如果这篇文章真的帮到你了,不妨打赏一下哦!

以上就是红色火线JAVA加油站整理的关于手机实现微信的免登陆的文章,
希望可以帮到你,如果你有不同的见解可以留言指正哦,避免让别的java小伙伴在走挫折路。
济南it公司黑名单
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

分享:

支付宝

微信