首先说说功能的需求:
一个单车泊位管理的项目,每一个用户组在添加或修改的时候都可以选择省市区。一个用户组下面有很多用户。
例如:
用户组 西湖区管理员 能看到的数据包括 :
首页
西湖区的所有车位信息
西湖区的所有车辆信息
总之:就是要管理某个省的人能看到这个省的数据,管理某个市的人可以看到这个市的数据,管理某个区的人就只能看到这个区的数据。
就是项目关于省市区权限的划分。
具体思路如下:
1.用户组表:dwz_auth_group 添加 省市区三个字段 ,即 province city area 然后在加上 行政级别字段 ,即 class
2.在添加或者修改用户组界面,都加上省市区的选项。这里给推荐一个好用的省市区插件。
下载地址:http://download.csdn.net/download/ougexingfuba/10013305
下载插件,并放在项目相应的位置。
在添加或修改用户组界面引入插件,代码如下
<script src="https://cdn.bootcss.com/jquery/2.1.2/jquery.js"></script>
<script src="__PUBLIC__/JS/distpicker.js"></script>
<tr>
<th width="18%">地区:</th>
<td>
<div data-toggle="distpicker" data-autoselect="3" data-province="浙江省">
<select name="province" class="province1"></select>
<select name="city"></select>
<select name="area"></select>
</div>
</td>
</tr>
如下图:
这样就能添加或者修改用户组的省市区和行政级别了。
3.相应的车位也要有相应的省市区信息。(添加修改)
3.显示满足条件的车位数据,根据行政级判断是省或者市或者区。如果是省,车位要满足用户组的省市区和车位的省市区相同。如果是省市,要满足用户组的省市和车位的省市一样。如果是省,满足用户组的省和车位的省一样。
代码如下:
if($_SESSION['auth']['class']=='省级'){
$sql .= " and province = '$province' ";
$where["province"] = $_SESSION['auth']['province'];
}
if($_SESSION['auth']['class']=='市级'){
$sql .= " and province = '$province' ";
$sql .= " and city = '$city' ";
$where["province"] = $_SESSION['auth']['province'];
$where["city"] = $_SESSION['auth']['city'];
}
if($_SESSION['auth']['class']=='区级'){
$sql .= " and province = '$province' ";
$sql .= " and city = '$city' ";
$sql .= " and area = '$area' ";
$where["province"] = $_SESSION['auth']['province'];
$where["city"] = $_SESSION['auth']['city'];
$where["area"] = $_SESSION['auth']['area'];
}
整个方法代码如下:
public function index() {
//var_dump($_SESSION['auth']);
$uid = $_SESSION["auth"]["id"];
$this->assign("uid",$uid);
$title="";
$usable_num="";
$storage_num="";
$no="";
$nowtime=date('Y-m-d H:i:s');
$starttime=date('Y-m-d H:i:s',strtotime('-1 hour'));
$this->assign("nowtime2",$nowtime);
$this->assign("starttime2",$starttime);
$data = M('info');
$sql="SELECT * FROM dwz_info where 1=1 ";
if (!empty($_GET["title"]) || $_GET["title"]==='0') {
$title = $_REQUEST["title"];
$sql .= " and title like '%$title%' ";
$where["title"] = array('like', "%$title%");
$this->assign('title', $title);
}
if (!empty($_GET["usable_num"]) || $_GET["usable_num"]==='0') {
$usable_num = $_REQUEST["usable_num"];
$sql .= " and usable_num = '$usable_num' ";
$where["usable_num"] = '$usable_num';
$this->assign('usable_num', $usable_num);
}
if (!empty($_GET["storage_num"]) || $_GET["storage_num"]==='0') {
$storage_num = $_REQUEST["storage_num"];
$sql .= " and storage_num = '$storage_num' ";
$where["storage_num"] = '$storage_num';
$this->assign('storage_num', $storage_num);
}
if (!empty($_GET["no"]) || $_GET["no"]==='0') {
$no = $_REQUEST["no"];
$sql .= " and id = '$no' ";
$where["id"] = '$no';
$this->assign('no', $no);
}
//根据行政级别,做相应的过滤
$province = $_SESSION['auth']['province'];
$city = $_SESSION['auth']['city'];
$area = $_SESSION['auth']['area'];
if($_SESSION['auth']['class']=='省级'){
$sql .= " and province = '$province' ";
$where["province"] = $_SESSION['auth']['province'];
}
if($_SESSION['auth']['class']=='市级'){
$sql .= " and province = '$province' ";
$sql .= " and city = '$city' ";
$where["province"] = $_SESSION['auth']['province'];
$where["city"] = $_SESSION['auth']['city'];
}
if($_SESSION['auth']['class']=='区级'){
$sql .= " and province = '$province' ";
$sql .= " and city = '$city' ";
$sql .= " and area = '$area' ";
$where["province"] = $_SESSION['auth']['province'];
$where["city"] = $_SESSION['auth']['city'];
$where["area"] = $_SESSION['auth']['area'];
}
//var_dump($where);
$count=$data->where($where)->count();
//var_dump($count);exit;
$pageSize = 20;
$page = new \Component\Page($count, $pageSize); //这里的分页类和Home模块的目录一致,可自行修改
$sql.=$page->limit;
$info = $data -> query($sql);
$pagelist = $page -> fpage();
$this->assign('show', $pagelist);
$this->assign("bparking_list", $info);
$this->display();
}
视图代码如下: