控制器源碼

 <?php
namespace app\controllers; use yii;
use yii\web\Controller; class PowerController extends Controller
{
public function actionLogin()
{
return $this->render('login');
}
public function actionLogin_do()
{
$name = yii::$app->request->post('name'); $password = yii::$app->request->post('password');
$session = yii::$app->session;
$data = yii::$app->db->createCommand("select * from user where name='$name' and password = '$password'")->queryOne();
if ($data){
$session->set("uid",$data['id']);
$id = $data['id'];
$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();
$session->set("power",json_encode($res));
return 1;
}else{
return 2;
}
}
}
 <?php
namespace app\controllers; use yii;
use yii\web\Controller; class RouteController extends Controller
{
//初始化頁面
public function init()
{
//開啟session
$session = yii::$app->session;
//獲取id
$id = $session->get('uid');
//判斷用戶id
if (empty($id)){
echo "<a href='/power/login'>請重新登錄</a>";
}
$power = $session->get("power");
$data = json_decode($power);
foreach ($data as $key=>$val){
$arr[] = $val->controller.'/'.$val->action;
}
$nowroute = yii::$app->requestedRoute;
if (!in_array($nowroute,$arr)){
echo "<a href='/power/login'>權限不夠,請重新登錄</a>";
}
// echo "<pre>";
// var_dump($arr);die;
}
//菜單欄目展示
public function actionList()
{
//開啟session
$session = yii::$app->session;
//獲取權限
$power = $session->get("power");
$power = json_decode($power,1);
$data = $this->actionGettree($power,0);
return $this->render('list',['data'=>$data]);
}
//無限極分類權限菜單欄
public function actionGettree($data,$p_id)
{
$arr = [];
foreach ($data as $key=>$val){
if ($val['p_id']==$p_id){
$val['son'] = $this->actionGettree($data,$val['id']);
$arr[] = $val;
}
}
return $arr;
}
public function actionShowlist()
{
$data = yii::$app->db->createCommand("select * from message")->queryAll();
return $this->render('showlist',['data'=>$data]);
}
public function actionAjaxsex()
{
$id = yii::$app->request->post('id');
$sex = yii::$app->request->post('sex');
if ($sex=="男"){
$sexx="女";
yii::$app->db->createCommand("update message set sex = 1 where id = '$id' ")->execute();
echo json_encode($sexx);
}else if ($sex=="女"){
$sexx="男";
yii::$app->db->createCommand("update message set sex = 0 where id = '$id' ")->execute();
echo json_encode($sexx);
}
}
}

視圖層源碼

 <!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>登錄頁面</title>
</head>
<body>
<center>
<h1>登錄頁面</h1>
<table border="1">
<tr>
<td>用戶名:</td>
<td><input type="text" name="name" id="name"></td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="text" name="password" id="password"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="button" class="btn" value="登錄"></td>
</tr>
</table>
</center>
</body>
</html>
<script src="../js/jquery-3.3.1.min.js"></script>
<script>
$(document).on("click",".btn",function () {
var name = $("#name").val();
var password = $("#password").val();
$.ajax({
url:"login_do",
type:"post",
dataType:"json",
data:{
name:name,
password:password,
},
success:function (data) {
console.log(data);
if (data==1){
alert("登錄成功");
location.href = "http://localhost/qianduan/yiirbac/basic/web/route/list";
} else {
alert("登錄失敗")
}
}
})
})
</script>
 <!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<?php foreach ($data as $key=>$val){?>
<?php echo $val['power_name']?><br>
<?php foreach ($val['son'] as $k=>$v){?>
<a href="<?php echo $v['action']?>"><?php echo $v['power_name']?></a><br>
<?php }?>
<?php }?>
</body>
</html>
 <!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>列表展示頁面</title>
</head>
<body>
<center>
<h1>列表展示頁面</h1>
</center>
<table class="table table-striped">
<tr>
<td>編號</td>
<td>用戶名</td>
<td>郵箱</td>
<td>詳細地址</td>
<td>性別</td>
<td>操作</td>
</tr>
<?php foreach ($data as $key=>$val) {?>
<tr>
<td><?php echo $val['id']?></td>
<td><?php echo $val['username']?></td>
<td><?php echo $val['email']?></td>
<td><?php echo $val['address']?></td>
<td id="<?php echo $val['id']?>" class="se" dat-sex="<?php echo $val['sex']?>"><?php
if ($val['sex']==0){
echo "男";
}else if ($val['sex']==1){
echo "女";
}
?>
</td>
<td><a href="#">刪除</a>|<a href="update?id=<?php echo $val['id']?>">編輯</a></td>
</tr>
<?php }?>
</table>
</body>
</html>
<script src="../js/jquery-3.3.1.min.js"></script>
<script>
$(document).on("click",".se",function () {
var id = $(this).attr('id'); var sex = $(this).attr("dat-sex");
var that = $(this);
var strsex = "";
if (sex==0){
strsex = "男";
} else {
strsex = "女";
}
$.ajax({
url:"ajaxsex",
type:"post",
dataType:"json",
data:{
id:id,
sex:strsex,
},
success:function (data) {
console.log(data);
if (data=="男"){
that.text("男");
that.attr("dat-sex",0)
} else {
that.text("女");
that.attr("dat-sex",1)
}
}
})
})
</script>
05-11 13:18