zoukankan      html  css  js  c++  java
  • TP6管理后台实战第四天权限管理

    第四天:

    1、菜单权限管理--ok
    2、日志管理-- ok
     

    进入开发:

    一、日志管理

    日志管理比较简单,先把日志管理搞好
     

    1.1 管理员登录日志

    搜索:起止时间,管理员账号 ,状态,IP, -- ok
    列表项:ID,管理员ID/账号 状态 IP 添加时间 操作 (删除) -- ok
    功能:批量删除, -- ok
    分页功能: --ok
     
     1  /**
     2      * 管理员登录日志
     3      */
     4     public function adminLoginList()
     5     {
     6         if (!$this->access)  exit('无此访问权限!');
     7 
     8         $data = request()->param();
     9 
    10         $return_data = array(
    11             'admin_info' => $this->admin_info,
    12             'admin_id'   => $this->admin_id
    13         );
    14 
    15         //搜索条件
    16         $whereCond = array();
    17         if (!empty($data['admin_name'])) 
    18         {
    19             $admin_id = Db::name('yphp_admin')->where("admin_name",$data['admin_name'])->value('admin_id');
    20             if (!empty($admin_id)) 
    21             {
    22                $whereCond[] = array('admin_id','=',$admin_id);
    23             }
    24             
    25         } 
    26         if (!empty($data['login_status'])) $whereCond[] = array('login_status','=',$data['login_status']);
    27         if (!empty($data['login_ip'])) $whereCond[] = array('login_ip','=',$data['login_ip']);
    28         if (!empty($data['start'])) $whereCond[] = array('add_datetime','>',$data['start']);
    29         if (!empty($data['end'])) $whereCond[] = array('add_datetime','<=',$data['end']." 23:59:59");
    30 
    31         
    32         //搜索默认值
    33         $return_data['admin_name']     = empty($data['admin_name'])?'':$data['admin_name'];
    34         $return_data['login_status'] = empty($data['login_status'])?'':$data['login_status'];
    35         $return_data['login_ip']   = empty($data['login_ip'])?'':$data['login_ip'];
    36         $return_data['start']   = empty($data['start'])?'':$data['start'];
    37         $return_data['end']   = empty($data['end'])?'':$data['end'];
    38 
    39 
    40         //获取列表
    41         $data_list = Db::name('yphp_admin_login')->where($whereCond)->order('id', 'desc')->paginate(array(
    42             'list_rows' => 10,
    43             'query'     => $data
    44         ))->each(function($item, $key){
    45 
    46             $item['admin_name'] = Db::name('yphp_admin')->where("admin_id",$item['admin_id'])->value('admin_name');
    47             return $item;
    48         });
    49 
    50 
    51         $return_data['data_list'] = $data_list;
    52         // 获取分页显示
    53         $return_data['page'] = $data_list->render();
    54 
    55        return view("logs/admin_login_list",$return_data);
    56     }
    57 
    58     /**
    59      * 删除日志
    60      */
    61     public function adminLoginDel()
    62     {
    63 
    64         if (!$this->access)  return json(array('status'=>'FAIL','msg'=>'无此访问权限!')); 
    65 
    66        $id  = request()->param('id');
    67        $ids  = request()->param('ids');
    68 
    69        if (!empty($id)) 
    70        {
    71            if($this->admin_info['admin_role_id'] == 0 || $this->admin_info['admin_role_id'] == 1)
    72            {
    73                 Db::name('yphp_admin_login')->where("id",$id)->delete();
    74                 return json(array('status'=>'SUCCESS','msg'=>'删除成功'));
    75            }
    76            else
    77            {
    78                 return json(array('status'=>'FAIL','msg'=>'删除失败,日志记录仅超级管理员和系统管理员可以删除!'));
    79            }
    80        }elseif(!empty($ids)){
    81 
    82             if($this->admin_info['admin_role_id'] == 0 || $this->admin_info['admin_role_id'] == 1)
    83            {
    84                 Db::name('yphp_admin_login')->where("id",'in',$ids)->delete();
    85                 return json(array('status'=>'SUCCESS','msg'=>'删除成功'));
    86            }
    87            else
    88            {
    89                 return json(array('status'=>'FAIL','msg'=>'删除失败,日志记录仅超级管理员和系统管理员可以删除!'));
    90            }
    91        }
    92     }
    View Code

    1.2 管理员操作日志

    搜索:添加时间 管理员账号,控制器,操作,IP, -- ok
    列表项:ID,管理员ID/账号 访问地址 ,控制器 操作 IP 添加时间 操作(查看明细,删除) -- ok
    功能:批量删除 ,单个删除-- ok
    功能:查看详情 -- ok
     
     1 /**
     2      * 管理员操作列表
     3      */
     4     public function adminOpList()
     5     {
     6         if (!$this->access)  exit('无此访问权限!');
     7 
     8         $data = request()->param();
     9 
    10         $return_data = array(
    11             'admin_info' => $this->admin_info,
    12             'admin_id'   => $this->admin_id
    13         );
    14 
    15         //搜索条件
    16         $whereCond = array();
    17         if (!empty($data['admin_name']))  $whereCond[] = array('admin_name','=',$data['admin_name']);
    18         if (!empty($data['op_controller'])) $whereCond[] = array('op_controller','=',$data['op_controller']);
    19         if (!empty($data['op_action'])) $whereCond[] = array('op_action','=',$data['op_action']);
    20         if (!empty($data['login_ip'])) $whereCond[] = array('login_ip','=',$data['login_ip']);
    21         if (!empty($data['start'])) $whereCond[] = array('add_datetime','>',$data['start']);
    22         if (!empty($data['end'])) $whereCond[] = array('add_datetime','<=',$data['end']." 23:59:59");
    23 
    24         //搜索默认值
    25         $return_data['admin_name']     = empty($data['admin_name'])?'':$data['admin_name'];
    26         $return_data['op_controller'] = empty($data['op_controller'])?'':$data['op_controller'];
    27         $return_data['op_action'] = empty($data['op_action'])?'':$data['op_action'];
    28         $return_data['ip_address']   = empty($data['ip_address'])?'':$data['ip_address'];
    29         $return_data['start']   = empty($data['start'])?'':$data['start'];
    30         $return_data['end']   = empty($data['end'])?'':$data['end'];
    31 
    32 
    33         //获取列表
    34         $data_list = Db::name('yphp_admin_op_log')->where($whereCond)->order('id', 'desc')->paginate(array(
    35             'list_rows' => 10,
    36             'query'     => $data
    37         ));
    38 
    39         $return_data['data_list'] = $data_list;
    40         // 获取分页显示
    41         $return_data['page'] = $data_list->render();
    42 
    43        return view("logs/admin_op_list",$return_data);
    44     }
    45 
    46     /**
    47      * 删除日志
    48      */
    49     public function adminOpDel()
    50     {
    51         if (!$this->access)  return json(array('status'=>'FAIL','msg'=>'无此访问权限!')); 
    52 
    53        $id  = request()->param('id');
    54        $ids  = request()->param('ids');
    55 
    56        if (!empty($id)) 
    57        {
    58            if($this->admin_info['admin_role_id'] == 0 || $this->admin_info['admin_role_id'] == 1)
    59            {
    60                 Db::name('yphp_admin_op_log')->where("id",$id)->delete();
    61                 return json(array('status'=>'SUCCESS','msg'=>'删除成功'));
    62            }
    63            else
    64            {
    65                 return json(array('status'=>'FAIL','msg'=>'删除失败,日志记录仅超级管理员和系统管理员可以删除!'));
    66            }
    67        }elseif(!empty($ids)){
    68 
    69             if($this->admin_info['admin_role_id'] == 0 || $this->admin_info['admin_role_id'] == 1)
    70            {
    71                 Db::name('yphp_admin_op_log')->where("id",'in',$ids)->delete();
    72                 return json(array('status'=>'SUCCESS','msg'=>'删除成功'));
    73            }
    74            else
    75            {
    76                 return json(array('status'=>'FAIL','msg'=>'删除失败,日志记录仅超级管理员和系统管理员可以删除!'));
    77            }
    78        }
    79     }
    80 
    81      /**
    82      * 查看日志
    83      */
    84      public function adminOpShow()
    85     {
    86         if (!$this->access)  exit('无此访问权限!');
    87         
    88        $id  = request()->param('id');
    89 
    90        $info = Db::name('yphp_admin_op_log')->where("id",$id)->find();
    91        
    92        return view("logs/admin_op_show",array('info'=>$info));
    93     }
    View Code

    二、菜单权限管理

    2.1 菜单,权限列表
     
    搜索:菜单名称,规则分类,控制器,操作,权限状态 -- ok
    列表项:ID,菜单名称,菜单类型,Controller,Action,状态,添加时间 ,操作(添加子菜单,编辑,修改) -- ok
    功能:删除菜单 --ok
    功能:添加菜单 --ok
    功能:编辑菜单--ok
     
    2.2 角色中加入权限选择
    添加角色,增加权限选项--ok
    修改角色,增加角色选项--ok
     
      1 /**
      2      * 菜单权限列表
      3      */
      4     public function powerList()
      5     {
      6         if (!$this->access)  exit('无此访问权限!');
      7 
      8         $data = request()->param();
      9 
     10         $return_data = array(
     11             'admin_info' => $this->admin_info,
     12             'admin_id'   => $this->admin_id
     13         );
     14 
     15         //搜索条件
     16         $whereCond = array();
     17         if (!empty($data['parent_id']))  $whereCond[] = array('id','=',$data['parent_id']);
     18       
     19         //搜索默认值
     20         $return_data['parent_id'] = empty($data['parent_id'])?'':$data['parent_id'];
     21 
     22         //搜索项
     23         $data_lista = Db::name('yphp_admin_power')->where("ptype",1)->order('porder', 'desc')->select()->toArray();
     24 
     25         //获取第一级菜单
     26         $data_list = Db::name('yphp_admin_power')->where($whereCond)->where("ptype",1)->order('porder', 'desc')->select()->toArray();
     27         foreach ($data_list as $key => $val) 
     28         {
     29             //获取二级菜单
     30             $data_list[$key]['child'] = Db::name('yphp_admin_power')->where("parent_id",$val['id'])->where("ptype",2)->order('porder', 'desc')->select()->toArray();
     31             //获取三级菜单
     32             foreach ($data_list[$key]['child'] as $key2 => $val2) 
     33             {
     34                 $data_list[$key]['child'][$key2]['child'] = Db::name('yphp_admin_power')->where("parent_id",$val2['id'])->where("ptype",3)->order('porder', 'desc')->select()->toArray();
     35             }
     36         }
     37         $return_data['data_list'] = $data_list;
     38         $return_data['data_lista'] = $data_lista;
     39         // 获取分页显示
     40         //$return_data['page'] = $data_list->render();
     41 
     42 
     43        return view("admin/powders_list",$return_data);
     44     }
     45 
     46     /**
     47      * 删除菜单或权限
     48      */
     49     public function powerDel()
     50     {
     51 
     52        if (!$this->access)  return json(array('status'=>'FAIL','msg'=>'无此访问权限!')); 
     53 
     54        $id  = request()->param('id');
     55        if (empty($id)) return json(array('status'=>'FAIL','msg'=>'ID不能为空!'));
     56 
     57        //判断该角色下面没有管理员,则可进行删除
     58        $num = Db::name('yphp_admin_power')->where("parent_id",$id)->count();
     59        if ($num > 0) {
     60           return json(array('status'=>'FAIL','msg'=>'删除失败,该菜单下面还有子菜单!'));
     61        }
     62 
     63        if($this->admin_info['admin_role_id'] > 1)
     64        {
     65             return json(array('status'=>'FAIL','msg'=>'删除失败,仅超级管理员或系统管理员可删除菜单'));
     66        }
     67 
     68        Db::name('yphp_admin_power')->where("id",$id)->delete();
     69 
     70        return json(array('status'=>'SUCCESS','msg'=>'删除成功'));
     71     }
     72 
     73     /**
     74      * 添加菜单
     75      */
     76     public function powerAdd()
     77     {
     78         if (!$this->access)  exit('无此访问权限!');
     79 
     80        $parent_id = request()->param('parent_id');
     81 
     82         //获取第一级菜单
     83         $data_list = Db::name('yphp_admin_power')->where("ptype",1)->order('porder', 'desc')->select()->toArray();
     84         foreach ($data_list as $key => $val) 
     85         {
     86             //获取二级菜单
     87             $data_list[$key]['child'] = Db::name('yphp_admin_power')->where("parent_id",$val['id'])->where("ptype",2)->order('porder', 'desc')->select()->toArray();
     88            
     89         }
     90         $return_data['parent_id'] = $parent_id;
     91         $return_data['data_list'] = $data_list;
     92 
     93 
     94        return view("admin/power_add",$return_data);
     95     }
     96 
     97     /**
     98      * 查看图标
     99      */
    100     public function showIcon()
    101     {
    102        return view("index/unicode");
    103     }
    104     /**
    105      * 编辑菜单
    106      */
    107     public function powerEdit()
    108     {
    109 
    110         if (!$this->access)  exit('无此访问权限!');
    111 
    112        $id = request()->param('id');
    113 
    114         //获取第一级菜单
    115         $data_list = Db::name('yphp_admin_power')->where("ptype",1)->order('porder', 'desc')->select()->toArray();
    116         foreach ($data_list as $key => $val) 
    117         {
    118             //获取二级菜单
    119             $data_list[$key]['child'] = Db::name('yphp_admin_power')->where("parent_id",$val['id'])->where("ptype",2)->order('porder', 'desc')->select()->toArray();
    120         }
    121         $info = Db::name('yphp_admin_power')->where("id",$id)->find();
    122 
    123         $return_data['data_list'] = $data_list;
    124         $return_data['info'] = $info;
    125 
    126 
    127        return view("admin/power_edit",$return_data);
    128     }
    129     /**
    130      * 添加菜单操作
    131      */
    132     public function powerAddAct()
    133     {
    134         $data = request()->param();
    135 
    136         //确定菜单类别
    137          if ($data['parent_id'] == 0) {
    138              $data['ptype'] = 1;
    139              if (empty($data['picon'])) {
    140                  $data['picon'] = "&#xe723;";
    141              }
    142 
    143              $data['picon'] = base64_encode($data['picon']);
    144 
    145          }else{
    146             $info = Db::name('yphp_admin_power')->where("id",$data['parent_id'])->find();
    147             $data['ptype'] = $info['ptype']+1;
    148          }
    149 
    150 
    151        if(empty($data['id']))
    152        {
    153          $info = Db::name('yphp_admin_power')->where("pname",$data['pname'])->find();
    154          if(!empty($info))
    155          {
    156             return json(array('status'=>'FAIL','msg'=>'该菜单已存在,请更换菜单名'));
    157          }
    158 
    159          $id = Db::name('yphp_admin_power')->strict(false)->insertGetId($data);
    160          if(empty($id)){
    161             return json(array('status'=>'FAIL','msg'=>'添加菜单失败!'));
    162          }else{
    163             return json(array('status'=>'SUCCESS','msg'=>'添加菜单成功!'));
    164          }
    165        }
    166        else
    167        {
    168          //修改管理员
    169          Db::name('yphp_admin_power')->strict(false)->update($data);
    170          return json(array('status'=>'SUCCESS','msg'=>'修改菜单成功!'));
    171        }
    172     }
    View Code
     

    唠唠嗑:

    第四天开发结束:针对TP中有些比较别扭的写法,直接按照官方的写就行了,没必要一直感觉别扭去抵触,这样只能大大缩减你的开发速度。既然选择了用TP,就得接受它。
     

    页面效果展示一部分:

    管理员登录日志
     

     

    管理员操作日志
     

     

    管理员查看日志详情
     

     

    菜单权限管理:
     

     

    添加菜单权限:
     
    修改菜单权限:
     

     

    但行好事,莫问前程!

    本文来自博客园,作者:yangphp,转载请注明原文链接:https://www.cnblogs.com/ypeih/p/15468680.html

  • 相关阅读:
    【线段树】懒标记的维护
    【dp】luoguP4796 关于图 想不到是状压dp (┬_┬)
    【数论】莫比乌斯函数+中国剩余定理
    【积累】Burnside引理和Polya定理
    【排序优化】牛客练习赛54D
    ubuntu修改hostname
    apt-get命令详解
    微信历史版本下载
    vim自动补全快捷键
    Servlet实例
  • 原文地址:https://www.cnblogs.com/ypeih/p/15468680.html
Copyright © 2011-2022 走看看