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

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

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

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

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

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

3   登陆后,js对box的操作

<script>
$(function () {/*控制导航栏显示登陆还是个人中心*/ $.post("{:U('user/index/is_login')}",{},function(data){ if(data.status==1){ if(data.user.avatar){ $("#main-menu-user ul li .headicong").attr("src",data.user.avatar.indexOf("http")==0?data.user.avatar:"__UPLOAD__avatar/"+data.user.avatar); } $("#main-menu-user ul li .user-nicename").text(data.user.user_nicename!=""?data.user.user_nicename:data.user.user_login); $("#main-menu-user .userlogin").show(); $("#main-menu-user .userout").hide(); } if(data.status==0){ $("#main-menu-user .userout").show(); $("#main-menu-user .userlogin").hide(); } }); }); </script>
posted @ 2015-12-12 17:32  镜子-正衣冠-知得失  阅读(697)  评论(0编辑  收藏  举报