php 根据省市区来划分区域权限

首先说说功能的需求:

一个单车泊位管理的项目,每一个用户组在添加或修改的时候都可以选择省市区。一个用户组下面有很多用户。

例如:

用户组 西湖区管理员 能看到的数据包括 :

首页

西湖区的所有车位信息

西湖区的所有车辆信息

总之:就是要管理某个省的人能看到这个省的数据,管理某个市的人可以看到这个市的数据,管理某个区的人就只能看到这个区的数据。

就是项目关于省市区权限的划分。

具体思路如下:

1.用户组表:dwz_auth_group 添加 省市区三个字段 ,即 province city area    然后在加上 行政级别字段 ,即 class

php 根据省市区来划分区域权限_第1张图片

2.在添加或者修改用户组界面,都加上省市区的选项。这里给推荐一个好用的省市区插件。

下载地址:http://download.csdn.net/download/ougexingfuba/10013305

下载插件,并放在项目相应的位置。

在添加或修改用户组界面引入插件,代码如下


    

                                地区:
                                
                                    

如下图:

php 根据省市区来划分区域权限_第2张图片


这样就能添加或者修改用户组的省市区和行政级别了。

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();
	}


视图代码如下:



    
    
    
        

车位设置    车位列表

添加车位

                   
ID 车位名称 可容纳车辆 已存储车辆 溢出车辆数量 车位编号 区域编号 经度 纬度 操作 Bulk Actions ( )
{$vo.id} {$vo.title} {$vo.usable_num} {$vo.storage_num} {$vo.overflow_num} {$vo.no} {$vo.block_no} {$vo.lng} {$vo.lat} {$vo.province} {$vo.city} {$vo.area} 修改 | 删除 | 实时停放车辆 | 历史停放车辆 | 实时数据
{$show}

4.以此类推,车辆的权限也可这样考虑。

你可能感兴趣的:(tp3.2,php)