• 6.25 权限管理


    <!DOCTYPE html>
    <html lang="zh">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <script src="../public/jquery-3.3.1.min.js" type="text/javascript" charset="utf-8"></script>
        <title>登陆页面</title>
    </head>
    <body>
        账号:<input type="text" id="uid" /><br>
        密码:<input type="password" id="pwd" /><br>
            <button onclick="denglu()">登陆</button>    
    </body>
    </html>
    <script type="text/javascript">
        function denglu(){
            var uid = $("#uid").val();
            var pwd = $("#pwd").val();
            $.ajax({
                type:"post",
                url:"chuli.php",
                async:true,
                data:{
                    type:"denglu",
                    uid:uid,
                    pwd:pwd
                },
                dataType:"text",
                success:function(data){
                    if(data.trim() == "ok"){
                        window.location.href = "quanxian.php";
                    };
                }
            });
        }
    </script>
    <?php
        session_start();
        if(empty($_SESSION['uid'])){
            header("Location:login.php");
        }
        ?>
    <!DOCTYPE html>
    <html lang="zh">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <link rel="stylesheet" type="text/css" href="../public/bootstrap.min.css"/>
        <script src="../public/jquery-3.3.1.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="../public/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
        <title>主页面</title>
    </head>
    <body>
        <input type="hidden" name="uid" id="uid" value="<?php echo $_SESSION['uid']; ?>" />
        <select id="renyuan">
            
        </select>
        <div id="juese">
            
        </div>
        <button id="btn">保存</button>
        <div id="daohang">
            
        </div>
        
    </body>
    </html>
    <script type="text/javascript">
        $(function(){
            reny();
            loadGn();
        })
        
        //打印人员的信息;
        function reny(){
            var str ="";
            $.ajax({
                type:"post",
                url:"chuli.php",
                async:false,
                data:{
                    type:"renyuan"
                },
                dataType:"json",
                success:function(data){
                    for(var i=0;i<data.length;i++){
                            str += "<option value="+data[i][1]+">"+data[i][0]+"</option>";
                    }
                    $("#renyuan").html(str);
                    juese();
                    xuan();
                }    
            });
        }
        //打印角色信息
        function juese(){
            var str ="";
            $.ajax({
                type:"post",
                url:"chuli.php",
                async:true,
                data:{
                    type:"juese"
                },
                dataType:"json",
                success:function(data){
                    //alert();
                    for(var i=0;i<data.length;i++){
                        str +="<input type='checkbox' value='"+data[i][1]+"' class='ck'>"+data[i][0]+"" ;    
                    }
                    $("#juese").html(str);
                }
            });
        }
        //根据人员选中对应角色
            function xuan(){
                var uid = $("#renyuan").val();
                $.ajax({
                    type:"post",
                    url:"chuli.php",
                    async:true,
                    data:{
                        type:"xuan",
                        uid:uid     
                    },
                    dataType:"json",
                    success: function(data){
                        //方法一:
                        //prop()设置或返回被选元素的属性和值。 方法应用于检索属性值主要是标签自带属性如(checkbox的checked属性)
                        //$.inArray() 函数用于在数组中查找指定值,并返回它的索引值(如果没有找到,则返回-1)
                        var attr = [];
                        for (var i=0;i<data.length;i++){
                            attr[i] = data[i][0];//每个人对应的角色id
                        }
                        var ck =$(".ck");
                        ck.prop("checked",false);
                        for(var j=0;j<ck.length;j++){
                            var ck_val = ck.eq(j).val();//每个input的值
                            if($.inArray(ck_val,attr)>-1){
                                ck.eq(j).prop("checked",true);
                            }
                        }
                            
                        
                            
                            //方法二
                          /*  var ck = $(".ck");
                            ck.removeAttr('checked');
                            for(var i = 0;i<data.length;i++){
                                ck.each(function(){//遍历每个复选框 当复选框的值等于php传过来的每个人的角色id时,使复选框选中
                                    if($(this).val() == data[i][0]){//
                                        $(this).attr('checked','checked');    
                                    }
                                })
                            }*/
                            
                        }
                    });
                }
        //下拉改变时,角色对应改变       
        $("#renyuan").change(function(){
                xuan();
            })
        // 选中每个人的角色,保存角色   
        $("#btn").click(function(){
                var uid = $("#renyuan").val();
                var ck = $(".ck");
                var juese = "";
                for(var i=0;i<ck.length;i++)
                {
                    if(ck.eq(i).prop("checked")){
                        juese += ck.eq(i).val()+"|";          
                    }
                }
                 //j001|j002|j003|
                juese = juese.substr(0,juese.length-1);
                $.ajax({
                        type:"post",
                        url:"chuli.php",
                        async:true,
                        data:{
                            type:"baocun",
                            uid:uid,
                            juese:juese               
                            },
                        dataType:"text",
                        success: function(data){
                             reny();
                                 
                            }
                   });
    
        })
        //打印每个人的功能
        function loadGn(){
            var uid = $("#uid").val();
             $.ajax({
                        type:"post",
                        url:"chuli.php",
                        async:true,
                        data:{
                            type:"load_Gn",
                            uid:uid                  
                        },
                        dataType:"json",
                        success: function(data){
                            var str = "";
                            for(var i=0;i<data.length;i++){
                                str +="<div>"+data[i][0]+"</div>";
                            }
                            $("#daohang").html(str);
                        }
                   });
        }
    </script>
    <?php
          session_start();
          $conn = new mysqli("localhost","root","","quanxian");
          $conn->connect_error?die():"";
          $type = $_POST['type'];
          switch($type){
              case "renyuan":
              $sql="select name,uid from user";
              $result= $conn->query($sql);
              $attr = $result->fetch_all();
              echo json_encode($attr);
              break;
              case "juese":
              $sql = "select jname,jcode from juese";
              $result= $conn->query($sql);
              $attr = $result->fetch_all();
              echo json_encode($attr);
              break;
              case "xuan":
                  $uid = $_POST['uid'];
                $sql = "select jueseid from user_juese where userid='{$uid}'";
                $result = $conn->query($sql);
                $attr =$result->fetch_all();
                echo json_encode($attr);
            break;
            case "baocun":
                $uid = $_POST['uid'];
                $juese = $_POST['juese'];
                $del_sql = "delete from user_juese where userid='{$uid}'";
                $del_result = $conn->query($del_sql);
                $attr= explode("|",$juese);
                //explode 通过|剪成数组;
                //implode/join("@","$attr")通过@把数组每一项连成字符串;
                foreach($attr as $v){
                    $in_sql = "insert into user_juese values('','{$uid}','{$v}')";
                    $in_result = $conn->query($in_sql);
                }
          break;
          case "denglu":
              $uid = $_POST['uid'];
              $pwd = $_POST['pwd'];
              $sql = "select pwd from user where uid ='{$uid}'";
              $result = $conn->query($sql);
              $attr = $result->fetch_row();
              if($pwd == $attr[0]){
                  $_SESSION['uid'] = $uid ;
                  echo "ok";
              }else{
                  echo "no";
               }
          break;
          case "load_Gn":
            $uid = $_POST['uid'];
            $js = "select jueseid from user_juese where userid='{$uid}'";
            $js_result = $conn->query($js);
            $js_attr = $js_result->fetch_all();
            $js_str = "";
            foreach($js_attr as $v){
                $js_str.= $v[0]."','";      
            }
            $js_str = substr($js_str,0,-3);//substr(string,start,length)
            
            $gn ="select gnid from juese_gongneng where jueseid in('{$js_str}')";
            $gn_result = $conn->query($gn);
            $gn_attr = $gn_result->fetch_all();
            $gn_str ="";
            foreach($gn_attr as $v){
                $gn_str.= $v[0]."','";      
            }
            $gn_str = substr($gn_str,0,-3);
            
            $gn_name = "select name from gongneng where code in('{$gn_str}')";
            $result = $conn->query($gn_name);
              $attr = $result->fetch_all();
              echo json_encode($attr);
              break;
          }
         
    ?>
  • 相关阅读:
    【ccf 2017/12/4】行车路线(dijkstra变形)
    【ccf2017-12-2】游戏(模拟)
    解决让浏览器兼容ES6特性
    富文本编辑器ckeditor的使用
    JavaScript中,让一个div在固定的父div中任意拖动
    父组件如何向子组件方法(对话框的封装)
    Vue2.0 Transition常见用法全解惑
    JavaScript事件冒泡简介及应用
    为什么axios请求接口会发起两次请求
    修改input type=file 标签默认样式的简单方法
  • 原文地址:https://www.cnblogs.com/sunhao1987/p/9226322.html
Copyright © 2020-2023  润新知