tp/app/admin/controller/ModulesController.php
2026-03-10 22:24:06 +08:00

480 lines
14 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace app\admin\controller;
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
{
/**
* 获取所有模块列表
* @return Json
*/
public function getList()
{
try {
$modules = AdminModules::where('delete_time', null)
->order('sort', 'asc')
->order('id', 'asc')
->select()
->toArray();
$this->logSuccess('模块管理', '获取模块列表', ['count' => count($modules)]);
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'list' => $modules,
'total' => count($modules)
]
]);
} catch (\Exception $e) {
$this->logFail('模块管理', '获取模块列表', $e->getMessage());
return json([
'code' => 500,
'msg' => '获取失败:' . $e->getMessage(),
'data' => []
]);
}
}
/**
* 获取租户模块列表
* @return Json
*/
public function getTenantList()
{
try {
$userInfo = $this->getAdminUserInfo();
$groupId = $userInfo['group_id'] ?? null;
$menuIds = [];
if ($groupId) {
$userGroup = AdminUserGroup::where('id', $groupId)->find();
if ($userGroup && $userGroup->rights) {
$rights = $userGroup->rights;
$decoded = json_decode($rights, true);
$menuIds = is_array($decoded) ? $decoded : [];
}
}
if (empty($menuIds)) {
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'list' => [],
'total' => 0
]
]);
}
// 根据子菜单ID查询所有父级模块ID
$moduleIds = $this->getModuleIdsByMenuIds($menuIds);
$modules = AdminModules::where('delete_time', null)
->whereIn('id', $moduleIds)
->where('status', 1)
->where('is_show', 1)
->order('sort', 'asc')
->order('id', 'asc')
->select()
->toArray();
$this->logSuccess('模块管理', '获取模块列表', ['count' => count($modules)]);
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'list' => $modules,
'total' => count($modules)
]
]);
} catch (\Exception $e) {
$this->logFail('模块管理', '获取模块列表', $e->getMessage());
return json([
'code' => 500,
'msg' => '获取失败:' . $e->getMessage(),
'data' => []
]);
}
}
/**
* 获取模块详情
* @return Json
*/
public function getDetail($id)
{
try {
$id = (int) $id;
$module = AdminModules::where('id', $id)
->where('delete_time', null)
->find();
if (!$module) {
return json([
'code' => 404,
'msg' => '模块不存在',
'data' => null
]);
}
$this->logSuccess('模块管理', '获取模块详情', ['id' => $id]);
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $module
]);
} catch (\Exception $e) {
$this->logFail('模块管理', '获取模块详情', $e->getMessage());
return json([
'code' => 500,
'msg' => '获取失败:' . $e->getMessage(),
'data' => null
]);
}
}
/**
* 添加模块
* @return Json
*/
public function add()
{
try {
$data = [
'name' => input('post.name', '', 'trim'),
'code' => input('post.code', '', 'trim'),
'description' => input('post.description', '', 'trim'),
'icon' => input('post.icon', '', 'trim'),
'path' => input('post.path', '', 'trim'),
'type' => input('post.type', '', 'trim'),
'sort' => (int)input('post.sort', 0),
'status' => (int)input('post.status', 1),
'is_show' => (int)input('post.is_show', 1),
'create_time' => date('Y-m-d H:i:s'),
'update_time' => date('Y-m-d H:i:s')
];
if (empty($data['name']) || empty($data['code'])) {
return json([
'code' => 400,
'msg' => '模块名称和编码不能为空',
'data' => null
]);
}
$exists = AdminModules::where('code', $data['code'])
->where('delete_time', null)
->find();
if ($exists) {
return json([
'code' => 400,
'msg' => '模块编码已存在',
'data' => null
]);
}
$id = AdminModules::insertGetId($data);
$this->logSuccess('模块管理', '添加模块', ['id' => $id, 'name' => $data['name']]);
return json([
'code' => 200,
'msg' => '添加成功',
'data' => ['id' => $id]
]);
} catch (\Exception $e) {
$this->logFail('模块管理', '添加模块', $e->getMessage());
return json([
'code' => 500,
'msg' => '添加失败:' . $e->getMessage(),
'data' => null
]);
}
}
/**
* 编辑模块
* @return Json
*/
public function edit()
{
try {
$id = (int)input('post.id', 0);
if (empty($id)) {
return json([
'code' => 400,
'msg' => '参数错误',
'data' => null
]);
}
$data = [
'name' => input('post.name', '', 'trim'),
'code' => input('post.code', '', 'trim'),
'description' => input('post.description', '', 'trim'),
'icon' => input('post.icon', '', 'trim'),
'path' => input('post.path', '', 'trim'),
'type' => input('post.type', '', 'trim'),
'sort' => (int)input('post.sort', 0),
'status' => (int)input('post.status', 1),
'is_show' => (int)input('post.is_show', 1),
'update_time' => date('Y-m-d H:i:s')
];
if (empty($data['name']) || empty($data['code'])) {
return json([
'code' => 400,
'msg' => '模块名称和编码不能为空',
'data' => null
]);
}
$exists = AdminModules::where('code', $data['code'])
->where('id', '<>', $id)
->where('delete_time', null)
->find();
if ($exists) {
return json([
'code' => 400,
'msg' => '模块编码已存在',
'data' => null
]);
}
AdminModules::where('id', $id)->update($data);
$this->logSuccess('模块管理', '编辑模块', ['id' => $id, 'name' => $data['name']]);
return json([
'code' => 200,
'msg' => '编辑成功',
'data' => null
]);
} catch (\Exception $e) {
$this->logFail('模块管理', '编辑模块', $e->getMessage());
return json([
'code' => 500,
'msg' => '编辑失败:' . $e->getMessage(),
'data' => null
]);
}
}
/**
* 删除模块
* @return Json
*/
public function delete(int $id)
{
try {
if (empty($id)) {
return json([
'code' => 400,
'msg' => '参数错误',
'data' => null
]);
}
AdminModules::where('id', $id)->update([
'delete_time' => date('Y-m-d H:i:s')
]);
$this->logSuccess('模块管理', '删除模块', ['id' => $id]);
return json([
'code' => 200,
'msg' => '删除成功',
'data' => null
]);
} catch (\Exception $e) {
$this->logFail('模块管理', '删除模块', $e->getMessage());
return json([
'code' => 500,
'msg' => '删除失败:' . $e->getMessage(),
'data' => null
]);
}
}
/**
* 批量删除模块
* @return Json
*/
public function batchDelete()
{
try {
$ids = input('post.ids', []);
$ids = array_map('intval', $ids);
if (empty($ids)) {
return json([
'code' => 400,
'msg' => '请选择要删除的模块',
'data' => null
]);
}
AdminModules::whereIn('id', $ids)
->update([
'delete_time' => date('Y-m-d H:i:s')
]);
$this->logSuccess('模块管理', '批量删除模块', ['ids' => $ids]);
return json([
'code' => 200,
'msg' => '批量删除成功',
'data' => null
]);
} catch (\Exception $e) {
$this->logFail('模块管理', '批量删除模块', $e->getMessage());
return json([
'code' => 500,
'msg' => '批量删除失败:' . $e->getMessage(),
'data' => null
]);
}
}
/**
* 修改状态
* @return Json
*/
public function changeStatus()
{
try {
$id = (int)input('post.id', 0);
$status = (int)input('post.status', 0);
if (empty($id)) {
return json([
'code' => 400,
'msg' => '参数错误',
'data' => null
]);
}
AdminModules::where('id', $id)->update([
'status' => $status,
'update_time' => date('Y-m-d H:i:s')
]);
$this->logSuccess('模块管理', '修改状态', ['id' => $id, 'status' => $status]);
return json([
'code' => 200,
'msg' => '状态修改成功',
'data' => null
]);
} catch (\Exception $e) {
$this->logFail('模块管理', '修改状态', $e->getMessage());
return json([
'code' => 500,
'msg' => '状态修改失败:' . $e->getMessage(),
'data' => null
]);
}
}
/**
* 根据菜单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
*/
public function getSelectList()
{
try {
$modules = AdminModules::where('delete_time', null)
->where('status', 1)
->where('is_show', 1)
->order('sort', 'asc')
->column('id, name, code');
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $modules
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => '获取失败:' . $e->getMessage(),
'data' => []
]);
}
}
}