Files

205 lines
6.3 KiB
PHP
Raw Permalink Normal View History

2025-08-07 20:21:47 +08:00
<?php
namespace app\adminapi\controller;
use app\admin\model\AuthRule;
use app\common\controller\adminApi;
use app\common\controller\Backend;
use fast\Tree;
use think\Cache;
/**
* 规则管理
*
* @icon fa fa-list
* @remark 规则通常对应一个控制器的方法,同时左侧的菜单栏数据也从规则中体现,通常建议通过控制台进行生成规则节点
*/
class Rule extends adminApi
{
/**
* @var \app\admin\model\AuthRule
*/
protected $model = null;
protected $rulelist = [];
protected $multiFields = 'ismenu,status';
public function _initialize()
{
parent::_initialize();
// if (!$this->auth->isSuperAdmin()) {
// $this->error(__('Access is allowed only to the super management group'));
// }
$this->model = model('app\admin\model\AuthRule');
// 必须将结果集转换为数组
$ruleList = \think\Db::name("auth_rule")->field('type,condition,remark,createtime,updatetime', true)->order('weigh DESC,id ASC')->select();
foreach ($ruleList as $k => &$v) {
$v['title'] = __($v['title']);
}
unset($v);
Tree::instance()->init($ruleList);
$this->rulelist = Tree::instance()->getTreeArray(0);
$ruledata = [0 => __('None')];
foreach ($this->rulelist as $k => &$v) {
if (!$v['ismenu']) {
continue;
}
$ruledata[$v['id']] = $v['title'];
unset($v['spacer']);
}
unset($v);
}
/*
* 菜单接口
*/
public function menus(){
$ruleList = Tree::instance()->getTreeArray(0);
$ruleList = $this->getTree($ruleList,['title','url','name','icon','extend']);
return V(1,"系统菜单接口", $ruleList);
}
public function getTree($ruleList,$fieldData=[]){
$ruledata = [];
$i= 0;
foreach ($ruleList as $k => &$v) {
if (!$v['ismenu']) {
continue;
}
if(!$this->auth->check($v['name'])){
continue;
}
foreach ($fieldData as $key => $value) {
$ruledata[$i][$value] = $v[$value];
}
if(!empty($v['childlist'])){
$ruledata[$i]['childlist'] = $this->getTree($v['childlist'],$fieldData);
if(empty($ruledata[$i]['childlist'])){
unset($ruledata[$i]['childlist']);
}
}
$i++;
}
return $ruledata;
}
/*
* 菜单列表(添加时选择父级菜单用)
*/
public function list(){
$ruledata = [0 => ""];
foreach ($this->rulelist as $k => &$v) {
if (!$v['ismenu']) {
continue;
}
$ruledata[$v['id']] = $v['title'];
unset($v['spacer']);
}
unset($v);
return V(1,"系统菜单接口", $ruledata);
}
/**
* 查看
*/
public function index()
{
$list = $this->rulelist;
$total = count($this->rulelist);
$result = array("total" => $total, "rows" => $list);
return V(1,"菜单规则列表", $result);
}
/**
* 添加
*/
public function add()
{
if ($this->request->isPost()) {
// $this->token();
$params = $this->request->post();
if ($params) {
if (!$params['ismenu'] && !$params['pid']) {
return V(0,'非菜单规则节点必须有父级', null);
}
$result = $this->model->validate('app\admin\validate\AuthRule')->save($params);
if ($result === false) {
return V(0,'操作失败', null);
}
return V(1,'操作成功', null);
}
return V(0,'操作失败', null);
}
return V(0,'操作失败', null);
}
/**
* 编辑
*/
public function edit($ids = NULL)
{
if ($this->request->isPost()) {
// $this->token();
$params = $this->request->post();
if ($params) {
if (!$params['ismenu'] && !$params['pid']) {
return V(0,'非菜单规则节点必须有父级', null);
}
if ($params['pid'] == $params['id']) {
return V(0,'父级不能是它自己', null);
}
if ($params['pid'] != $params['pid']) {
$childrenIds = Tree::instance()->init(collection(AuthRule::select())->toArray())->getChildrenIds($params['id']);
if (in_array($params['pid'], $childrenIds)) {
return V(0,'父组别不能是它的子组别', null);
}
}
//这里需要针对name做唯一验证
$ruleValidate = \think\Loader::validate('app\admin\validate\AuthRule');
$ruleValidate->rule([
'name' => 'require|unique:AuthRule,name,' . $params['id'],
]);
$result = $this->model->validate('app\admin\validate\AuthRule')->save($params, $params['id']);
if ($result === false) {
return V(0,'操作失败', null);
}
return V(1,'操作成功', null);
}
return V(0,'操作失败', null);
}
}
/*
* 详情
*/
public function detail(){
$id = $this->request->param('id');
$row = $this->model->get($id);
if (!$row) {
return V(0,'操作失败', null);
}
return V(1,"详情", $row);
}
/**
* 删除
*/
public function del($ids = "")
{
$ids = input('ids', '');
if ($ids) {
$delIds = [];
foreach (explode(',', $ids) as $k => $v) {
$delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, true));
}
$delIds = array_unique($delIds);
$count = $this->model->where('id', 'in', $delIds)->delete();
if ($count) {
Cache::rm('__menu__');
return V(1,'操作成功', null);return V(1,'操作成功', null);
}
}
return V(0,'操作失败', null);
}
}