|
|
@@ -55,22 +55,26 @@ class UserRole extends BaseController
|
|
|
{
|
|
|
$userInfo = $this->request->userInfo;
|
|
|
|
|
|
+ // 获取请求数据
|
|
|
$data = Request::only(['role_name', 'privileges']);
|
|
|
+ $data['merchant_id'] = $userInfo['merchant_id'];
|
|
|
|
|
|
- if (empty($data['role_name'])) {
|
|
|
- return json_error([], '角色名称不能为空');
|
|
|
- }
|
|
|
-
|
|
|
- if (empty($data['privileges']) || !is_array($data['privileges'])) {
|
|
|
- return json_error([], '权限配置不能为空');
|
|
|
+ // 使用验证器进行字段验证
|
|
|
+ try {
|
|
|
+ $validate = validate(UserRoleValidate::class);
|
|
|
+ if (!$validate->scene('create')->check($data)) {
|
|
|
+ return json_error([], $validate->getError());
|
|
|
+ }
|
|
|
+ } catch (\think\exception\ValidateException $e) {
|
|
|
+ return json_error([], $e->getMessage());
|
|
|
}
|
|
|
|
|
|
+ // 额外的业务逻辑验证
|
|
|
+ // 检查角色名称是否已存在
|
|
|
if (UserRoleModel::checkRoleExists($data['role_name'], $userInfo['merchant_id'])) {
|
|
|
return json_error([], '角色名称已存在');
|
|
|
}
|
|
|
-
|
|
|
- $data['merchant_id'] = $userInfo['merchant_id'];
|
|
|
-
|
|
|
+
|
|
|
try {
|
|
|
$role = UserRoleModel::createRole($data);
|
|
|
return json_success(['id' => $role->id], '创建角色成功');
|
|
|
@@ -86,32 +90,50 @@ class UserRole extends BaseController
|
|
|
{
|
|
|
$userInfo = $this->request->userInfo;
|
|
|
|
|
|
- $id = Request::param('id', 0, 'intval');
|
|
|
+ $id = Request::post('id', 0, 'intval');
|
|
|
if (!$id) {
|
|
|
return json_error([], '角色ID不能为空');
|
|
|
}
|
|
|
|
|
|
- $data = Request::only(['role_name', 'privileges']);
|
|
|
-
|
|
|
- if (empty($data['role_name'])) {
|
|
|
- return json_error([], '角色名称不能为空');
|
|
|
+ $role = UserRoleModel::getRoleById($id, $userInfo['merchant_id']);
|
|
|
+ if (!$role) {
|
|
|
+ return json_error([], '角色不存在');
|
|
|
}
|
|
|
|
|
|
- if (empty($data['privileges']) || !is_array($data['privileges'])) {
|
|
|
- return json_error([], '权限配置不能为空');
|
|
|
+ // 获取请求中提供的所有可更新字段
|
|
|
+ $requestData = Request::post();
|
|
|
+ $allowedFields = ['role_name', 'privileges'];
|
|
|
+ $updateData = [];
|
|
|
+
|
|
|
+ // 只处理请求中存在且允许更新的字段
|
|
|
+ foreach ($allowedFields as $field) {
|
|
|
+ if (array_key_exists($field, $requestData)) {
|
|
|
+ $updateData[$field] = $requestData[$field];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- $role = UserRoleModel::getRoleById($id, $userInfo['merchant_id']);
|
|
|
- if (!$role) {
|
|
|
- return json_error([], '角色不存在');
|
|
|
+ if (empty($updateData)) {
|
|
|
+ return json_error([], '没有要更新的数据');
|
|
|
}
|
|
|
|
|
|
- if (UserRoleModel::checkRoleExists($data['role_name'], $userInfo['merchant_id'], $id)) {
|
|
|
- return json_error([], '角色名称已存在');
|
|
|
+ // 使用验证器进行字段验证
|
|
|
+ $validate = new UserRoleValidate();
|
|
|
+
|
|
|
+ // 只验证传入的字段
|
|
|
+ if (!$validate->only(array_keys($updateData))->check($updateData)) {
|
|
|
+ return json_error([], $validate->getError());
|
|
|
}
|
|
|
|
|
|
+ // 额外的业务逻辑验证
|
|
|
+ // 检查角色名称是否重复
|
|
|
+ if (isset($updateData['role_name'])) {
|
|
|
+ if (UserRoleModel::checkRoleExists($updateData['role_name'], $userInfo['merchant_id'], $id)) {
|
|
|
+ return json_error([], '角色名称已存在');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
try {
|
|
|
- UserRoleModel::updateRole($id, $userInfo['merchant_id'], $data);
|
|
|
+ UserRoleModel::updateRole($id, $userInfo['merchant_id'], $updateData);
|
|
|
return json_success([], '更新角色成功');
|
|
|
} catch (\Exception $e) {
|
|
|
return json_error([], '更新角色失败:' . $e->getMessage());
|