官方的太麻烦,太繁琐,自己写个类 判断控制器和方法在不在自己的里面即可。
#创建后台管理员表
create table trkr_admin(
id int unsigned not null auto_increment primary key comment '管理员ID编号',
admin_name varchar(30) not null default '' comment '管理员账号',
admin_cname varchar(30) not null default '' comment '管理员昵称',
password char(32) not null default '' comment '管理员密码',
sex tinyint unsigned not null default '1' comment '性别 1男 2女',
telephone varchar(12) not null default '11111111111' comment '管理员手机号',
email varchar(20) not null default 'admin@qq.com' comment '管理员邮箱',
add_time timestamp not null default '0000-00-00 00:00:00' comment '添加时间',
login_time timestamp NOT NULL default '0000-00-00 00:00:00' comment '上次登陆时间',
login_num int NOT NULL default '0' comment '登陆次数',
login_ip varchar(16) NOT NULL default '192.168.0.1' comment '最近登陆IP',
status tinyint not null default '1' comment '账号状态 1开启 2关闭',
UNIQUE(admin_name)
)engine=MyISAM charset=utf8;
#插入一条记录作为管理员 用户名和密码均为admin
insert into trkr_admin(admin_name,admin_cname,password,email) values('admin','大叔刘海强','21232f297a57a5a743894a0e4a801fc3','admin@trkr.cn');
#创建后台管理员和角色表 关联表
create table trkr_admin_group(
id int unsigned not null auto_increment primary key comment '管理员ID编号',
admin_id int unsigned not null default '0' comment '管理员表对的ID外键',
auth_group_id int unsigned not null default '0' comment '角色表对应的ID外键'
)engine=MyISAM charset=utf8;
#创建后台管理员角色表
create table trkr_auth_group (
id int unsigned not null auto_increment primary key comment '角色ID编号',
title char(100) NOT NULL DEFAULT ' ' comment '角色标题',
status tinyint(1) NOT NULL DEFAULT '1' comment '是否开启',
rules char(80) NOT NULL DEFAULT ' ' comment '角色节点',
info varchar(30) not null default '' comment '描述'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
#创建后台管理员权限表节点表
create table trkr_auth_rule(
id int unsigned NOT NULL AUTO_INCREMENT primary key comment '节点ID',
name char(80) NOT NULL DEFAULT '' comment'节点名称',
title char(20) NOT NULL DEFAULT '' comment '节点的urk',
pid int unsigned not null default '0' comment '父级分类ID',
type tinyint(1) NOT NULL DEFAULT '1' comment '保留字段类型',
status tinyint(1) NOT NULL DEFAULT '1' comment '状态1 正常 0 关闭',
info varchar(100) NOT NULL DEFAULT ' '
)engine=MyISAM charset=utf8;创建auth方法<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
class Auth {
private $defauth = array(
'index/index',
);
public function check($nodes,$id){
$ca = $this->getca();
$re = $this->getnodelist($nodes);
$is_ok = false;
$def = $this->defauth;
foreach ($def as $v){
if(strtolower($v)==strtolower($ca)){
return true;
}
}
foreach ($re as $v){
if(strtolower($v[0])==strtolower($ca)){
return true;
}
}
if($is_ok==false){
return false;
}
}
//获取当前操作方法
public function getca(){
$c = Request::instance()->controller();
$a = Request::instance()->action();
return $c.'/'.$a;
}
//根据node节点 获取所有的ca
public function getnodelist($nodes){
$nodes = explode(',',$nodes);
foreach ($nodes as $v){
$re[] = Db::table('trkr_auth_rule')->where('id','=',$v)->column('title');
}
return $re;
}
}然后再公共方法内 $auto = new Auth();
$re = $auto->check($nodes,$uid);
if (!$re){
$this->error('你没有操作这个的权限');
} 最佳答案