<?php
namespace app\admin\model;
use app\BaseModel;
class AuthRule extends BaseModel
{
public function authPid()
{
return $this->belongsTo('AuthRule', 'pid', 'id');
}
public function authCid()
{
return $this->hasMany('AuthRule', 'pid', 'id');
}
public function search($srcfrom)
{
$src = [
'searchval' => ''
,'status' => ''
];
$src = array_cover($srcfrom, $src);
$data = $this
->when(strlen($src['searchval']) > 0, function($query) use($src){
$query->where('title|name', 'like', '%' . $src['searchval'] . '%');
})
->when(strlen($src['status']) > 0, function($query) use($src){
$query->where('status', $src['status']);
})
->with([
'authPid' => function($query){
$query->field('id, title');
}
])
->field('id, name, title, condition, paixu, ismenu, url, pid, type, status')
->select();
return $data;
}
public function menu($user_id)
{
if($user_id == 1 || $user_id ==2)
{
$auth = array();
}else{
$admin = new \app\admin\model\Admin;
$auth = $admin->srcAuth($user_id);
}
$data = self::where('pid', 0)
->where('status&ismenu', 1)
->when(count($auth) > 0, function($query) use($auth){
$query->where('id', 'in', $auth);
})
->field('id, title, font')
->with([
'authCid' => function($query){
$query->order(['paixu'])
->where('status&ismenu', 1)
->field('id, pid, title, url');
}
])
->order(['paixu'])
->select();
return $data;
}
public function digui($arrAll = array(), $arrSelected = array(), $pid = 0)
{
$child = array();
count($arrSelected) == 0 ? $e = false : $e = true;
foreach ($arrAll as $key => $value) {
# 获取当前子权限
if($value['pid'] == $pid)
{
# 判断当前权限是否被选中
if($e == true)
{
$selk = array_search($value['id'], $arrSelected);
if(false === $selk)
{
$value['select'] = false;
} else {
$value['select'] = true;
unset($arrSelected[$selk]);
}
}else{
$value['select'] = false;
}
$child[$value['id']] = $value;
unset($arrAll[$key]);
$child[$value['id']]['child'] = $this->digui($arrAll, $arrSelected, $value['id']);
}
}
return $child;
}
}