批量更新租户规则
This commit is contained in:
@@ -0,0 +1,260 @@
|
||||
<?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('tid', $this->getTenantId())
|
||||
->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'
|
||||
]);
|
||||
|
||||
// 查找角色(验证tid)
|
||||
$role = AdminUserGroup::where('id', $id)
|
||||
->where('tid', $this->getTenantId())
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
if (!$role) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '角色不存在'
|
||||
]);
|
||||
}
|
||||
|
||||
// 检查角色名称是否已被其他角色使用
|
||||
$exists = AdminUserGroup::where('name', $data['name'])
|
||||
->where('id', '<>', $id)
|
||||
->where('tid', $this->getTenantId())
|
||||
->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' => '不允许删除超级管理员角色'
|
||||
]);
|
||||
}
|
||||
|
||||
// 查找角色(验证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()
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user