sql
- --用户表
- CREATE TABLE IF NOT EXISTS `sw_manager` (
- `mg_id` int NOT NULL AUTO_INCREMENT,
- `mg_name` varchar(20) NOT NULL comment '名称',
- `mg_pwd` varchar(32) NOT NULL comment '密码',
- `mg_time` int unsigned NOT NULL comment '时间',
- `mg_role_id` tinyint(1) unsigned not null default 0 comment '角色id',
- PRIMARY KEY (`mg_id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- --权限表
- CREATE TABLE IF NOT EXISTS `sw_auth` (
- `auth_id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
- `auth_name` varchar(20) NOT NULL comment '名称',
- `auth_pid` smallint(6) unsigned NOT NULL comment '父id',
- `auth_c` varchar(32) not null default '' comment '模块',
- `auth_a` varchar(32) not null default '' comment '操作方法',
- `auth_path` varchar(32) NOT NULL comment '全路径',
- `auth_level` tinyint not null default 0 comment '权限级别012',
- PRIMARY KEY (`auth_id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- --角色表
- CREATE TABLE IF NOT EXISTS `sw_role` (
- `role_id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
- `role_name` varchar(20) NOT NULL comment '角色名称',
- `role_auth_ids` varchar(128) not null default '' comment '权限ids,1,2,5',
- `role_auth_ac` text comment '模块-操作',
- PRIMARY KEY (`role_id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- `role_auth_ac`=”Company-show,Cat-mag,Product-list”
- 角色:
- 董事长
- 总监
- 高级经理
- 经理
- 项目经理
- 业务主管
- 客服
- 技术支持
- 美工
- 员工
- CREATE TABLE IF NOT EXISTS `sw_auth` (
- `auth_id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
- `auth_name` varchar(20) NOT NULL comment '名称',
- `auth_pid` smallint(6) unsigned NOT NULL comment '父id',
- `auth_c` varchar(32) not null default '' comment '模块',
- `auth_a` varchar(32) not null default '' comment '操作方法',
- `auth_path` varchar(32) NOT NULL comment '全路径父级全路径与本身id做连接,如果没有父级,全路径就是本身id值,用于排序',
- PRIMARY KEY (`auth_id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- insert into sw_auth values (null,'商品管理',0,'','','1');
- insert into sw_auth values (null,'商品列表',1,'Goods','showlist','1-2');
- insert into sw_auth values (null,'添加商品',1,'Goods','add','1-3');
- insert into sw_auth values (null,'用户评论',1,'User','pinglun','1-4');
- insert into sw_auth values (null,'订单管理',0,'','','5');
- insert into sw_auth values (null,'订单列表',5,'Order','showlist','5-6');
- insert into sw_auth values (null,'订单查询',5,'Order','view','5-7');
- insert into sw_auth values (null,'文章管理',0,'','','8');
- insert into sw_auth values (null,'文章列表',8,'Article','showlist','8-9');
- insert into sw_auth values (null,'权限管理',0,'','','10');
- insert into sw_auth values (null,'管理员列表',10,'Manager','showlist','10-11');
- insert into sw_auth values (null,'角色管理',10,'Role','showlist','10-12');
- insert into sw_auth values (null,'权限管理',10,'Auth','showlist','10-13');
- CREATE TABLE IF NOT EXISTS `sw_role` (
- `role_id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
- `role_name` varchar(20) NOT NULL comment '角色名称',
- `role_auth_ids` varchar(128) not null default '' comment '权限ids,1,2,5',
- `role_auth_ac` text comment '模块-操作',
- PRIMARY KEY (`role_id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- insert into sw_role values (null,'经理','5,6,7','Order-showlist,Order-view');
- insert into sw_role values (null,'员工','1,2,3,4','Goods-showlist,Goods-add,User-pinglun');
- alter table sw_manager add mg_time int UNSIGNED not null comment '时间';
- alter table sw_manager add mg_role_id tinyint UNSIGNED not null default 0 comment '角色id';
RoleAction
- <?php
- import("@.components.AdminAction");
- class RoleAction extends AdminAction{
- function showlist(){
- $info = D("Role")->select();
- $this -> assign('info',$info);
- $this -> display();
- }
- //分配权限方法
- function distribute($role_id){
- //表单数据收集
- if(!empty($_POST)){
- //给角色分配具体权限
- $rst = D("Role")->distributeAuth($_POST['auth_name'],$role_id); //普通模型方法
- if($rst){
- $this -> success("分配权限成功",U("Role/showlist"));
- }
- }else {
- //获得全部权限信息
- $p_auth = D("Auth")->where("auth_level=0")->select();
- $s_auth = D("Auth")->where("auth_level=1")->select();
- $t_auth = D("Auth")->where("auth_level=2")->select();
- //根据$role_id查询对应角色名称
- $role_info = D("Role")->getByRole_id($role_id);
- $role_name = $role_info['role_name']; //角色名称
- $role_auth_ids = $role_info['role_auth_ids']; //权限id值
- $this -> assign('role_name', $role_name);
- $this -> assign('role_auth_ids', explode(',',$role_auth_ids));
- $this -> assign('p_auth',$p_auth);
- $this -> assign('s_auth',$s_auth);
- $this -> assign('t_auth',$t_auth);
- $this -> display();
- }
- }
- }
IndexAction
- <?php
- //后台主架构控制器
- class IndexAction extends Action{
- //默认调用index方法
- function index(){
- $this -> display();
- }
- //"品"字头部
- function head(){
- //查看系统有哪些常量可以使用
- //获得全部常量信息,true,常量根据类型进行分类显示
- //var_dump(get_defined_constants(true));
- $this -> display();
- }
- //"品"字左边
- function left(){
- //用户--角色--权限
- //给左边传递数据,可以直接使用
- //$_SESSION['mg_id']
- //manager role auth
- $model = M();
- $sql = "select b.role_auth_ids from sw_manager a join sw_role b on a.mg_role_id=b.role_id where a.mg_id=".$_SESSION['mg_id'];
- $info = $model -> query($sql);
- $auth_ids = $info[0]['role_auth_ids'];
- //查询具体权限
- //查询父权限
- $sql = "select * from sw_auth where auth_level=0";
- if($_SESSION['mg_id'] != 1){
- $sql .=" and auth_id in ($auth_ids)";
- }
- $p_auth_info = $model -> query($sql);
- //查询子权限
- $sql = "select * from sw_auth where auth_level=1";
- if($_SESSION['mg_id'] != 1){
- $sql .=" and auth_id in ($auth_ids)";
- }
- $s_auth_info = $model -> query($sql);
- $this -> assign('p_auth', $p_auth_info);
- $this -> assign('s_auth', $s_auth_info);
- $this -> display();
- }
- function right(){
- $this -> display();
- }
- }
ManagerAction
- <?php
- //后台管理员控制器
- class ManagerAction extends Action{
- //登录系统
- function login(){
- //读取语言变量信息
- //L(名称) 读取指定语言信息
- //L() 把全部语言以数组形式给我们返回
- //show_bug(L());
- if(!empty($_POST)){
- //判断验证码是否正确
- //$_SESSION['verify']
- //让用户提交过来的验证码与session的做比较
- if(md5($_POST['captcha']) == $_SESSION['verify']){
- //用户名和密码校验
- //在数据model模型里边,自定义一个方法校验用户名和密码
- $manager_model = D("Manager");
- $user_info = $manager_model -> checkNamePwd($_POST['mg_name'],$_POST['mg_pwd']);
- //如果$user_info不等于false,就说明用户名和密码是正确的
- if($user_info !== false){
- //持久化用户信息(id和名字)
- session("mg_name",$user_info['mg_name']);
- session("mg_id",$user_info['mg_id']);
- $this -> redirect("Index/index");
- } else {
- echo "用户名或密码错误!";
- }
- } else {
- echo "验证码不正确";
- }
- }
- $this -> assign('language',L());
- $this -> display();
- }
- //退出系统
- function logout(){
- //删除session信息
- session(null);
- $this -> redirect("Manager/login");
- }
- //生成验证码
- function verifyImg1(){
- //手动加载对应的类文件 include()引入
- //echo Image::buildImageVerify();
- //ThinkPHP/Common/common.php
- // show_bug(class_exists('World'));
- // //shop/Lib/hello/world.class.php
- // import("@.hello.world");
- // show_bug(class_exists("World"));
- //
- // show_bug(class_exists('Orange'));
- // import("@.apple.orange");
- // show_bug(class_exists('Orange'));
- //引入框架核心类文件
- // show_bug(class_exists('Driver'));
- // import("think.car.driver");
- // show_bug(class_exists('Driver'));
- //第三方类库文件引入
- // show_bug(class_exists('Pink'));
- // import("ORG.red.pink");
- // show_bug(class_exists('Pink'));
- // import("ORG.Util.Image");
- // echo Image::buildImageVerify();
- //引入特殊类文件
- // show_bug(class_exists('Banana'));
- // //shop/Lib/apple/banana/good/fresh.class.php
- // import("@.apple.banana#good#fresh");
- // show_bug(class_exists('Banana'));
- }
- //生成验证码
- function verifyImg(){
- import("ORG.Util.Image");
- echo Image::buildImageVerify();
- }
- function showlist(){
- //获得全部管理员信息
- $info = D("Manager")->select();
- //获得角色信息
- $role = D("Role")->select();
- //把角色变为一维数组信息 array(id值->名称,id值->名称...)
- $role_info = array();
- foreach($role as $k => $v){
- $role_info[$v['role_id']] = $v['role_name'];
- }
- $this -> assign('role_info', $role_info);
- $this -> assign("info", $info);
- $this -> display();
- }
- //添加管理员
- function add(){
- //判断form提交
- if(!empty($_POST)){
- $manager_model = D("Manager");
- //给manager存入数据
- $_POST['mg_pwd'] = "123456";
- $_POST['mg_time'] = time();
- $manager_model -> create();
- $rst = $manager_model -> add();
- if($rst){
- $this -> success("添加管理员成功",U("Manager/showlist"));
- }
- }else {
- //获得角色信息
- $role = D("Role")->select();
- //把角色变为一维数组信息 array(id值->名称,id值->名称...)
- $role_info = array();
- foreach($role as $k => $v){
- $role_info[$v['role_id']] = $v['role_name'];
- }
- $this -> assign('role_info', $role_info);
- $this -> display();
- }
- }
- }
AuthAction
- <?php
- //权限控制器
- class AuthAction extends Action{
- function showlist(){
- //获得全部权限
- $info = D("Auth")->order("auth_path")->select();
- //权限父子级有缩进关系
- foreach($info as $k => $v){
- $info[$k]['auth_name'] = str_repeat("-/",$v['auth_level']).$info[$k]['auth_name'];
- }
- $this -> assign("info",$info);
- $this -> display();
- }
- function add(){
- //判断表单是否提交数据
- if(!empty($_POST)){
- // /show_bug($_POST);
- //在model模型里边制作一个方法处理权限添加
- $rst = D("Auth")->saveAuth($_POST);
- if($rst){
- $this -> success("添加权限成功",U("Auth/showlist"));
- }
- } else {
- $info = D("Auth")->where('auth_level<2')->order("auth_path")->select();
- //权限父子级有缩进关系
- foreach($info as $k => $v){
- $info[$k]['auth_name'] = str_repeat("-/",$v['auth_level']).$info[$k]['auth_name'];
- }
- //show_bug($info);
- $authinfo = array(); //array(1=>商品管理,2=>商品列表...)
- foreach($info as $kk => $vv){
- $authinfo[$vv['auth_id']] = $vv['auth_name'];
- }
- $this -> assign("authinfo",$authinfo);
- $this -> display();
- }
- }
- }