* Date: 2018-4-3
*/
namespace app\admin\controller;
use think\Page;
use think\Db;
use think\Validate;
class AuthRole extends Base {
public function _initialize() {
parent::_initialize();
$this->language_access(); // 多语言功能操作权限
}
/**
* 权限组管理
*/
public function index()
{
$map = array();
$pid = input('pid/d');
$keywords = input('keywords/s');
$keywords = addslashes(trim($keywords));
if (!empty($keywords)) {
$map['c.name'] = array('LIKE', "%{$keywords}%");
}
$AuthRole = Db::name('auth_role');
$count = $AuthRole->alias('c')->where($map)->count();// 查询满足要求的总记录数
$Page = new Page($count, config('paginate.list_rows'));// 实例化分页类 传入总记录数和每页显示的记录数
$fields = "c.*,s.name AS pname";
$list = DB::name('auth_role')
->field($fields)
->alias('c')
->join('__AUTH_ROLE__ s','s.id = c.pid','LEFT')
->where($map)
->order('c.id asc')
->limit($Page->firstRow.','.$Page->listRows)
->select();
$show = $Page->show();// 分页显示输出
$this->assign('page',$show);// 赋值分页输出
$this->assign('list',$list);// 赋值数据集
$this->assign('pager',$Page);// 赋值分页集
return $this->fetch();
}
/**
* 新增权限组
*/
public function add()
{
if (IS_POST) {
$rule = array(
'name' => 'require',
);
$msg = array(
'name.require' => '权限组名称不能为空!',
);
$data = array(
'name' => trim(input('name/s')),
);
$validate = new Validate($rule, $msg);
$result = $validate->check($data);
if(!$result){
$this->error($validate->getError());
}
$model = model('AuthRole');
$count = $model->where('name', $data['name'])->count();
if(! empty($count)){
$this->error('该权限组名称已存在,请检查');
}
$role_id = $model->saveAuthRole(input());
if($role_id){
adminLog('新增权限组:'.$data['name']);
$admin_role_list = model('AuthRole')->getRoleAll();
$this->success('操作成功', url('AuthRole/index'), ['role_id'=>$role_id,'role_name'=>$data['name'],'admin_role_list'=>json_encode($admin_role_list)]);
}else{
$this->error('操作失败');
}
}
// 权限组
$admin_role_list = model('AuthRole')->getRoleAll();
$this->assign('admin_role_list', $admin_role_list);
// 模块组
$modules = getAllMenu();
$this->assign('modules', $modules);
// 权限集
// $singleArr = array_multi2single($modules, 'child'); // 多维数组转为一维
$auth_rules = get_auth_rule(['is_modules'=>1]);
$auth_rule_list = group_same_key($auth_rules, 'menu_id');
foreach ($auth_rule_list as $key => $val) {
if (is_array($val)) {
$sort_order = [];
foreach ($val as $_k => $_v) {
$sort_order[$_k] = $_v['sort_order'];
}
array_multisort($sort_order, SORT_ASC, $val);
$auth_rule_list[$key] = $val;
}
}
$this->assign('auth_rule_list', $auth_rule_list);
// 栏目
$arctype_list = Db::name('arctype')->where([
'is_del' => 0,
])->order("grade desc")->select();
$arctype_p_html = $arctype_child_html = "";
$arctype_all = list_to_tree($arctype_list);
foreach ($arctype_all as $key => $arctype) {
if (!empty($arctype['children'])) {
if ($key > 0) {
$arctype_p_html .= '';
} else {
$arctype_p_html .= '';
}
$arctype_child_html .= '
get_arctype_child_html($arctype);
$arctype_child_html .= '
';
}
$arctype_p_html .= '