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

来源:互联网 发布:cf烟雾头软件 编辑:程序博客网 时间:2024/05/17 09:10

首先说说功能的需求:

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

例如:

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

首页

西湖区的所有车位信息

西湖区的所有车辆信息

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

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

具体思路如下:

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


视图代码如下:

<extend name="Public:left" /><block name="main">    <script src="https://cdn.bootcss.com/jquery/2.1.2/jquery.js"></script>    <script src="__PUBLIC__/JS/distpicker.js"></script>    <!-- page content -->        <div class="">            <div class="page-title">                <div class="title_left">                    <h3>车位设置    <small>车位列表</small></h3>                </div>                <div class="title_right">                    <div class="col-md-5 col-sm-5 col-xs-12 form-group pull-right top_search">                        <div class="input-group">                            <input type="text" class="form-control" placeholder="Search for...">                    <span class="input-group-btn">                      <button class="btn btn-default" type="button">Go!</button>                    </span>                        </div>                    </div>                </div>            </div>            <div class="clearfix"></div>            <div class="row">                </div>                <div class="clearfix"></div>                <div class="col-md-12 col-sm-12 col-xs-12">                    <div class="x_panel">                        <div class="x_title">                            <ul class="nav navbar-right panel_toolbox">                                <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>                                </li>                                <li class="dropdown">                                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>                                    <ul class="dropdown-menu" role="menu">                                        <li><a href="#">Settings 1</a>                                        </li>                                        <li><a href="#">Settings 2</a>                                        </li>                                    </ul>                                </li>                                <li><a class="close-link"><i class="fa fa-close"></i></a>                                </li>                            </ul>                            <div class="clearfix"></div>                        </div>                        <div class="x_content">                            <p>                                <a class="btn btn-primary" href="javascript:;" onclick="add()">添加车位</a>                            </p>                            <div style="margin-bottom: 10px" class="input-group">                                <form method="get" action="{:U('Admin/Bparking/index')}">                                    <input type="text" class="form-control" style="width: 150px" placeholder="车位名称" name="title" value="{$title}" id="title"/>                                        <input type="text" class="form-control" style="width: 150px" placeholder="可容纳车辆" name="usable_num" value="{$usable_num}" id="usable_num"/>                                        <input type="text" class="form-control" style="width: 150px" placeholder="已存储车辆" name="storage_num" value="{$storage_num}" id="storage_num"/>                                        <input type="text" class="form-control" style="width: 150px" placeholder="车位编号" name="no" value="{$no}" id="no"/>                                        <input type="submit" value="查询" class="btn btn-default"/>                                </form>                            </div>                            <div class="table-responsive">                                <table class="table table-striped jambo_table bulk_action">                                    <thead>                                    <tr class="headings">                                        <th>                                        </th>                                        <th class="column-title">ID </th>                                        <th class="column-title">车位名称 </th>                                        <th class="column-title">可容纳车辆 </th>                                        <th class="column-title">已存储车辆 </th>                                        <th class="column-title">溢出车辆数量 </th>                                        <th class="column-title">车位编号 </th>                                        <th class="column-title">区域编号 </th>                                        <th class="column-title">经度 </th>                                        <th class="column-title">纬度 </th>                                        <th class="column-title">省 </th>                                        <th class="column-title">市 </th>                                        <th class="column-title">区 </th>                                        <th class="column-title no-link last"><span class="nobr">操作</span>                                        </th>                                        <th class="bulk-actions" colspan="7">                                            <a class="antoo" style="color:#fff; font-weight:500;">Bulk Actions ( <span class="action-cnt"> </span> ) <i class="fa fa-chevron-down"></i></a>                                        </th>                                    </tr>                                    </thead>                                    <tbody>                                    <volist name="bparking_list" id="vo">                                        <tr class="even pointer">                                            <td class="a-center ">                                            </td>                                            <td class=" ">{$vo.id}</td>                                            <td class=" ">{$vo.title}</td>                                            <td class=" ">{$vo.usable_num} </td>                                            <td class=" ">{$vo.storage_num} </td>                                            <td class=" ">{$vo.overflow_num} </td>                                            <td class=" ">{$vo.no} </td>                                            <td class="a-right a-right ">{$vo.block_no}</td>                                            <td class="a-right a-right ">{$vo.lng}</td>                                            <td class="a-right a-right ">{$vo.lat}</td>                                            <td class="a-right a-right ">{$vo.province}</td>                                            <td class="a-right a-right ">{$vo.city}</td>                                            <td class="a-right a-right ">{$vo.area}</td>                                            <td class=" last">                                                <a href="#">                                                   <a href="javascript:;" navId="{$vo['id']}" navTitle="{$vo['title']}" navUsable_num="{$vo['usable_num']}" navStorage_num="{$vo['storage_num']}" navOverflow_num="{$vo['overflow_num']}" navNo="{$vo['no']}" navBlock_no="{$vo['block_no']}" navLng_Lat="{$vo['lng']},{$vo['lat']}" onclick="edit(this)">修改</a>                                                    | <a href="javascript:if(confirm('确定删除?'))location='{:U('Admin/Bparking/delete',array('id'=>$vo['id']))}'">删除</a>                                                    | <a id="{$vo['id']}" class="xiangqing" href="javascript:void(0);">实时停放车辆</a>                                                    | <a id="{$vo['id']}" class="xiangqing1" href="javascript:void(0);">历史停放车辆</a>                                                    <if condition="$uid eq '1'">                                                        | <a href="{:U('Admin/Bparking/fssj',array('id'=>$vo['id']))}">实时数据</a>                                                    </if>                                                </a>                                            </td>                                        </tr>                                    </volist>                                    </tbody>                                </table>                            </div>                        </div>                    </div>                </div>            <div class="dataTables_paginate paging_simple_numbers" id="datatable_paginate">                {$show}            </div>            </div>        </div>    <div class="modal fade" id="bjy-add" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">       <div class="modal-dialog">            <div class="modal-content">                <div class="modal-header">                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> ×                    </button>                    <h4 class="modal-title" id="myModalLabel"> 添加车位</h4>                </div>                <div class="modal-body">                    <form id="bjy-form" class="form-inline" action="{:U('Admin/Bparking/add')}" method="post">                        <input type="hidden" name="pid" value="0">                        <table class="table table-striped table-bordered table-hover table-condensed">                            <tr>                                <th width="20%">车位名称:</th>                                <td>                                    <input class="input-medium" type="text" name="title">                                </td>                            </tr>                           <tr>                                <th>可容纳车辆:</th>                               <td>                                    <input class="input-medium" type="text" name="usable_num">                                </td>                            </tr>                            <tr>                                <th>已存储车辆:</th>                                <td>                                   <input class="input-medium" type="text" name="storage_num">                               </td>                            </tr>                            <tr>                                <th>溢出车辆数量:</th>                                <td>                                    <input class="input-medium" type="text" name="overflow_num">                                </td>                            </tr>                           <tr>                                <th>车位编号:</th>                                <td>                                    <input class="input-medium" type="text" name="no">                                </td>                            </tr>                            <tr>                                <th>区域编号:</th>                                <td>                                    <input class="input-medium" type="text" name="block_no">                                </td>                           </tr>                            <tr>                                <th>经纬度:</th>                                <td>                                    <input id="aInput" name="lng_lat" value="拖动鼠标获取经纬度">                                </td>                            </tr>                            <tr>                                <th width="18%">地区:</th>                                <td>                                    <div data-toggle="distpicker" data-autoselect="3" data-province="浙江省">                                        <select name="province"></select>                                        <select name="city"></select>                                        <select name="area"></select>                                    </div>                                </td>                            </tr>                            <tr>                                <th></th>                               <td>                                    <input class="btn btn-success" type="submit" value="添加">                                </td>                        </table>                    </form>                    <div style="width:400px; height:300px; position: absolute; left: 330px; top:230px;">                        <button id="aButton">点击获取经纬度</button>                    </div>                </div>            </div>       </div>    </div>    <div class="modal fade" id="bjy-edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">        <div class="modal-dialog">            <div class="modal-content">                <div class="modal-header">                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> ×</button>                    <h4 class="modal-title" id="myModalLabel"> 修改车位</h4>                </div>                <div class="modal-body">                    <form id="bjy-form" class="form-inline" action="{:U('Admin/Bparking/edit')}" method="post">                        <input type="hidden" name="id">                        <table class="table table-striped table-bordered table-hover table-condensed">                            <tr>                                <th width="20%">车位名称:</th>                                <td>                                    <input class="input-medium" type="text" name="title">                                </td>                            </tr>                            <tr>                                <th>可容纳车辆:</th>                                <td>                                    <input class="input-medium" type="text" name="usable_num">                                </td>                            </tr>                            <tr>                                <th>已存储车辆:</th>                                <td>                                    <input class="input-medium" type="text" name="storage_num">                                </td>                            </tr>                            <tr>                                <th>溢出车辆数量:</th>                                <td>                                    <input class="input-medium" type="text" name="overflow_num">                                </td>                            </tr>                            <tr>                                <th>车位编号:</th>                                <td>                                    <input class="input-medium" type="text" name="no">                                </td>                            </tr>                            <tr>                                <th>区域编号:</th>                                <td>                                    <input class="input-medium" type="text" name="block_no">                                </td>                            </tr>                            <tr>                            <th>经纬度:</th>                                <td>                                    <input id="aInput2" name="lng_lat" value="拖动鼠标获取经纬度">                                </td>                            </tr>                            <tr>                                <th width="18%">地区:</th>                                <td>                                    <div data-toggle="distpicker" data-autoselect="3" data-province="浙江省">                                        <select name="province"></select>                                        <select name="city"></select>                                        <select name="area"></select>                                    </div>                                </td>                            </tr>                            <tr>                                <th></th>                                <td>                                    <input class="btn btn-success" type="submit" value="修改">                                </td>                            </tr>                        </table>                    </form>                    <div style="width:400px; height:300px; position: absolute; left: 330px; top:230px;">                        <button id="aButton2">点击获取经纬度</button>                    </div>                </div>            </div>        </div>    </div>        <script type="text/javascript">            // 传递给B页面            document.getElementById('aButton').onclick = function () {                var aValue = document.getElementById('aInput').value;                art.dialog.data('aValue', aValue);// 存储数据                var path = art.dialog.data('homeDemoPath') || 'http://localhost/index.php/Admin/Bparking/';                art.dialog.open(path + 'iframeB.html?fd', {                    id: 'AAA',                    width:500,                    left:900,                    close: function () {                        var bValue = art.dialog.data('bValue');// 读取B页面的数据                        if (bValue !== undefined) document.getElementById('aInput').value = bValue;                    }                }, false);            };            // 传递给B页面            document.getElementById('aButton2').onclick = function () {                var aValue = document.getElementById('aInput2').value;                art.dialog.data('aValue', aValue);// 存储数据                var path = art.dialog.data('homeDemoPath') || 'http://localhost/index.php/Admin/Bparking/';                art.dialog.open(path + 'iframeB.html?fd', {                    id: 'AAA',                    width:500,                    left:900,                    close: function () {                        var bValue = art.dialog.data('bValue');// 读取B页面的数据                        if (bValue !== undefined) document.getElementById('aInput2').value = bValue;                    }                }, false);            };        // 添加菜单        function add(){            //$("input[name='title'],input[name='mca']").val('');           // $("input[name='pid']").val(0);            $('#bjy-add').modal('show');        }//        function add2(){//                art.dialog.open('http://localhost/manbike0.3/index.php/Admin/Bparking/iframeA.html', {title: '提示'});////        }        // 添加子菜单        function add_child(obj){            var navId=$(obj).attr('navId');            $("input[name='pid']").val(navId);            $("input[name='title']").val('');            $("input[name='mca']").val('');            $("input[name='icon']").val('');            $('#bjy-add').modal('show');        }       //   navOverflow_num="{$vo['Overflow_num']}" navNo="{$vo['No']}" navBlock_no="{$vo['Block_no']}"        // 修改菜单        function edit(obj){            var navId=$(obj).attr('navId');            var navTitle=$(obj).attr('navTitle');            var navUsable_num=$(obj).attr('navUsable_num');            var navStorage_num=$(obj).attr('navStorage_num');            var navOverflow_num=$(obj).attr('navOverflow_num');            var navNo=$(obj).attr('navNo');            var navBlock_no=$(obj).attr('navBlock_no');            var navLng_Lat=$(obj).attr('navLng_Lat');            $("input[name='id']").val(navId);            $("input[name='title']").val(navTitle);            $("input[name='usable_num']").val(navUsable_num);            $("input[name='storage_num']").val(navStorage_num);            $("input[name='overflow_num']").val(navOverflow_num);            $("input[name='no']").val(navNo);            $("input[name='block_no']").val(navBlock_no);            $("input[name='lng_lat']").val(navLng_Lat);            $('#bjy-edit').modal('show');        }    </script>    <script>        $("#datatable_paginate a").click(function(){            var href = $(this).attr("href");            // $(this).attr("href","#");            var title = $("#title").val();            var usable_num = $("#usable_num").val();            var storage_num = $("#storage_num").val();            var no = $("#no").val();            if(title != ''){                href = href + '&title='+title;            }            if(usable_num != ''){                href = href + '&usable_num='+usable_num;            }            if(storage_num != ''){                href = href + '&storage_num='+storage_num;            }            if(no != ''){                href = href + '&no='+no;            }            //alert(href);            window.location.href=href;            return false;        });    </script><script>$(".xiangqing").click(function(){//alert(1);var id = $(this).attr("id");//alert(id);//去后台获取数据var url = 'http://localhost/index.php/Admin/Bparking/infobikesexist/id/'+ id;location.href=url;})$(".xiangqing1").click(function(){//alert(1);var id = $(this).attr("id");//alert(id);//去后台获取数据var url = 'http://localhost/index.php/Admin/Bparking/infobikesall1/id/'+ id;location.href=url;})</script>    <!-- /page content --></block>

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

原创粉丝点击