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.

113 lines
3.9 KiB

<?php
namespace app\auth\model;
use think\model\concern\SoftDelete;
class AuthRule extends Base
{
use SoftDelete;
/*
* 获取列表信息
*/
public function getListAll()
{
//获取一级权限
$data = $this->where(array('pid' => 0))->order('sort desc,id asc')->select();
$redata = array();
if (!empty($data)) {
$idData = array();
foreach ($data as $value) {
$idData[] = $value['id'];
}
//获取二级权限
$list = $this->where('type', 2)->whereIn('pid', implode(',', $idData))->order('sort desc,id asc')->select();
$idData1 = array();
foreach ($list as $value) {
$idData1[] = $value['id'];
}
//获取三级权限
$list_next = $this->where('type', 3)->whereIn('pid',implode(',', $idData1))->order('sort desc,id asc')->select();
foreach ($data as $value) {
$temp = array();
$temp['id'] = $value['id'];
$temp['pid'] = $value['pid'];
$temp['title'] = $value['title'];
$temp['rule_val'] = $value['rule_val'];
$temp['rule_url'] = $value['rule_url'];
$temp['icon'] = $value['icon'];
$temp['type'] = $value['type'];
$temp['sort'] = $value['sort'];
$temp['list'] = array();
//写入下一级信息
foreach ($list as $val) {
if ($val['pid'] == $value['id']) {
$temp['list'][] = $val;
}
}
//写入下二级信息
if (!empty($temp['list'])) {
foreach ($temp['list'] as $key => $val) {
$val['next'] = array();
$next = array();
foreach ($list_next as $v) {
if ($v['pid'] == $val['id']) {
$next[] = $v;
}
}
$val['next'] = $next;
$temp['list'][$key] = $val;
}
}
$redata[] = $temp;
}
}
return $redata;
}
/*
* 获取等级数据(用于角色授权页面)
*/
public function getLevelData()
{
$data = $this->where(['uid' => $this->mid,'is_sub_visible'=>1])->order('pid asc, sort desc,id asc')->select();
if (empty($data)) {
return $data;
}
$ret = [];
$thridarr = [];
foreach ($data as $val) {
if ($val['type'] == 3) {
$temp = ['id' => $val->id, 'title' => $val->title, 'pid' => $val->pid, 'rule_val' => $val->rule_val];
if (!isset($thridarr[$val->pid])) {
$thridarr[$val->pid][] = $temp;
} else {
array_push($thridarr[$val->pid], $temp);
}
}
if ($val->pid == 0) {
$ret[$val->id] = ['id' => $val->id, 'title' => $val->title, 'pid' => $val->pid, 'rule_val' => $val->rule_val];
} elseif (isset($ret[$val->pid])) {
$ret[$val->pid]['children'][$val->id] = ['id' => $val->id, 'title' => $val->title, 'pid' => $val->pid, 'rule_val' => $val->rule_val];
}
}
foreach ($ret as $key => $value) {
if (!empty($value['children'])) {
foreach ($value['children'] as $k => $val) {
foreach ($thridarr as $kk => $vv) {
if ($k == $kk) {
$val['next'] = $vv;
}
}
$value['children'][$k] = $val;
}
}
$ret[$key] = $value;
}
return $ret;
}
}