注意:添加灰色背景處,關於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" > <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" > <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>