Maison >développement back-end >tutoriel php >登陆前前后后导航栏处理 2015-12-12
登陆前后导航栏处理 2015-12-12
注意:添加灰色背景处,关于ajax传值,和js的判断
1 登陆后控制器处理:判断是否登陆并传递用户信息
<span style="color: #0000ff;">class</span> IndexController <span style="color: #0000ff;">extends</span><span style="color: #000000;"> HomeBaseController { </span><span style="color: #008000;">//</span><span style="color: #008000;">登录</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> index() { </span><span style="color: #800080;">$id</span>=I("get.id"<span style="color: #000000;">); </span><span style="color: #800080;">$users_model</span>=M("Users"<span style="color: #000000;">); </span><span style="color: #800080;">$user</span>=<span style="color: #800080;">$users_model</span>->where(<span style="color: #0000ff;">array</span>("id"=><span style="color: #800080;">$id</span>))-><span style="color: #000000;">find(); </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$user</span><span style="color: #000000;">)){ </span><span style="color: #800080;">$this</span>->error("查无此人!"<span style="color: #000000;">); } </span><span style="color: #800080;">$this</span>->assign(<span style="color: #800080;">$user</span><span style="color: #000000;">); </span><span style="color: #800080;">$this</span>->display(":index"<span style="color: #000000;">); } </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> is_login(){ </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(sp_is_user_login()){ </span><span style="background-color: #c0c0c0;"><span style="color: #800080;">$this</span>->ajaxReturn(<span style="color: #0000ff;">array</span>("status"=>1,"user"=></span><span style="color: #000000;"><span style="background-color: #c0c0c0;">sp_get_current_user()));</span> }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$this</span>->ajaxReturn(<span style="color: #0000ff;">array</span>("status"=>0,"info"=>"此用户未登录!"<span style="color: #000000;">)); } }<br>。。。。。。</span>
2 登陆前后主页导航栏的 登陆和未登录 的显示
<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="main-menu-user"</span><span style="color: #0000ff;">></span> <span style="color: #008000;"><!--</span><span style="color: #008000;">还未登陆</span><span style="color: #008000;">--></span> <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">='userout </span><span style="color: #ff0000;">user' </span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">ul </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="nav navbar-nav navbar-right"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">a </span><span style="color: #ff0000;">href</span><span style="color: #0000ff;">="{:u('user/register/index')}"</span><span style="color: #ff0000;"> data-toggle</span><span style="color: #0000ff;">="dropdown"</span><span style="color: #0000ff;">></span>注册<span style="color: #0000ff;"></span><span style="color: #800000;">a</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">ul</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">ul </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="nav navbar-nav navbar-right"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">a </span><span style="color: #ff0000;">href</span><span style="color: #0000ff;">="{:u('user/login/index')}"</span><span style="color: #ff0000;"> data-toggle</span><span style="color: #0000ff;">="dropdown"</span><span style="color: #0000ff;">></span>登陆<span style="color: #0000ff;"></span><span style="color: #800000;">a</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">ul</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">ul </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="nav navbar-nav navbar-right"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="margin-top:10px"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">li </span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">="__TMPL__/Public/img/headicon.png"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="headicon"</span><span style="color: #0000ff;">/></span> <span style="color: #0000ff;"></span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">ul</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #008000;"><!--</span><span style="color: #008000;">已经登陆</span><span style="color: #008000;">--></span> <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="userlogin user"</span> <span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">ul </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="nav navbar-nav navbar-right"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">a </span><span style="color: #ff0000;">href</span><span style="color: #0000ff;">="{:u('user/index/logout')}"</span><span style="color: #ff0000;"> data-toggle</span><span style="color: #0000ff;">="dropdown"</span><span style="color: #0000ff;">></span>退出<span style="color: #0000ff;"></span><span style="color: #800000;">a</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">ul</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">ul </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="nav navbar-nav navbar-right"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">a </span><span style="color: #ff0000;">href</span><span style="color: #0000ff;">="{:u('user/center/index')}"</span><span style="color: #ff0000;"> data-toggle</span><span style="color: #0000ff;">="dropdown"</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="user-nicename"</span><span style="color: #0000ff;">></span><span style="color: #800000;">a</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">ul</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">ul </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="nav navbar-nav navbar-right"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="margin-top:10px"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">li </span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">img </span><span style="color: #ff0000;">src</span><span style="color: #0000ff;">=""</span><span style="color: #ff0000;"> class</span><span style="color: #0000ff;">="headicon"</span><span style="color: #0000ff;">/></span> <span style="color: #0000ff;"></span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">ul</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
3 登陆后,js对box的操作
<script><br /><span style="color: #000000;"> $(<span style="color: #0000ff;">function<span style="color: #000000;"> () {<span style="color: #008000;">/*<span style="color: #008000;">控制导航栏显示登陆还是个人中心<span style="color: #008000;">*/<span style="color: #000000;"> $.post("{:U('user/index/is_login')}",{},<span style="color: #0000ff;">function<span style="color: #000000;">(data){ <span style="color: #0000ff;">if(data.status==1<span style="color: #000000;">){ <span style="color: #0000ff;">if<span style="color: #000000;">(data.user.avatar){<span style="background-color: #c0c0c0;"> $(<span style="background-color: #c0c0c0;">"#main-menu-user ul li .headicong").attr("src",data.user.avatar.indexOf("http")==0?data.user.avatar:"__UPLOAD__avatar/"+<span style="color: #000000;">data.user.avatar); } $("#main-menu-user ul li .user-nicename").text(data.user.user_nicename!=""?<span style="color: #000000;"><span style="background-color: #c0c0c0;">data.user.user_nicename:data.user.user_login); $("#main-menu-user .userlogin"<span style="color: #000000;">).show(); $("#main-menu-user .userout"<span style="color: #000000;">).hide(); } <span style="color: #0000ff;">if(data.status==0<span style="color: #000000;">){ $("#main-menu-user .userout"<span style="color: #000000;">).show(); $("#main-menu-user .userlogin"<span style="color: #000000;">).hide(); }}); });</script>