tp/app/admin/controller/RoleController.php
2026-01-26 09:29:36 +08:00

250 lines
7.1 KiB
PHP

<?php
declare(strict_types=1);
namespace app\admin\controller;
use app\admin\BaseController;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Session;
use think\response\Json;
use app\model\AdminUser;
use app\model\AdminUserGroup;
class RoleController extends BaseController
{
/**
* 获取所有角色列表
* @return Json
*/
public function getAllRoles()
{
$roles = AdminUserGroup::where('delete_time', null)
->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('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('delete_time', null)
->find();
if ($exists) {
return json([
'code' => 400,
'msg' => '角色名称已存在'
]);
}
// 准备数据
$roleData = [
'name' => $data['name'],
'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'
]);
// 查找角色
$role = AdminUserGroup::where('id', $id)
->where('delete_time', null)
->find();
if (!$role) {
return json([
'code' => 404,
'msg' => '角色不存在'
]);
}
// 检查角色名称是否已被其他角色使用
$exists = AdminUserGroup::where('name', $data['name'])
->where('id', '<>', $id)
->where('delete_time', null)
->find();
if ($exists) {
return json([
'code' => 400,
'msg' => '角色名称已存在'
]);
}
// 更新数据
$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();
// 记录操作日志
$this->logSuccess('角色管理', '更新角色', ['id' => $id]);
return json([
'code' => 200,
'msg' => '更新成功',
'data' => $role->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' => '不允许删除超级管理员角色'
]);
}
// 查找角色
$role = AdminUserGroup::where('id', $id)
->where('delete_time', null)
->find();
if (!$role) {
return json([
'code' => 404,
'msg' => '角色不存在'
]);
}
// 检查是否有用户正在使用该角色
$userCount = AdminUser::where('group_id', $id)
->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()
]);
}
}
}