控制器源碼
1 <?php 2 namespace appcontrollers; 3 4 use yii; 5 use yiiwebController; 6 7 class PowerController extends Controller 8 { 9 public function actionLogin() 10 { 11 return $this->render('login'); 12 } 13 public function actionLogin_do() 14 { 15 $name = yii::$app->request->post('name'); 16 17 $password = yii::$app->request->post('password'); 18 $session = yii::$app->session; 19 $data = yii::$app->db->createCommand("select * from user where name='$name' and password = '$password'")->queryOne(); 20 if ($data){ 21 $session->set("uid",$data['id']); 22 $id = $data['id']; 23 $res = yii::$app->db->createCommand("select * from user JOIN u_r on `user`.id=u_r.u_id JOIN route on route.id=u_r.u_id JOIN r_p on u_r.r_id=r_p.r_id JOIN power on r_p.p_id=power.id where `user`.id='$id'")->queryAll(); 24 $session->set("power",json_encode($res)); 25 return 1; 26 }else{ 27 return 2; 28 } 29 } 30 }
1 <?php 2 namespace appcontrollers; 3 4 use yii; 5 use yiiwebController; 6 7 class RouteController extends Controller 8 { 9 //初始化頁面 10 public function init() 11 { 12 //開啟session 13 $session = yii::$app->session; 14 //獲取id 15 $id = $session->get('uid'); 16 //判斷用戶id 17 if (empty($id)){ 18 echo "<a href='/power/login'>請重新登錄</a>"; 19 } 20 $power = $session->get("power"); 21 $data = json_decode($power); 22 foreach ($data as $key=>$val){ 23 $arr[] = $val->controller.'/'.$val->action; 24 } 25 $nowroute = yii::$app->requestedRoute; 26 if (!in_array($nowroute,$arr)){ 27 echo "<a href='/power/login'>權限不夠,請重新登錄</a>"; 28 } 29 // echo "<pre>"; 30 // var_dump($arr);die; 31 } 32 //菜單欄目展示 33 public function actionList() 34 { 35 //開啟session 36 $session = yii::$app->session; 37 //獲取權限 38 $power = $session->get("power"); 39 $power = json_decode($power,1); 40 $data = $this->actionGettree($power,0); 41 return $this->render('list',['data'=>$data]); 42 } 43 //無限極分類權限菜單欄 44 public function actionGettree($data,$p_id) 45 { 46 $arr = []; 47 foreach ($data as $key=>$val){ 48 if ($val['p_id']==$p_id){ 49 $val['son'] = $this->actionGettree($data,$val['id']); 50 $arr[] = $val; 51 } 52 } 53 return $arr; 54 } 55 public function actionShowlist() 56 { 57 $data = yii::$app->db->createCommand("select * from message")->queryAll(); 58 return $this->render('showlist',['data'=>$data]); 59 } 60 public function actionAjaxsex() 61 { 62 $id = yii::$app->request->post('id'); 63 $sex = yii::$app->request->post('sex'); 64 if ($sex=="男"){ 65 $sexx="女"; 66 yii::$app->db->createCommand("update message set sex = 1 where id = '$id' ")->execute(); 67 echo json_encode($sexx); 68 }else if ($sex=="女"){ 69 $sexx="男"; 70 yii::$app->db->createCommand("update message set sex = 0 where id = '$id' ")->execute(); 71 echo json_encode($sexx); 72 } 73 } 74 }
視圖層源碼
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" 6 content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> 7 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 8 <title>登錄頁面</title> 9 </head> 10 <body> 11 <center> 12 <h1>登錄頁面</h1> 13 <table border="1"> 14 <tr> 15 <td>用戶名:</td> 16 <td><input type="text" name="name" id="name"></td> 17 </tr> 18 <tr> 19 <td>密碼:</td> 20 <td><input type="text" name="password" id="password"></td> 21 </tr> 22 <tr> 23 <td colspan="2" align="center"><input type="button" class="btn" value="登錄"></td> 24 </tr> 25 </table> 26 </center> 27 </body> 28 </html> 29 <script src="../js/jquery-3.3.1.min.js"></script> 30 <script> 31 $(document).on("click",".btn",function () { 32 var name = $("#name").val(); 33 var password = $("#password").val(); 34 $.ajax({ 35 url:"login_do", 36 type:"post", 37 dataType:"json", 38 data:{ 39 name:name, 40 password:password, 41 }, 42 success:function (data) { 43 console.log(data); 44 if (data==1){ 45 alert("登錄成功"); 46 location.href = "http://localhost/qianduan/yiirbac/basic/web/route/list"; 47 } else { 48 alert("登錄失敗") 49 } 50 } 51 }) 52 }) 53 </script>
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" 6 content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> 7 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 8 <title>Document</title> 9 </head> 10 <body> 11 <?php foreach ($data as $key=>$val){?> 12 <?php echo $val['power_name']?><br> 13 <?php foreach ($val['son'] as $k=>$v){?> 14 <a href="<?php echo $v['action']?>"><?php echo $v['power_name']?></a><br> 15 <?php }?> 16 <?php }?> 17 </body> 18 </html>
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" 6 content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> 7 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 8 <title>列表展示頁面</title> 9 </head> 10 <body> 11 <center> 12 <h1>列表展示頁面</h1> 13 </center> 14 <table class="table table-striped"> 15 <tr> 16 <td>編號</td> 17 <td>用戶名</td> 18 <td>郵箱</td> 19 <td>詳細地址</td> 20 <td>性別</td> 21 <td>操作</td> 22 </tr> 23 <?php foreach ($data as $key=>$val) {?> 24 <tr> 25 <td><?php echo $val['id']?></td> 26 <td><?php echo $val['username']?></td> 27 <td><?php echo $val['email']?></td> 28 <td><?php echo $val['address']?></td> 29 <td id="<?php echo $val['id']?>" class="se" dat-sex="<?php echo $val['sex']?>"><?php 30 if ($val['sex']==0){ 31 echo "男"; 32 }else if ($val['sex']==1){ 33 echo "女"; 34 } 35 ?> 36 </td> 37 <td><a href="#">刪除</a>|<a href="update?id=<?php echo $val['id']?>">編輯</a></td> 38 </tr> 39 <?php }?> 40 </table> 41 </body> 42 </html> 43 <script src="../js/jquery-3.3.1.min.js"></script> 44 <script> 45 $(document).on("click",".se",function () { 46 var id = $(this).attr('id'); 47 48 var sex = $(this).attr("dat-sex"); 49 var that = $(this); 50 var strsex = ""; 51 if (sex==0){ 52 strsex = "男"; 53 } else { 54 strsex = "女"; 55 } 56 $.ajax({ 57 url:"ajaxsex", 58 type:"post", 59 dataType:"json", 60 data:{ 61 id:id, 62 sex:strsex, 63 }, 64 success:function (data) { 65 console.log(data); 66 if (data=="男"){ 67 that.text("男"); 68 that.attr("dat-sex",0) 69 } else { 70 that.text("女"); 71 that.attr("dat-sex",1) 72 } 73 } 74 }) 75 }) 76 </script>