You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
10 KiB

<?php
namespace app\auth\logic;
use app\auth\model\AuthGroupRule;
use app\auth\model\AuthRule;
use app\auth\model\AuthRuleParam;
class Auth extends Base
{
public function getMenu($controller_name,$action_name)
{
$json_string = file_get_contents('menu.json');
$menu = json_decode($json_string, true);
$login_er = serializeMysql(session('login_er'),1);
if(!$login_er){
return $login_er;
}
$index = strpos($action_name, 'indie'); //权限控制
if($login_er['rank'] > 0 || $index){
$auth_rule_model = new AuthRule();
//获取角色授权id列表
$auth_group_rule_model = new AuthGroupRule();
$rule_data = $auth_group_rule_model->where(['role_id' => $login_er['role_id']])->column('rule_id');
$group = $auth_rule_model->field('rule_val,group_name')->where([['id','in',$rule_data], ['type','<>',1]])->select();
$controller_group = [];
$auth_group = [];
foreach ($group as $key => $value) {
$auth_group[] = $value['group_name'];
$controller_group[$value['group_name']][] = $value['rule_val'];
}
$auth_group = array_unique($auth_group);
foreach ($menu as $key => &$value) {
if (!in_array($value['group'], $auth_group)) {
unset($menu[$key]);
} else {
if (empty($value['_child'])) { //只有一级
if (!in_array($value['url'], $controller_group[$value['group']])) {
unset($menu[$key]);
}
foreach ($value['_auth'] as $k3 => $v3) {
if (!in_array($v3['url'], $controller_group[$value['group']])) {
unset($menu[$key]['_auth'][$k3]);
}
}
} else {
foreach ($value['_child'] as $key1 => &$val) {
if (!in_array($val['url'], $controller_group[$value['group']])) {
unset($menu[$key]['_child'][$key1]);
}
foreach ($val['_auth'] as $k1 => $v1) {
if (!in_array($v1['url'], $controller_group[$value['group']])) {
unset($menu[$key]['_child'][$key1]['_auth'][$k1]);
unset($menu[$key]['_child'][$key1]['_auth'][$k1]);
}
}
}
}
}
}
}
//如果是总账号要把不让总账号可见的权限剔除
if($login_er['rank'] === 0){
foreach ($menu as $key => &$value) {
if ($value['is_total_visible'] == 0) {
unset($menu[$key]);
} else {
if (!empty($value['_child'])) { //只有一级
foreach ($value['_child'] as $key1 => &$val) {
if ($val['is_total_visible'] == 0) {
unset($menu[$key]['_child'][$key1]);
}
// foreach ($val['_auth'] as $k1 => $v1) {
// if (!in_array($v1['url'], $controller_group[$value['group']])) {
// unset($menu[$key]['_child'][$key1]['_auth'][$k1]);
// unset($menu[$key]['_child'][$key1]['_auth'][$k1]);
// }
// }
}
}
}
}
}
$group_name = '';
$auth_name = [];
$three_level = [];
foreach ($menu as $key1 => &$menu1) {
if (empty($menu1['_child'])) { //一级为最后一级
$param_base = empty($menu1['param']) ? [] : $menu1['param'];
$param_r = [];
if (!empty($menu1['_param'])) {
$param_r = ['r' => $menu1['_param'][0]['r']];
}
if (!array_key_exists('url', $menu1)) {
continue;
}
$menu1['linkurl'] = getMenuLinkurl($menu1['module'],$menu1['group'], $menu1['url'], array_merge($param_base, $param_r));
if ($controller_name == $menu1['group']) {
$menu1['cur'] = 1; //一级高亮
$group_name = $menu1['url']; //当前组和权限组
$urls = array_column($menu1['_auth'], 'url');
$auth_name = array_merge([$menu1['url']], $urls);
if (!empty($menu1['_param'])) {
$three_level = $menu1['_param'];
$r = input('param.r');
foreach ($three_level as $k => $v) {
$three_level[$k]['linkurl'] = getMenuLinkurl($menu1['module'],$menu1['group'], $menu1['url'], array_merge($param_base, ['r' => $v['r']]));
if ($r == $v['r']) {
$three_level[$k]['cur'] = 1;
}
}
}
}
} else {
foreach ($menu1['_child'] as $key2 => &$menu2) {
//二级是最后一级
$param_base = empty($menu2['param']) ? [] : $menu2['param'];
$param_r = [];
if (!empty($menu2['_param'])) {
$param_r = ['r' => $menu2['_param'][0]['r']];
}
if (!array_key_exists('url', $menu2)) {
continue;
}
$menu2['linkurl'] = getMenuLinkurl($menu1['module'],$menu1['group'], $menu2['url'], array_merge($param_base, $param_r));
$urls = array_column($menu2['_auth'], 'url');
$arr = array_merge([$menu2['url']], $urls);
if ($controller_name == $menu1['group'] && in_array($action_name, $arr)) {
$menu1['cur'] = 1; //一级高亮
$menu2['cur'] = 1; //二级高亮
$group_name = $menu2['url']; //当前组和权限组
$auth_name = $arr;
if (!empty($menu2['_param'])) {
$three_level = $menu2['_param'];
$r = input('param.r');
foreach ($three_level as $k => $v) {
$three_level[$k]['linkurl'] = getMenuLinkurl($menu1['module'],$menu1['group'], $menu2['url'], array_merge($param_base, ['r' => $v['r']]));
if ($r == $v['r']) {
$three_level[$k]['cur'] = 1;
}
}
}
}
}
}
}
// dump($menu);
// exit();
return ['menu'=>$menu,'group_name'=>$group_name,'auth_name'=>$auth_name,'three_level'=>$three_level];
}
/*
* 生成menu.json文件
*/
public function createMenu()
{
//获取一级权限
$auth_rule_model = new AuthRule();
$auth_rule_param_model = new AuthRuleParam();
$data = $auth_rule_model->where(array('pid' => 0, 'uid' => UID))->order('sort desc,id asc')->select();
$redata = array();
if (!empty($data)) {
$idData = array();
foreach ($data as $value) {
$idData[] = $value['id'];
}
//dump($idData);
//获取二级权限
$where = [];
$where[] = ['pid', 'in', $idData];
$where[] = ['type', '=', 2];
$where[] = ['uid', '=', UID];
$list = $auth_rule_model
->where(
$where
)
->order('sort desc,id asc')
// ->fetchSql(true)
->select();
//dump($list);
$idData1 = array();
foreach ($list as $value) {
$idData1[] = $value['id'];
}
//获取三级权限
$list_next = $auth_rule_model->where(
array(['pid', "IN", $idData1], ['type', '=', 3], ['uid', '=', UID])
)->order('sort desc,id asc')->select();
//获取参数
$authParam = $auth_rule_param_model->getListByRule();
$two_level_list = [];
//拼装二级权限数组
foreach ($list as $value) {
$temp = array();
$temp['id'] = $value['id'];
$temp['pid'] = $value['pid'];
$temp['name'] = $value['title'];
$temp['url'] = $value['rule_val'];
$temp['is_total_visible'] = $value['is_total_visible'];
$temp['_auth'] = [];
foreach ($list_next as $val) {
if ($val['pid'] == $value['id']) {
$tem = [
'name' => $val['title'],
'url' => $val['rule_val']
];
$temp['_auth'][] = $tem;
}
}
if (isset($authParam[$value['id']])) {
$temp['_param'] = $authParam[$value['id']];
}
$two_level_list[] = $temp;
}
//拼装权限组数组
foreach ($data as $value) {
$temp = array();
$temp['module'] = $value['module'];
$temp['group'] = $value['rule_val'];
$temp['icon'] = $value['icon'];
$temp['name'] = $value['title'];
$temp['is_total_visible'] = $value['is_total_visible'];
$temp['_child'] = [];
foreach ($two_level_list as $val) {
if ($val['pid'] == $value['id']) {
unset($val['id']);
unset($val['pid']);
$temp['_child'][] = $val;
}
}
$redata[] = $temp;
}
}
$json_string = json_encode($redata);
$res = file_put_contents('menu.json', $json_string);
if ($res == FALSE) {
return sendArray([], 101, '生成失败');
}
return sendArray();
}
}