Maison >php教程 >php手册 >登陆前后导航栏处理 2015-12-12,导航栏2015-12-12

登陆前后导航栏处理 2015-12-12,导航栏2015-12-12

WBOY
WBOYoriginal
2016-06-13 08:49:37995parcourir

登陆前后导航栏处理 2015-12-12,导航栏2015-12-12

注意:添加灰色背景处,关于ajax传值,和js的判断

1   登陆后控制器处理:判断是否登陆并传递用户信息   

<span>class</span> IndexController <span>extends</span><span> HomeBaseController {
    </span><span>//</span><span>登录</span>
    <span>public</span> <span>function</span><span> index() {
        </span><span>$id</span>=I("get.id"<span>);      
        </span><span>$users_model</span>=M("Users"<span>);        
        </span><span>$user</span>=<span>$users_model</span>->where(<span>array</span>("id"=><span>$id</span>))-><span>find();      
        </span><span>if</span>(<span>empty</span>(<span>$user</span><span>)){
            </span><span>$this</span>->error("查无此人!"<span>);
        }
        </span><span>$this</span>->assign(<span>$user</span><span>);
        </span><span>$this</span>->display(":index"<span>);
    }
    </span><span>function</span><span> is_login(){
        </span><span>if</span><span>(sp_is_user_login()){
            </span><span><span>$this</span>->ajaxReturn(<span>array</span>("status"=>1,"user"=></span><span><span>sp_get_current_user()));</span>
        }</span><span>else</span><span>{
            </span><span>$this</span>->ajaxReturn(<span>array</span>("status"=>0,"info"=>"此用户未登录!"<span>));
        }
    }<br />。。。。。。</span>

2   登陆前后主页导航栏的 登陆和未登录 的显示

<span><</span><span>div </span><span>id</span><span>="main-menu-user"</span><span>></span>
      <span><!--</span><span>还未登陆</span><span>--></span>
            <span><</span><span>div  </span><span>class</span><span>='userout </span><span>user' </span><span>></span>
                <span><</span><span>ul </span><span>class</span><span>="nav navbar-nav  navbar-right"</span><span>></span>
                    <span><</span><span>li</span><span>></span>
                        <span><</span><span>a </span><span>href</span><span>="{:u('user/register/index')}"</span><span> data-toggle</span><span>="dropdown"</span><span>></span>注册<span></</span><span>a</span><span>></span>
                    <span></</span><span>li</span><span>></span>
                <span></</span><span>ul</span><span>></span>
                <span><</span><span>ul </span><span>class</span><span>="nav navbar-nav  navbar-right"</span><span>></span>
                    <span><</span><span>li</span><span>></span>
                        <span><</span><span>a </span><span>href</span><span>="{:u('user/login/index')}"</span><span>  data-toggle</span><span>="dropdown"</span><span>></span>登陆<span></</span><span>a</span><span>></span>
                    <span></</span><span>li</span><span>></span>
                <span></</span><span>ul</span><span>></span>
                <span><</span><span>ul </span><span>class</span><span>="nav navbar-nav  navbar-right"</span><span> style</span><span>="margin-top:10px"</span><span>></span>
                    <span><</span><span>li </span><span>></span>
                        <span><</span><span>img </span><span>src</span><span>="__TMPL__/Public/img/headicon.png"</span><span> class</span><span>="headicon"</span><span>/></span>
                    <span></</span><span>li</span><span>></span>
                <span></</span><span>ul</span><span>></span>
            <span></</span><span>div</span><span>></span>
    <span><!--</span><span>已经登陆</span><span>--></span>
            <span><</span><span>div </span><span>class</span><span>="userlogin user"</span> <span>></span>
                <span><</span><span>ul </span><span>class</span><span>="nav navbar-nav  navbar-right"</span><span>></span>
                    <span><</span><span>li</span><span>></span>
                        <span><</span><span>a </span><span>href</span><span>="{:u('user/index/logout')}"</span><span> data-toggle</span><span>="dropdown"</span><span>></span>退出<span></</span><span>a</span><span>></span>
                    <span></</span><span>li</span><span>></span>
                <span></</span><span>ul</span><span>></span>
                <span><</span><span>ul </span><span>class</span><span>="nav navbar-nav  navbar-right"</span><span>></span>
                    <span><</span><span>li</span><span>></span>
                 <span><</span><span>a </span><span>href</span><span>="{:u('user/center/index')}"</span><span>  data-toggle</span><span>="dropdown"</span><span> class</span><span>="user-nicename"</span><span>></</span><span>a</span><span>></span>
                    <span></</span><span>li</span><span>></span>
                <span></</span><span>ul</span><span>></span>
                <span><</span><span>ul </span><span>class</span><span>="nav navbar-nav  navbar-right"</span><span> style</span><span>="margin-top:10px"</span><span>></span>
                    <span><</span><span>li </span><span>></span>
                        <span><</span><span>img </span><span>src</span><span>=""</span><span> class</span><span>="headicon"</span><span>/></span>
                    <span></</span><span>li</span><span>></span>
                <span></</span><span>ul</span><span>></span>
            <span></</span><span>div</span><span>></span>
<span></</span><span>div</span><span>></span>

3   登陆后,js对box的操作

<script><br /><span> $(</span><span>function</span><span> () {</span><span>/*</span><span>控制导航栏显示登陆还是个人中心</span><span>*/</span><span>
  $.post(</span>"{:U('user/index/is_login')}",{},<span>function</span><span>(data){
      </span><span>if</span>(data.status==1<span>){
          </span><span>if</span><span>(data.user.avatar){
<span>              $(</span></span><span>"#main-menu-user ul li .headicong").attr("src",data.user.avatar.indexOf("http")==0?data.user.avatar:"__UPLOAD__avatar/"+<span>data.user.avatar);
            }
      $(</span>"#main-menu-user ul li .user-nicename").text(data.user.user_nicename!=""?</span><span><span>data.user.user_nicename:data.user.user_login);</span>
      $(</span>"#main-menu-user .userlogin"<span>).show();
      $(</span>"#main-menu-user .userout"<span>).hide();               
      }
      </span><span>if</span>(data.status==0<span>){
         $(</span>"#main-menu-user .userout"<span>).show();
         $(</span>"#main-menu-user .userlogin"<span>).hide();
      }
});  });
</span></script>
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn