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

272 lines
8.2 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\System;
use app\admin\BaseController;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Session;
use think\response\Json;
use app\model\System\AdminUser;
use app\model\System\AdminUserGroup;
class RoleController extends BaseController
{
/**
* 获取所有角色列表
* @return Json
*/
public function getAllRoles()
{
$tid = $this->getTenantId();
$roles = AdminUserGroup::where('delete_time', null)
->where(function ($query) use ($tid) {
$query->where('tid', $tid)->whereOr('tid', 0);
})
->order('id', 'asc')
->select();
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $roles->toArray()
]);
}
/**
* 获取角色详情
* @param int $id
* @return Json
*/
public function getRoleById(int $id)
{
$role = AdminUserGroup::where('id', $id)
->where(function ($query) {
$query->where('tid', $this->getTenantId())->whereOr('tid', 0);
})
->where('delete_time', null)
->find();
if (!$role) {
return json([
'code' => 404,
'msg' => '角色不存在',
'data' => null
]);
}
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $role->toArray()
]);
}
/**
* 创建角色
* @return Json
*/
public function createRole()
{
try {
$data = $this->request->param();
// 验证参数
$this->validate($data, [
'name|角色名称' => 'require|max:50',
'status|状态' => 'in:0,1',
'rights|权限' => 'array'
]);
// 检查角色名称是否已存在
$exists = AdminUserGroup::where('name', $data['name'])
->where('tid', $this->getTenantId())
->where('delete_time', null)
->find();
if ($exists) {
return json([
'code' => 400,
'msg' => '角色名称已存在'
]);
}
// 准备数据
$roleData = [
'name' => $data['name'],
'tid' => $this->getTenantId(),
'status' => $data['status'] ?? 1,
'rights' => !empty($data['rights']) ? json_encode($data['rights']) : null,
'create_time' => date('Y-m-d H:i:s'),
'update_time' => date('Y-m-d H:i:s')
];
// 创建角色
$roleModel = new AdminUserGroup();
$roleModel->save($roleData);
// 记录操作日志
$this->logSuccess('角色管理', '创建角色', ['id' => $roleModel->id]);
return json([
'code' => 200,
'msg' => '创建成功',
'data' => $roleModel->toArray()
]);
} catch (ValidateException $e) {
// 记录失败日志
$this->logFail('角色管理', '创建角色', $e->getMessage());
return json([
'code' => 400,
'msg' => $e->getError()
]);
} catch (\Exception $e) {
// 记录失败日志
$this->logFail('角色管理', '创建角色', $e->getMessage());
return json([
'code' => 500,
'msg' => '创建失败:' . $e->getMessage()
]);
}
}
/**
* 更新角色
* @param int $id
* @return Json
*/
public function updateRole(int $id)
{
try {
$data = $this->request->param();
// 验证参数
$this->validate($data, [
'name|角色名称' => 'require|max:50',
'status|状态' => 'in:0,1',
'rights|权限' => 'array'
]);
// 查找角色验证tidtid为0的系统角色也可编辑
$role = AdminUserGroup::where('id', $id)
->where(function ($query) {
$query->where('tid', $this->getTenantId())->whereOr('tid', 0);
})
->where('delete_time', null)
->find();
if (!$role) {
return json([
'code' => 404,
'msg' => '角色不存在'
]);
}
// 检查角色名称是否已被其他角色使用tid为0或当前用户tid的角色
$exists = AdminUserGroup::where('name', $data['name'])
->where('id', '<>', $id)
->where(function ($query) {
$query->where('tid', $this->getTenantId())->whereOr('tid', 0);
})
->where('delete_time', null)
->find();
if ($exists) {
return json([
'code' => 400,
'msg' => '角色名称已存在'
]);
}
// 更新数据
$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' => $updatedRole->toArray()
]);
} catch (ValidateException $e) {
// 记录失败日志
$this->logFail('角色管理', '更新角色', $e->getMessage());
return json([
'code' => 400,
'msg' => $e->getError()
]);
} catch (\Exception $e) {
// 记录失败日志
$this->logFail('角色管理', '更新角色', $e->getMessage());
return json([
'code' => 500,
'msg' => '更新失败:' . $e->getMessage()
]);
}
}
/**
* 删除角色
* @param int $id
* @return Json
*/
public function deleteRole(int $id)
{
try {
// 不允许删除ID为1的超级管理员角色
if ($id == 1) {
return json([
'code' => 400,
'msg' => '不允许删除超级管理员角色'
]);
}
// 查找角色验证tid
$role = AdminUserGroup::where('id', $id)
->where('tid', $this->getTenantId())
->where('delete_time', null)
->find();
if (!$role) {
return json([
'code' => 404,
'msg' => '角色不存在'
]);
}
// 检查是否有用户正在使用该角色
$userCount = AdminUser::where('group_id', $id)
->where('tid', $this->getTenantId())
->where('delete_time', null)
->count();
if ($userCount > 0) {
return json([
'code' => 400,
'msg' => "该角色下还有 {$userCount} 个用户,无法删除"
]);
}
// 软删除
$role->delete();
// 记录操作日志
$this->logSuccess('角色管理', '删除角色', ['id' => $id]);
return json([
'code' => 200,
'msg' => '删除成功'
]);
} catch (\Exception $e) {
// 记录失败日志
$this->logFail('角色管理', '删除角色', $e->getMessage());
return json([
'code' => 500,
'msg' => '删除失败:' . $e->getMessage()
]);
}
}
}