修复若干bug

This commit is contained in:
2026-03-10 22:24:06 +08:00
parent 7cdb591f69
commit 98156991c1
16 changed files with 1021 additions and 892 deletions
+39 -1
View File
@@ -100,10 +100,13 @@ class MenuController extends BaseController
]);
}
// 获取有权限的菜单及其所有父级菜单
$allMenuIds = $this->getMenuIdsWithParents($menuIds);
// 获取有权限的菜单
$menus = SystemMenu::where('delete_time', null)
->where('status', 1)
->whereIn('id', $menuIds)
->whereIn('id', $allMenuIds)
->field('id,pid,title,path,component_path,icon,sort')
->order('sort', 'asc')
->select();
@@ -489,6 +492,41 @@ class MenuController extends BaseController
}
}
/**
* 获取菜单ID及其所有父级菜单ID
* @param array $menuIds 菜单ID数组
* @return array 包含所有父级菜单的ID数组
*/
private function getMenuIdsWithParents(array $menuIds): array
{
if (empty($menuIds)) {
return [];
}
$allIds = $menuIds;
$toCheck = $menuIds;
while (!empty($toCheck)) {
// 查询这些菜单的父级
$parents = SystemMenu::whereIn('id', $toCheck)
->where('delete_time', null)
->column('pid');
// 过滤掉已经存在的和为0的父ID
$newParents = [];
foreach ($parents as $pid) {
if ($pid > 0 && !in_array($pid, $allIds)) {
$newParents[] = $pid;
$allIds[] = $pid;
}
}
$toCheck = $newParents;
}
return $allIds;
}
/**
* 构建菜单树形结构
* @param array $menus 菜单列表
+69 -5
View File
@@ -8,6 +8,8 @@ use app\admin\BaseController;
use think\response\Json;
use app\model\AdminModules;
use app\model\System\AdminUserGroup;
use app\model\SystemMenu;
use Symfony\Component\VarDumper\VarDumper;
class ModulesController extends BaseController
{
@@ -52,10 +54,9 @@ class ModulesController extends BaseController
{
try {
$userInfo = $this->getAdminUserInfo();
$groupId = $userInfo['group_id'] ?? null;
$menuIds = [];
if ($groupId) {
$userGroup = AdminUserGroup::where('id', $groupId)->find();
if ($userGroup && $userGroup->rights) {
@@ -64,7 +65,7 @@ class ModulesController extends BaseController
$menuIds = is_array($decoded) ? $decoded : [];
}
}
if (empty($menuIds)) {
return json([
'code' => 200,
@@ -75,9 +76,14 @@ class ModulesController extends BaseController
]
]);
}
// 根据子菜单ID查询所有父级模块ID
$moduleIds = $this->getModuleIdsByMenuIds($menuIds);
$modules = AdminModules::where('delete_time', null)
->whereIn('mid', $menuIds)
->whereIn('id', $moduleIds)
->where('status', 1)
->where('is_show', 1)
->order('sort', 'asc')
->order('id', 'asc')
->select()
@@ -386,6 +392,64 @@ class ModulesController extends BaseController
}
}
/**
* 根据菜单ID列表获取对应的模块ID列表(包括父级模块)
* @param array $menuIds 菜单ID列表(子菜单)
* @return array 模块ID列表
*/
private function getModuleIdsByMenuIds(array $menuIds): array
{
if (empty($menuIds)) {
return [];
}
// 1. 获取所有子菜单及其父级菜单ID
$allMenuIds = $this->getMenuIdsWithParents($menuIds);
// 2. 获取所有匹配的模块(mid在allMenuIds中)
$moduleIds = AdminModules::where('delete_time', null)
->whereIn('mid', $allMenuIds)
->column('id');
// 去重并返回
return array_unique($moduleIds);
}
/**
* 获取菜单ID及其所有父级菜单ID
* @param array $menuIds 菜单ID数组
* @return array 包含所有父级菜单的ID数组
*/
private function getMenuIdsWithParents(array $menuIds): array
{
if (empty($menuIds)) {
return [];
}
$allIds = $menuIds;
$toCheck = $menuIds;
while (!empty($toCheck)) {
// 查询这些菜单的父级
$parents = SystemMenu::whereIn('id', $toCheck)
->where('delete_time', null)
->column('pid');
// 过滤掉已经存在的和为0的父ID
$newParents = [];
foreach ($parents as $pid) {
if ($pid > 0 && !in_array($pid, $allIds)) {
$newParents[] = $pid;
$allIds[] = $pid;
}
}
$toCheck = $newParents;
}
return $allIds;
}
/**
* 获取模块选择列表
* @return Json
+22 -11
View File
@@ -43,7 +43,9 @@ class RoleController extends BaseController
public function getRoleById(int $id)
{
$role = AdminUserGroup::where('id', $id)
->where('tid', $this->getTenantId())
->where(function ($query) {
$query->where('tid', $this->getTenantId())->whereOr('tid', 0);
})
->where('delete_time', null)
->find();
if (!$role) {
@@ -143,9 +145,11 @@ class RoleController extends BaseController
'rights|权限' => 'array'
]);
// 查找角色(验证tid
// 查找角色(验证tidtid为0的系统角色也可编辑
$role = AdminUserGroup::where('id', $id)
->where('tid', $this->getTenantId())
->where(function ($query) {
$query->where('tid', $this->getTenantId())->whereOr('tid', 0);
})
->where('delete_time', null)
->find();
if (!$role) {
@@ -155,10 +159,12 @@ class RoleController extends BaseController
]);
}
// 检查角色名称是否已被其他角色使用
// 检查角色名称是否已被其他角色使用tid为0或当前用户tid的角色)
$exists = AdminUserGroup::where('name', $data['name'])
->where('id', '<>', $id)
->where('tid', $this->getTenantId())
->where(function ($query) {
$query->where('tid', $this->getTenantId())->whereOr('tid', 0);
})
->where('delete_time', null)
->find();
if ($exists) {
@@ -169,18 +175,23 @@ class RoleController extends BaseController
}
// 更新数据
$role->name = $data['name'];
$role->status = $data['status'] ?? 1;
$role->rights = !empty($data['rights']) ? json_encode($data['rights']) : null;
$role->update_time = date('Y-m-d H:i:s');
$role->save();
$updateData = [
'name' => $data['name'],
'status' => $data['status'] ?? 1,
'rights' => !empty($data['rights']) ? json_encode($data['rights']) : null,
'update_time' => date('Y-m-d H:i:s')
];
AdminUserGroup::where('id', $id)->update($updateData);
// 重新查询获取最新数据
$updatedRole = AdminUserGroup::where('id', $id)->find();
// 记录操作日志
$this->logSuccess('角色管理', '更新角色', ['id' => $id]);
return json([
'code' => 200,
'msg' => '更新成功',
'data' => $role->toArray()
'data' => $updatedRole->toArray()
]);
} catch (ValidateException $e) {
// 记录失败日志
+2 -1
View File
@@ -5,8 +5,9 @@ return [
// \think\middleware\CheckRequestCache::class,
// 多语言加载
// \think\middleware\LoadLangPack::class,
// Session初始化
// 跨域支持
\app\common\middleware\AllowCrossDomain::class,
// Session初始化
\think\middleware\SessionInit::class,
];